Adding together ASCII(h) representation of bytes in an array<Byte>

I am trying to send a message consisting of a few byte using visual studios c++. The device requires serial communication and I am using array<Byte>: https://stackoverflow.com/questions/32770503/serial-port-write-c

The message to be send should start with 02 ASCII(h) i.e. STX. This is followed by 'A1C0' which when send using array<Byte> would correspond to 0x41, 0x31, 0x43, 0x30 in ASCII(h).

The other user input value is for a variable kV which is converted into hex after user provide it; e.g. if a user enter 7700, my code is supposed to convert it to a string of hex i.e. '1E14' which when sent using array<Byte> should correspond to 0x31, 0x45, 0x31, 0x34.

The issue I am faced with is that to calculate the checksum, I am required to add all the ASCII(h) which my array<Byte> consist of and then take the total’s one complement. I am struggling to add all these ASCII(h) primarily because I am sending ‘A1C0’ and ‘1E14’ as hex and depends on computer to interpret them in ASCII(h). In this situation I am struggling, with how to add all these ASCII(h) which in described case would require me to add ASCII(h): 2+41+31+43+30+31+45+31+34 = 1C2 and then take the total’s one’s complement i.e 3D which in ASCII(h) is represented 0x33, 0x44.

Following the checksum, I am required to send 03, i.e. ETX to tell the device that the message has ended. So the complete message to be send will contain STX, A1C0, kV, checksum and ETX which from my given explanation in this example will look like: 0x02, 0x41, 0x31, 0x43, 0x30, 0x31, 0x45, 0x31, 0x34, 0x33, 0x44, 0x03.

I know to add elements in an array as described here: http://www.cplusplus.com/forum/windows/32209/ but that is not simply what I want. Before reaching out here: I tried to also look online, but was not able to accomplish what I wanted:
https://social.msdn.microsoft.com/F...3a/sum-of-char-array-as-bytes?forum=vcgeneral
https://stackoverflow.com/questions/30223983/c-add-up-all-bytes-in-a-structure
http://forums.codeguru.com/showthread.php?383059-Perform-hex-addition-in-C-C

For the one’s complement, my code is based on: https://msdn.microsoft.com/en-us/library/dxt4z71k.aspx

I will really appreciate some help here! My current code is below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
static void message(SerialPort^ port){
	array<Byte>^ message = gcnew array<Byte>
	message[0] = 0x02;                           // STX
	message[1] = 'A1C0';                         // A1C0 in ASCII(h) represents 0X41, 0x31, 0x43, 0x30
	
	kV<< std::hex << decimal_value;  // user entered int decimal_value for kV using Cin - for example user entered 7700, converted to hex --- hex value of 7700 is 1E14
	std::string power ( kV.str() );
	message[2] = power;    // ASCII(h) of power (which is hex 1E14) represents 0x31, 0x45, 0x31, 0x34
	
	unsigned short CHKSUM = ???;   // PROBLEM: Calculating total --- adding all the Ascii(h) bytes in the array together
	cout << hex << CHKSUM << endl;  
	CHKSUM = ~CHKSUM;                 // Take one's complement  
	cout << hex << CHKSUM << endl;
	std::stringstream CHKSUM;
	message[3] = CHKSUM;
	message[4] = 0x03;                          // ETX

	port->Write(message, 0, message->Length);
}
Last edited on
'ASCII' is text representation.
'hex' notation is text representation.
They are one and the same.

The point is to get the checksum of the data (which is text) that you are sending in the packet.

Hope this helps.
Topic archived. No new replies allowed.