Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,
So I am implementing the brilliant serial class made by Ramone de Klein here:
Serial library for C++[^]

In his definition of the Read function, he writes the incoming bytes into an array of size 100. The problem is when I send a packet of data whose size I know to be less than 100, it comes bundled with garbage as the remaining bytes.

I want to filter this out.



Any suggestions as to what I could try next?

What I have tried:

1. Store the incoming data into a CString, but the function defines the buffer variable as void* buffer. So CString doesn't work.
2. Dynamically declare the buffer variable based on the number of bytes received, but the function does not accept an array of indeterminate size.
Posted
Updated 19-Apr-17 1:09am
Comments
Richard MacCutchan 19-Apr-17 6:40am    
No, it does not come with extra data, you just have garbage in your buffer. You need to check the size of each incoming message to ensure you process only the actual number of bytes that are received.
TheLostJedi 19-Apr-17 6:47am    
But how would I do that if I have to declare the size of the buffer before the port is read?
Will emptying the buffer help?
Richard MacCutchan 19-Apr-17 6:56am    
You check on each read how many bytes have been returned. Serial devices return data in random amounts, and you can never be certain how much is coming, so you need to manage it accordingly.

1 solution

Just don't use the additional bytes. You usually know how many bytes has been received.

Allocate a char* buffer that is large enough to hold the maximum length of data (optionally plus one byte for a null terminator).

If you know that the data are printable characters and you want to create a string, you have to specify the length or append a null byte to the buffer.

To copy the data into a CString you can use
C++
/* Use the constructor for char* because data are usually ASCII. */
/* Note that this converts to Unicode for Unicode builds. */
CString str(buffer, dwRead);

/* Using a CStringA and setString(): */
CStringA strA;
strA.SetString(buffer, dwRead);
Or append a null byte to the buffer:
C++
/* Check for buffer overrun. */
/* RX_BUF_SIZE is the size of the allocated buffer. */
ASSERT(dwRead < RX_BUF_SIZE);
buffer[dwRead] = 0;

CString str(buffer);
 
Share this answer
 
Comments
TheLostJedi 19-Apr-17 7:30am    
Does this mean that only the bytes before the null byte will be kept?
I'm sorry but I don't think I have understood some parts. You are appending a null after the number of bytes read. So if I have initialised the buffer with a larger size, does this mean that the conversion to a printable form will stop at the null?
Jochen Arndt 19-Apr-17 7:51am    
A C/C++ basic string must have a terminating null character. That is the end indicator. When receiving data this end indicator is not present. So you have two choices:
- Add it behind the received data (buffer must be large enough)
- Copy data to some kind of string using the known length

The C standard library string functions (like strcpy, strcmp) require that the strings are null terminated.

Other classes like CString track the length internally. But when passing classic strings (char*) they must be null terminated or the length must be specified.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900