Event and serial port

Hi, first of all I’ll say thank you to those who replied my last post a
week ago. I’ve been away for a week to my hometown.

The application I’m developing uses a device that’s attached to serial
port. The device is used as input and output. The device has a program
embedded in it that can send (input) and receive (output) a string and
it’s built like a NULL modem. The input is in a form of a button. If the
button is pressed, it’ll send a series of string continuously (until I
send a string to the device to stop it). And the output is in a form of a
LED. If I send a string to it, the LED will blink. My question is, can
this be handled in SDL_Event?

Thanks in advance.

This is a two part answer. First I will attempt to address working
with a serial device. Secondly I will talk about SDL events.

SDL does not provider support for serial port devices. You will have
to use platform specific code to access the serial device – or a
cross-platform library that implements the platform specific code for
serial device access on various platforms. The main point: SDL has no
built in support for serial devices.

Onto events. SDL provides reserved an event type for the programmer to
implement his own events. Just as the event type for mouse movements
is SDL_MOUSEMOTION, the event type for this programmer-defined
SDL_event is SDL_USEREVENT.

The SDL_UserEvent structure is structured to give you your own
tier-two event code. Instead of being named “type” it is named "code."
It also gives you two pointers which you can point at arbitrary
objects containing arbitrary amounts of data, or cast / union and use
as scalar data types (which you probably want if you’re just receiving
characters.) So it’s pretty flexible.

typedef struct{
Uint8 type; // must be SDL_USEREVENT i think
int code;
void *data1;
void *data2;
} SDL_UserEvent;

So theoretically, on many platforms you should be able to register
your hook / callback routine that your kernel will invoke within your
program, in between execution of your main thread (in a manner of
speaking,) which can then allocate an SDL_UserEvent on the heap
(malloc(),) and use SDL_PushEvent(eventPtr) into the SDL event queue.

However personally I recommend against this. The SDL event queue can
easily create a lot more overhead than should be necessary for just
receiving input from a character stream. Also I imagine if your device
or connection went awry and your SDL program started receiving data
very quickly, it may slow down immensely, or fill up more quickly than
it’s emptying (spend more time in interrupts than in main execution)
and run out of memory (that’s not a fun thing to do, especially on
Windows.)

If you are not committed to C or C++, I highly recommend Python. There
are some really simple cross-platform serial I/O libraries out
there. For SDL in Python visit Pygame.org.On 10/30/06, benang at cs.its.ac.id wrote:

Hi, first of all I’ll say thank you to those who replied my last post a
week ago. I’ve been away for a week to my hometown.

The application I’m developing uses a device that’s attached to serial
port. The device is used as input and output. The device has a program
embedded in it that can send (input) and receive (output) a string and
it’s built like a NULL modem. The input is in a form of a button. If the
button is pressed, it’ll send a series of string continuously (until I
send a string to the device to stop it). And the output is in a form of a
LED. If I send a string to it, the LED will blink. My question is, can
this be handled in SDL_Event?

Thanks in advance.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

OIC, thanks. Then using a thread to handle the serial port is unavoidable
then. And I thought it can be done in SDL_event so that I didn’t have to
use a thread. So that’s one more thread to add to the program. Sigh… I
don’t like threading very much. Have a bad experience with it.> This is a two part answer. First I will attempt to address working

with a serial device. Secondly I will talk about SDL events.

SDL does not provider support for serial port devices. You will have
to use platform specific code to access the serial device – or a
cross-platform library that implements the platform specific code for
serial device access on various platforms. The main point: SDL has no
built in support for serial devices.

Onto events. SDL provides reserved an event type for the programmer to
implement his own events. Just as the event type for mouse movements
is SDL_MOUSEMOTION, the event type for this programmer-defined
SDL_event is SDL_USEREVENT.

The SDL_UserEvent structure is structured to give you your own
tier-two event code. Instead of being named “type” it is named "code."
It also gives you two pointers which you can point at arbitrary
objects containing arbitrary amounts of data, or cast / union and use
as scalar data types (which you probably want if you’re just receiving
characters.) So it’s pretty flexible.

typedef struct{
Uint8 type; // must be SDL_USEREVENT i think
int code;
void *data1;
void *data2;
} SDL_UserEvent;

So theoretically, on many platforms you should be able to register
your hook / callback routine that your kernel will invoke within your
program, in between execution of your main thread (in a manner of
speaking,) which can then allocate an SDL_UserEvent on the heap
(malloc(),) and use SDL_PushEvent(eventPtr) into the SDL event queue.

However personally I recommend against this. The SDL event queue can
easily create a lot more overhead than should be necessary for just
receiving input from a character stream. Also I imagine if your device
or connection went awry and your SDL program started receiving data
very quickly, it may slow down immensely, or fill up more quickly than
it’s emptying (spend more time in interrupts than in main execution)
and run out of memory (that’s not a fun thing to do, especially on
Windows.)

If you are not committed to C or C++, I highly recommend Python. There
are some really simple cross-platform serial I/O libraries out
there. For SDL in Python visit Pygame.org.

On 10/30/06, @benang_at_cs.its.ac <@benang_at_cs.its.ac> wrote:

Hi, first of all I’ll say thank you to those who replied my last post a
week ago. I’ve been away for a week to my hometown.

The application I’m developing uses a device that’s attached to serial
port. The device is used as input and output. The device has a program
embedded in it that can send (input) and receive (output) a string and
it’s built like a NULL modem. The input is in a form of a button. If the
button is pressed, it’ll send a series of string continuously (until I
send a string to the device to stop it). And the output is in a form of
a
LED. If I send a string to it, the LED will blink. My question is, can
this be handled in SDL_Event?

Thanks in advance.