getting started with multi-tasking and using the uarts.

wanted working example:
a send and ack example of a working uart. The idea being that the rabbit sends out a message, and waits for a response, but not indefinitely, however as with RS232, sometimes the cable is disconnected, and no response is ever received.

The workings of dynamic C’s cof_serXread function has a timeout capability, that’s great, but it is based on time AFTER the comm has started. While this is a very clever way to handle variable length messages (by assuming the timeout is really, “ok, im done”) it means that if no response is ever forthcoming, we will wait forever.

To handle this situation, I’ve added a timeout factor on the costate of the talker to wait for a fixed period of time, and then check the response.

I think I did this correctly, but being new to this idea of cofunctions, waitfordone, etc, I’m not sure if I did this as well as I could of. I’ll attach my program example at the bottom.

I guess my confusion is with the wfd () interaction with the co-state. I was expecting that I would need more in the wfd to get this to work, but that is not the case. What I’m trying to say is I don’t know why this works, and if I did it as elegantly as possible.

I also don’t understand the cofunction idea with respect to this demo program. I mean whats the difference between
cof_serCread and serCread? And why the WFD for cof_serCread? I would of thought that cof_serCread was the same as wdf {serCread(…);} but as cof_serCread needs the wfd also; I know my understanding is wrong.

And why doesn’t the printf need a wfd? isn’t it possible that a printf in two different costates get messed up as they trip on each other?

Looking forward to an interesting discussion,


My test program:
everything of interest is at the bottom in the while(1) loop and the two co-states.
looks like the html parser of this forum has made a mess of things, I’ve included the C source as an attachment as well:

ok lets try to rewrite simple3wire to make it a bit more rohbust.

Z-World 2004

This program is used with BL2600 series controllers.

This program demonstrates basic initialization for a
simple RS232 3-wire loopback displayed in STDIO window.

Here’s the typical connections for a 3 wire interface that
would be made between controllers.

   TX <---> RX
   RX <---> TX
	Gnd <---> Gnd

1. Connect TXC to RXF located on J17.
  1. Connect TXF to RXC located on J17.
1. Compile and run this program.
2. View STDIO window for sample program results.

090922 JL first run works!
090922 JL let’s get it so it doesn’t lock up, use waitfor rather than
while wait forever. - well that’s no good, lets revert it back
and we are going to have to go with something completely different.
090923 JL OK talker and listener are working, you send out a message, and wait
5 seconds for a response. If 2 responses come within the 5 seconds,
you destroy the first response. May or may not be what you want, but
basically this demonstrates a send and ack with timeout for no response.

// Set a default of declaring all local variables “auto” (on stack)
#class auto

// serial buffer size
#define CINBUFSIZE 15
#define COUTBUFSIZE 15
#define FINBUFSIZE 15
#define FOUTBUFSIZE 15

// serial baud rate
#define BAUD232 115200

auto int nIn;
auto char cOut;
char outbuffersz[44];
char inbuffersz[44];
char receivebuffersz[44];
char msg01sz[17];
int nread;
auto int tempint;
int loopcounter;

outbuffersz[0] = 0;
inbuffersz[0] = 0;
receivebuffersz[0] = 0;
strcpy(msg01sz, ":hello world!!!
loopcounter = 0;

// Initialize controller

// Open serial port

// Can use serial mode 0 - 2 for 3 wire RS232 operation
// depending on what serial port you select and serMode
// must be executed after the serXopen function(s).

// Clear serial buffers

printf (“write uart test 090922_01 strlen outbuffersz=%d
while (1) {
costate talker_C always_on {
wfd { cof_serCwrite(outbuffersz, strlen(outbuffersz)); }
waitfor (DelaySec(5));
if (strlen(receivebuffersz) > 0) {
printf("talker_c:%05d: received answer >%s<
receivebuffersz[0] = 0;
} //then
else {
printf(“talker_c:%05d: no response, abort
}//costate talker_C
costate listener_C always_on {
wfd nread = cof_serCread(inbuffersz, 5, 2000); //2 seconds max between characters
inbuffersz[nread] = 0;
printf (” listener_C:got %d chars: >%s<
}//costate listener_C
}//while (1) main loop