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

Talha

Junior Member
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/Forums/vstudio/en-US/6b85f124-d9de-46d5-a569-aecf08146f3a/sum-of-char-array-as-bytes?forum=vcgeneral

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:
Code:
static void message(SerialPort^ port){

array<Byte>^ message = gcnew array<Byte>

message = 0x02; // STX

message = 'A'; // A in ASCII(h) represents 0X41

message = '1'; // 1 in ASCII(h) represents 0x31

message = 'C'; // C n ASCII(h) represents 0x43

message = '0'; // 0 in ASCII(h) represents 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 = 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 = CHKSUM;

message = 0x03; // ETX

port->Write(message, 0, message->Length);

}

Last edited: