There is no need to use C++ string classes. Just use
char
buffers instead. This avoids problems with wide char strings (e.g.
CString
with MFC). When using using string classes use the corresponding functions to access the characters (e.g.
string::c_str
).
Avoid using helper variables and casts. This makes the code unreadable and is prone to errors (the number of characters to be written in your case).
This should do the job:
char lpBuffer[] = "100001111";
DWORD dNoOFBytestoWrite;
DWORD dNoOfBytesWritten = strlen(lpBuffer);
Status = WriteFile(hComm, lpBuffer, dNoOFBytestoWrite, &dNoOfBytesWritten, NULL);
But I guess that you will still get a result similar to the one shown in your question. It looks like you are receiving the data and did not append a NULL byte before printing the received data out so that the print function can not determine the end of the string. A possible solution would be sending also the NULL byte:
DWORD dNoOfBytesWritten = strlen(lpBuffer) + 1;
But this is rather uncommon with serial communications using printable characters. Instead, change your receiving function to append a NULL byte when there are no more data.