# getting started with multi-tasking and using the uarts.

**URL:** <https://forums.digi.com/t/getting-started-with-multi-tasking-and-using-the-uarts/10948>\
**Category:** Rabbit\
**Created:** [September 23, 2009, 5:11pm UTC](https://forums.digi.com/t/getting-started-with-multi-tasking-and-using-the-uarts/10948 "2009-09-23T17:11:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jleslie48](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@jleslie48](https://forums.digi.com/u/jleslie48)\
**Post date:** [September 23, 2009, 5:11pm UTC](https://forums.digi.com/t/getting-started-with-multi-tasking-and-using-the-uarts/10948/1 "2009-09-23T17:11:29Z")

</div>

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.

issues:  
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,

Jon

My test program:  
Note:  
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:

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
write\_uart01.c  
ok lets try to rewrite simple3wire to make it a bit more rohbust.

was:  
Simple3wire.c  
Z-World 2004

```
This program is used with BL2600 series controllers.

Description
===========
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 &lt;---&gt; RX
   RX &lt;---&gt; TX
	Gnd &lt;---&gt; Gnd

Connections
===========
1. Connect TXC to RXF located on J17.

```

1. Connect TXF to RXC located on J17.

```
Instructions
============
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

main()  
{  
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  
brdInit();

// Open serial port  
serCopen(BAUD232);  
serFopen(BAUD232);

// 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).  
serMode(0);

// Clear serial buffers  
serCwrFlush();  
serCrdFlush();  
serFwrFlush();  
serFrdFlush();

printf (“write uart test 090922\_01 strlen outbuffersz=%d  
“,strlen(outbuffersz));  
loopinit();  
while (1) {  
loophead();  
costate talker\_C always\_on {  
loopcounter++;  
sprintf(outbuffersz,”%05d%s”,loopcounter,msg01sz);  
wfd { cof\_serCwrite(outbuffersz, strlen(outbuffersz)); }  
waitfor (DelaySec(5));  
if (strlen(receivebuffersz) \> 0) {  
printf("talker\_c:%05d: received answer \>%s\<  
"  
,loopcounter,receivebuffersz);  
receivebuffersz[0] = 0;  
} //then  
else {  
printf(“talker\_c:%05d: no response, abort  
"  
,loopcounter);  
}//else  
}//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\<  
",  
strlen(inbuffersz),  
inbuffersz);  
strcpy(receivebuffersz,inbuffersz);  
}//costate listener\_C  
}//while (1) main loop

}//main
