NULL character problem
-
I'm reading data from the serial port with ReadFile(). Now I know that in the middle of the read, I will get a null character (0x00). After the ReadFile statement, the string it's put the characters into only exists until the null character, thinking that position is the null terminating the string, but there's more data that it read past that null which I need. Previously, I'd read one character at a time, if it was a null, it would toss it out. If it wasn't null, it'd copy it to a string. That works but it's too complicated and takes too much time communicating with the serial port. Anyone have an idea I can bypass this null but not have to read each character one at a time. When reading one character at a time, there are some really sticky and annoying timing issues that quickly turn into a minefield for me so any way I can avoid it is fine with me. Thanks in advance, Nate.
-
I'm reading data from the serial port with ReadFile(). Now I know that in the middle of the read, I will get a null character (0x00). After the ReadFile statement, the string it's put the characters into only exists until the null character, thinking that position is the null terminating the string, but there's more data that it read past that null which I need. Previously, I'd read one character at a time, if it was a null, it would toss it out. If it wasn't null, it'd copy it to a string. That works but it's too complicated and takes too much time communicating with the serial port. Anyone have an idea I can bypass this null but not have to read each character one at a time. When reading one character at a time, there are some really sticky and annoying timing issues that quickly turn into a minefield for me so any way I can avoid it is fine with me. Thanks in advance, Nate.
It's not because there is a null character in your string that the rest of your data isn't read from the serial port. What you can do is read the data (all at once) in to a char[] and then copy character per character into a string if you want to... Something like this: int i, index=0; char DataBuffer[], StringBuffer[]; ReadFile(...) for (i=0; i < NumberOfBytesRead; i++) { if (DataBuffer[i] != '\0') StringBuffer[index++] = DataBuffer[i]; } This way you don't have to worry about communication timing and so on. Don't think you are, know you are...
-
I'm reading data from the serial port with ReadFile(). Now I know that in the middle of the read, I will get a null character (0x00). After the ReadFile statement, the string it's put the characters into only exists until the null character, thinking that position is the null terminating the string, but there's more data that it read past that null which I need. Previously, I'd read one character at a time, if it was a null, it would toss it out. If it wasn't null, it'd copy it to a string. That works but it's too complicated and takes too much time communicating with the serial port. Anyone have an idea I can bypass this null but not have to read each character one at a time. When reading one character at a time, there are some really sticky and annoying timing issues that quickly turn into a minefield for me so any way I can avoid it is fine with me. Thanks in advance, Nate.
Since the buffer can contain
'\0'
characters, you cannot use any string-related functions likestrlen()
orstrcpy()
. You'll need to usememcpy()
instead if you need to transfer data from one variable to another.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Since the buffer can contain
'\0'
characters, you cannot use any string-related functions likestrlen()
orstrcpy()
. You'll need to usememcpy()
instead if you need to transfer data from one variable to another.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
ah, makes sense because the string functions read until the first null, good call.
-
It's not because there is a null character in your string that the rest of your data isn't read from the serial port. What you can do is read the data (all at once) in to a char[] and then copy character per character into a string if you want to... Something like this: int i, index=0; char DataBuffer[], StringBuffer[]; ReadFile(...) for (i=0; i < NumberOfBytesRead; i++) { if (DataBuffer[i] != '\0') StringBuffer[index++] = DataBuffer[i]; } This way you don't have to worry about communication timing and so on. Don't think you are, know you are...
That's kind of what I was doing, but I was reading in a byte at a time instead of reading them all in one step, then copy the character if it wasn't null. But that looks pretty sharp though, good call, thanks.