I am using SDL-1.2.15. For technical reasons, I cannot switch to later version.
I need to read a serial port in the main loop of my program (attach screenshot). But it doesn’t work. It would be great if I cound use this code like this, as I want it to be as lightweight as possible.
Serial port reading works perfectly in a program without SDL (command line). What did I do wrong?
What did you mean for “doesn’t work”…? In case you really unsure check did read returned error (in errno) like this first;
#include <errno.h> // at top of the .c
...
int x0 = 5, y0 = 5;
ssize_t readsize; // Add
...
errno = 0; // Add
readsize = read(cfd, &bytes, 1);
printf("read: %d, %ld => %d\n", cfd, readsize, errno); // Add
if read succeeded it should return some positive number in readsize of above. In case it failed it return error code in errno variable. Meanings of errno value varies OS and CPU.
Since it seems you’re trying to react serial port input per-byte basis, SDL_PollEvent should have its own while loop like this:
Thanks for your answer! I tried what you suggested to me, but it appears to be not working.
I tried various file operation in the main loop of my code, but it seems calling SDL_Flip or SDL_FillRect (I failed in determining which one is problematic) blocks all standard I/O operation, including console output, file output, using write, fwrite, sprintf.
I also tried to use SDL_RWops, but as it’s based on fopen() and the FILE* pointer, it is not suitable for serial port reading.
Is there a way to read a serial port and use the data inside my SDL program?
Hmm, do you have any other PC to see printf outputs during the program running? Debugging without console output is super-hard and I’d guess it’s worth to try setup SSH server on the note PC and log in from another PC etc. Trying on X11 first would be considerable alternative as well.