Dont understand this code

Hello everyone,

Can somebody explain me what this code means? I want to change this code so that i can send 1 Byte to a serial port.
Thx

// change function to write a byte to the serial port
char __fastcall serial::SendMessage(char *MessStruct, int MessLength)
{
int count;
int bcount;
DWORD dwBytesWritten;
char chksum = dle^stx^dle^etx;

if(!commValid) return 0;

sendBuffer[0] = dle; //indicate start of message
sendBuffer[1] = stx;

for ( count = 2 , bcount = 0 ; bcount < MessLength ; bcount++ )
{
if (MessStruct[bcount] == dle) //perform byte stuffing
{
sendBuffer[count++] = dle;
chksum ^= dle; //calculate checksum
}
sendBuffer[count] = MessStruct[bcount];
chksum ^= sendBuffer[count];
count++;
}

sendBuffer[count++] = dle; //indicate end of message
sendBuffer[count++] = etx;
sendBuffer[count++] = chksum;
sendBuffer[count] = '\0';

txLength = count;
if (!WriteFile(hcomm , sendBuffer ,txLength , &dwBytesWritten , NULL))
return 0;
analysis->line1Add(sendBuffer,txLength);
return 1; //indicates write operation successfull
}

// change function to write a byte to the serial port
char __fastcall serial::SendMessageByte(char *MessStruct, int MessLength)
{
int bcount;
DWORD dwBytesWritten;

if(!commValid) return 0;

for (bcount = 0 ; bcount < MessLength ; bcount++ )
{
sendBuffer[bcount] = MessStruct[bcount];
}

sendBuffer[bcount] = '\0';

txLength = bcount;

if (!WriteFile(hcomm , sendBuffer ,txLength , &dwBytesWritten , NULL))
return 0;
analysis->line1Add(sendBuffer,txLength);
return 1; //indicates write operation successfull
}
Can you please edit your post and add code formatting to the code using the relevant tag.

If you don't understand that code, it's not clear what level to pitch an answer. Don't panic, there's a full explanation here.
http://www.codeproject.com/KB/system/serial_com.aspx
Topic archived. No new replies allowed.