MOD5213: RS232 communication

Discussion to talk about software related topics only.
Post Reply
CelNB
Posts: 10
Joined: Fri Aug 21, 2009 3:43 pm

MOD5213: RS232 communication

Post by CelNB »

Hi,

I used a MOD5213 talk to a device through RS232 (UART 1). MOD5213 sent a command to the device and device feedback status. MOD5213 will write feedback status on UART 0. Problem is: when MOD5213 read the feedback from the device (on UART1), it sent back the readout content to the device (on UART1), which caused problem. I use NBEClipse and the code is as follow:

{
SimpleUart(1, 19200); // Initialize UART1
writestring(1, "GetStatus\r\n"); // Send command to UART1

OSTimeDly(2);
assign_stdio(1);

if ( charavail(1) ) // Listen to UART1
{
gets(buffer); // Read UART1 if feedback detected
assign_stdio(0);
}
else
{
assign_stdio(0);
writestring(0, "Communication ERROR\r\n");
return 1;
}
writestring(0, buffer); // Write device feedback value to UART0
writestring(0, "\r\n");
}

Thanks.
User avatar
lgitlitz
Posts: 331
Joined: Wed Apr 23, 2008 11:43 am
Location: San Diego, CA
Contact:

Re: MOD5213: RS232 communication

Post by lgitlitz »

Look at the function "int ioctl( int fd, int cmd );" in:
C:\nburn\include_nn\iosys.h
STDIN FD = 0
STDOUT FD = 1
STDERR FD = 2

So all you need to turn off echo on stdin:
#include <iosys.h>
...
ioctl( 0, IOCTL_CLR | IOCTL_RX_ECHO );

Also it is not a good idea to keep swapping stdio between ports like you are doing. The inner workings of the stdio library must manipulate the data before sending it to the UART buffer. By swapping stdio you open up the possibility that not all the stdio data makes it to the correct UART buffer before you swap. I believe if you keep all the swapping in the same task you should be OK but you will definitely run into problems if you start swapping stdio in mutliple tasks without some sort of flush before the swap.

The safer method of using stdio with multiple UARTs is to create file pointers to the UARTs:
FILE *create_file( int portnum );
You can then use the file stdio function on this file such as fprintf or fgets without having to swap your stdio UART.

-Larry Gitlitz
CelNB
Posts: 10
Joined: Fri Aug 21, 2009 3:43 pm

Re: MOD5213: RS232 communication

Post by CelNB »

Hi Larry,

Thanks for the reply. I tried to use ioctl( 0, IOCTL_CLR | IOCTL_RX_ECHO ) in my program, but there is an Error during compiling: undefined reference to `ioctl(int, int)'

I included <iosys.h>

Thanks,
Yi
User avatar
lgitlitz
Posts: 331
Joined: Wed Apr 23, 2008 11:43 am
Location: San Diego, CA
Contact:

Re: MOD5213: RS232 communication

Post by lgitlitz »

It looks like there was a bug that is corrected in the newer version of the NNDK release. This function is written in a C file but you are using them in a C++ file. This is fine but you must do an extern "C" to avoid the C++ name mangling.

An easy way to do this is to add the following to the beginning of the iosys header file:
#ifdef __cplusplus
extern "C"
{
#endif

Then add the following at the end of the iosys header file:
#ifdef __cplusplus
}
#endif
Post Reply