Serial Library class method

Sep 28, 2021 at 4:32pm
Hi all,

I am using the Serialib serial library, but am having trouble understanding how to use the below class method. More specifically, the first method argument of char* pByte. I believe this is represents data read on the serial device, but I am unsure if I would need to eg declare a variable, then declare a char pointer to it, then pass that to the function. Or something else entirely. Any tips gratefully received. Thank you

1
2
3
4
 
char serialib::readChar	(	char * 	pByte,
const unsigned int 	timeOut_ms = 0 
)	
Sep 28, 2021 at 4:53pm
Do you know the standard library tools for file I/O like f ofstream.write() or ifstream.read()?
I am guessing here, as I do not know your library, but its probably like that: the char* most likely just means "bytes" of some data that you want to read or write.
its not unusual to see, for example, something like this:
struct s
{
bunch of simple fields like int/double
};
...
somefile.write((char*)(&some_s_variable), sizeof(s));

the char* in these cases is not really a buffer or true pointer, its really just a way to cast the data to bytes, in other words. Similar things are done on most network or serial port type interfaces.
Last edited on Sep 28, 2021 at 4:54pm
Sep 28, 2021 at 7:14pm
Thank you, you were right, this has definitely pointed me in the right direction! I will update this post shortly with a complete solution, in case anybody else needs it in future. Thanks again.
Sep 29, 2021 at 7:53am
What I did was:

//allocate some memory
char* pByte = new char [10];

//write and read a char
serial.writeChar('g');
usleep(10000);
serial.readChar (pByte,10000);
cout << pByte;

And I was able to read back the 'g' char I sent via a loopback test with a serial-to-usb adapter.
Sep 29, 2021 at 12:39pm
glad you got it working. if that readchar is max number to read, it should match your buffer size (and leave 1 spot for the zero .. see next thought).
pbyte is not ensured to be zero filled unless in debug mode. You need to put a zero into the next location after reading if you want to treat it as a c-string safely.
Last edited on Sep 29, 2021 at 12:40pm
Topic archived. No new replies allowed.