BS2 serial data null character to PC in C+
CircuitMage
Posts: 62
First, hello all! My first post here!
I have been working on this for a couple days, searched for directly related postings...I fear this is a simple problem for my less-than-experienced RS232 work.
So, I have my board working with hyperterminal, as far as SERIN and SEROUT and bi-dir communication via the serial port. BUT, in working to get my PC application to work in C+, I am having problems on the PC side.
Specifically, the PC reads the serial data OK. It gets a 6 byte character "TSTART" (no extra characters...null or CR) but when I try and verify it received it, it fails the check.
ex.
if (buf != "TSTART") , where buf is a string that contains the received data "TSTART"
I believe my C+ program expects a null terminator on the string. I am acutally using the PC to interface to another piece of equipment that has it's own COMREAD function that states it puts a NULL character at the end of the received string like C does. I am not sure how to add this to my BS2. I originally was using SEROUT TX, Baud, ["TSTART",CR] , and my C+ program was seeing "TSTART" with a CR box symbol at the end.
How do I get my program to see the SEROUT data like hyperterminal does?
Thanks in advance
I have been working on this for a couple days, searched for directly related postings...I fear this is a simple problem for my less-than-experienced RS232 work.
So, I have my board working with hyperterminal, as far as SERIN and SEROUT and bi-dir communication via the serial port. BUT, in working to get my PC application to work in C+, I am having problems on the PC side.
Specifically, the PC reads the serial data OK. It gets a 6 byte character "TSTART" (no extra characters...null or CR) but when I try and verify it received it, it fails the check.
ex.
if (buf != "TSTART") , where buf is a string that contains the received data "TSTART"
I believe my C+ program expects a null terminator on the string. I am acutally using the PC to interface to another piece of equipment that has it's own COMREAD function that states it puts a NULL character at the end of the received string like C does. I am not sure how to add this to my BS2. I originally was using SEROUT TX, Baud, ["TSTART",CR] , and my C+ program was seeing "TSTART" with a CR box symbol at the end.
How do I get my program to see the SEROUT data like hyperterminal does?
Thanks in advance
Comments
It all depends on how you're receiving the string on the C++ side. Often the receive routine will put the zero byte in when it sees a CR.
Update;
I added another character variable tstrt and verified it has the null terminator. I found the terminator missing from buf after comread .
So, I added what looked like the correct terminator using "buf[7] = '\0';" and verified it is there .
Looking at the hex values in MSVS , the buf is 0x0255fd80, while the tstrt string looks similar but has the value ox0255fd70. Does that mean anything to you?
Here's the loop that is locked up, it should compare and pass...
do
{
comread(pd, buf, 6);
printf("Received from BS2 Interface attempt# %d: %s\n",loop_counter, buf);
loop_counter++;
buf[7] = '\0';
} while ((buf!=tstrt)&&(loop_counter<20));
I tried the SEROUT TX, Baud, ["Stuff",0] method and it appears to do the same thing my buf[7] = '\0'; . So both look like they put the null terminator on the data.
However, the values are still not comparing....and the hex values are different, though the contents look the same.
Could it be my tstrt string is the problem? I'm initializing with;
char tstrt[7]="TSTART";
I used strcmp(buf,tstrt) and it worked...must have been checking the pointers (?).
Thanks for the spurs.