Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

rs232d/rs232d.c

Simple RS232 server. Use a serial cable to connect the RS232 port of the Ethernut Board with a COM port of a PC. Start a terminal program and a telnet client on the PC. Telnet should connect to the Ethernut Board.

Characters typed in the telnet window will appear in the terminal program window and vice versa. Baudrate is 9600.

#include <string.h>

#include <dev/nicrtl.h>
#include <dev/uartavr.h>

#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/print.h>

#include <netinet/sostream.h>
#include <arpa/inet.h>

volatile u_char connected = 0;

volatile TCPSOCKET *sock;
NUTDEVICE *uart;

u_char rxbuff[256];
u_char txbuff[256];

extern int main_loop( void );

#define NutWrite(s) NutDeviceWrite(uart, s, strlen(s));NutPrintFlush(uart);NutSleep(500);

#define USE_ETHERNET 1
/*
 * From RS232 to socket.
 */
void Receiver(void)
{
    int rlen;
    int slen;
    int c;
#if USE_ETHERNET
    for(;;) {
        if(connected)
            if((rlen = NutDeviceRead(uart, rxbuff, sizeof(rxbuff))) > 0)
                for(c = 0; connected && c < rlen; c += slen) 
                    if((slen = NutTcpSend(sock, rxbuff + c, rlen - c)) < 0)
                        connected = 0;
        NutThreadYield();
    }
#endif
}

/*
 * From socket to RS232.
 */
void Transmitter(void)
{
    int rlen;
    int slen;
    int c;
#if USE_ETHERNET

    connected = 1;
    while(connected) {
        if((rlen = NutTcpReceive(sock, txbuff, sizeof(txbuff))) < 0)
            break;
        for(c = 0; connected && c < rlen; c += slen) {
            if((slen = NutDeviceWrite(uart, txbuff + c, rlen - c)) < 0)
                break;
            NutDeviceWrite(uart, 0, 0);
        }
        NutThreadYield();
    }
    connected = 0;
#endif
}
char banner[] = "\r\nSample:12345678901234567890\r\n";
char banner2[] = "LOOP:12345678901234567890\r\n";
char banner3[] = "ETHERNET:12345678901234567890\r\n";
char banner3a[] = "ETHERNET-OK:12345678901234567890\r\n";
char banner4[] = "MainLOOP:12345678901234567890\r\n";

THREAD(Loop, arg)
{
       NutWrite( "Entering MainLoop\r\n");
    //main_loop();
    for(;;) {
       NutWrite( banner2);
    }
}

THREAD(EthCard, arg)
{
#if USE_ETHERNET
    /*
     * Register Realtek controller at address 8300 hex
     * and interrupt 5.
     */
    NutWrite(banner3);
   NutRegisterDevice(&devEth0, 0x8300, 5);
    NutWrite(banner3a);

    /*
     * Configure lan interface. 
     *
     */
    NutNetAutoConfig("eth0");
   // Receiver();

    /*
     * Now loop endless for connections.
     */
    for(;;) {
        /*
         * Create a socket.
         */
     NutWrite( banner3);
      sock = NutTcpCreateSocket();

        /*
         * Listen on telnet port. If we return,
         * we got a client.
         */
        //NutTcpAccept(sock, 23);

        /*
         * Call RS232 transmit routine.
         */
        //Transmitter();

        /*
         * Close our socket.
         */
        NutTcpCloseSocket(sock);
    }
#endif
    for(;;)
    {
             NutWrite( banner3);
    }
}

/*
 * Main application routine. 
 *
 * Nut/OS automatically calls this entry after initialization.
 */
THREAD(NutMain, arg)
{
    u_long baud = 9600;

    NutRegisterDevice(&devUart0, 0, 0);
    uart = NutDeviceOpen("uart0");
    NutDeviceIOCtl(uart, UART_SETSPEED, &baud);
    NutWrite( banner);

     NutThreadCreate("loop", Loop, 0, 1024);
     NutThreadCreate("card", EthCard, 0, 1024);
     for(;;){
           NutWrite( banner4);
     }

}

© 2000-2003 by egnite Software GmbH - visit http://www.ethernut.de/