linux - c - serial port, and the null char.

Colt45

Lifer
Apr 18, 2001
19,720
1
0
I've got a little app that writes a few bytes to the serial port, to my device on the other end.

Anyways, Sometimes I need to write the value 0 to the serial port, and when i write this to the file descriptor, it interprets this as ascii null, i.e. the end of the string, and doesn't send it or anything after it down the port.

any ideas? I'm pretty newbish at this :eek:
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
If you are trying to send the ascii character '0' over the serial port you need to send 48, not 0. Google 'ASCII chart' or 'ASCII table' to find a lookup table to convert between ASCII character and the binary equivalent.

For example

'0' = 48
'3' = 51
'A' = 65
'a' = 97
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
are you using an unsigned char array

how are you sending the data currently?

it depends on how your data is stored in memory if that is where you are grabbing it from. We were in a hurry so we wrote the code below. I'm sure someone here can give you a more elegant approach.

void sendPacket(UDP & udp, double _speed, double _curvature, double _time)
{
int magicNum = 4480;
double speed = _speed;
double curvature = _curvature;
double time = _time;
unsigned char commandMessage[32] =
{
*(((unsigned char*)&magicNum) + 3), *(((unsigned char*)&magicNum + 2)), *(((unsigned char*)&magicNum + 1)), *(((unsigned char*)&magicNum + 0)),
*(((unsigned char*)&speed) + 7), *(((unsigned char*)&speed) + 6), *(((unsigned char*)&speed) + 5), *(((unsigned char*)&speed) + 4),
*(((unsigned char*)&speed) + 3), *(((unsigned char*)&speed) + 2), *(((unsigned char*)&speed) + 1), *(((unsigned char*)&speed) + 0),
*(((unsigned char*)&curvature) + 7), *(((unsigned char*)&curvature) + 6), *(((unsigned char*)&curvature) + 5), *(((unsigned char*)&curvature) + 4),
*(((unsigned char*)&curvature) + 3), *(((unsigned char*)&curvature) + 2), *(((unsigned char*)&curvature) + 1), *(((unsigned char*)&curvature) + 0),
*(((unsigned char*)&time) + 7), *(((unsigned char*)&time) + 6), *(((unsigned char*)&time) + 5), *(((unsigned char*)&time) + 4),
*(((unsigned char*)&time) + 3), *(((unsigned char*)&time) + 2), *(((unsigned char*)&time) + 1), *(((unsigned char*)&time)),
*(((unsigned char*)&magicNum) + 3), *(((unsigned char*)&magicNum + 2)), *(((unsigned char*)&magicNum + 1)), *(((unsigned char*)&magicNum + 0))
};
udp.SendPacket((char*)commandMessage, 32);
}
 

Colt45

Lifer
Apr 18, 2001
19,720
1
0
I figured it out when I was driving to work, amateur night or what...

So you need to do a write(file_descriptor, string, length_of_string);.

I was doing:
write(fd, string, strlen(string));

And of course forgot that any string tools will treat 0 as \0, and quit on that char...

Anyways, that's fixed now, but now my microcontroller side of things is acting up!
 

Cogman

Lifer
Sep 19, 2000
10,286
145
106
Originally posted by: Colt45
I figured it out when I was driving to work, amateur night or what...

So you need to do a write(file_descriptor, string, length_of_string);.

I was doing:
write(fd, string, strlen(string));

And of course forgot that any string tools will treat 0 as \0, and quit on that char...

Anyways, that's fixed now, but now my microcontroller side of things is acting up!

Glad to hear it. That was what I was going to suggest (Sending an array of bytes rather then a null terminated string)
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
Just a suggestion, have the string length and a checksum in your array to keep your sanity if things start to get funky.