Use serial port with SDL 1.2.15

Hello !

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?

Please keep in mind I am still a newbie…

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:

while(SDL_PollEvent(&event)){
    switch(event.type)
    {
        ...
    }
}

because SDL might get multiple events regardless how many bytes received from the serial port.

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?

I remember very little of SDL 1.x, but those shouldn’t be blocking I/O. What platform is this?

Debian GNU/Linux 11 (Linux 5.10.92-1 i686) on a 2002 HP Pavilion ze4111s laptop. No desktop, no display server.

I want to turn it into an arcade game console, and SDL-1.2.15 works well directly with framebuffer graphics (what SDL 2.x does not).

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.

I cannot, this computer is not powerful enough… SSH is a good suggestion. I will try it and see what happens.