rs232d/rs232d.c

Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

For additional information see http://www.ethernut.de/

Log
Revision 1.4 2005/11/22 09:14:13 haraldkipp Replaced specific device names by generalized macros.

Revision 1.3 2004/09/10 10:26:35 haraldkipp Removed old header files

Revision 1.2 2003/11/04 17:46:52 haraldkipp Adapted to Ethernut 2

Revision 1.1 2003/08/05 18:59:05 haraldkipp Release 3.3 update

Revision 1.7 2003/02/04 18:19:39 harald Version 3 released

Revision 1.6 2003/02/04 16:24:34 harald Adapted to version 3

Revision 1.5 2002/10/31 16:25:48 harald Mods by troth for Linux

Revision 1.3 2002/06/26 17:29:03 harald First pre-release with 2.4 stack

Revision 1.2 2002/06/12 11:22:55 harald *** empty log message ***

Revision 1.1 2002/06/04 19:00:49 harald First check in

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.

00001 
00080 #include <dev/board.h>
00081 
00082 #include <sys/heap.h>
00083 #include <sys/thread.h>
00084 #include <sys/timer.h>
00085 #include <sys/socket.h>
00086 
00087 #include <stdlib.h>
00088 #include <stdio.h>
00089 #include <string.h>
00090 #include <io.h>
00091 #include <fcntl.h>
00092 
00093 #include <arpa/inet.h>
00094 
00095 #include <pro/dhcp.h>
00096 
00097 #define BUFFERSIZE  128
00098 #define TCPPORT     23
00099 
00100 typedef struct {
00101     FILE *cd_rs232;
00102     FILE *cd_tcpip;
00103     char cd_connected;
00104 } CHANNEL;
00105 
00106 /*
00107  * Transfer data from input stream to output stream.
00108  */
00109 void StreamCopy(FILE * istream, FILE * ostream, char *cop)
00110 {
00111     int cnt;
00112     char *buff;
00113 
00114     buff = malloc(BUFFERSIZE);
00115     while (*cop) {
00116         if ((cnt = fread(buff, 1, BUFFERSIZE, istream)) <= 0)
00117             break;
00118         if (*cop && (cnt = fwrite(buff, 1, cnt, ostream)) <= 0)
00119             break;
00120         if (*cop && fflush(ostream))
00121             break;
00122     }
00123     *cop = 0;
00124     free(buff);
00125 }
00126 
00127 /*
00128  * From RS232 to socket.
00129  */
00130 THREAD(Receiver, arg)
00131 {
00132     CHANNEL *cdp = arg;
00133 
00134     for (;;) {
00135         if (cdp->cd_connected) {
00136             NutThreadSetPriority(64);
00137             /*
00138              * We are reading from the UART without any timeout. So we
00139              * won't return immediately when disconnected.
00140              */
00141             StreamCopy(cdp->cd_rs232, cdp->cd_tcpip, &cdp->cd_connected);
00142             NutThreadSetPriority(128);
00143         }
00144         NutThreadYield();
00145     }
00146 }
00147 
00148 /*
00149  * Main application routine. 
00150  *
00151  * Nut/OS automatically calls this entry after initialization.
00152  */
00153 int main(void)
00154 {
00155     TCPSOCKET *sock;
00156     CHANNEL cd;
00157     u_long baud = 9600;
00158 
00159     /*
00160      * Register our devices.
00161      */
00162     NutRegisterDevice(&DEV_UART, 0, 0);
00163     NutRegisterDevice(&DEV_ETHER, 0x8300, 5);
00164 
00165     /*
00166      * Setup the uart device.
00167      */
00168     cd.cd_rs232 = fopen(DEV_UART_NAME, "r+b");
00169     _ioctl(_fileno(cd.cd_rs232), UART_SETSPEED, &baud);
00170 
00171     /*
00172      * Setup the ethernet device. Try DHCP first. If this is
00173      * the first time boot with empty EEPROM and no DHCP server
00174      * was found, use hardcoded values.
00175      */
00176     if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {
00177         /* No valid EEPROM contents, use hard coded MAC. */
00178         u_char my_mac[] = { 0x00, 0x06, 0x98, 0x20, 0x00, 0x00 };
00179 
00180         if (NutDhcpIfConfig("eth0", my_mac, 60000)) {
00181             /* No DHCP server found, use hard coded IP address. */
00182             u_long ip_addr = inet_addr("192.168.192.100");
00183             u_long ip_mask = inet_addr("255.255.255.0");
00184 
00185             NutNetIfConfig("eth0", my_mac, ip_addr, ip_mask);
00186             /* If not in a local network, we must also call 
00187                NutIpRouteAdd() to configure the routing. */
00188         }
00189     }
00190 
00191     /*
00192      * Start a RS232 receiver thread.
00193      */
00194     NutThreadCreate("xmit", Receiver, &cd, 512);
00195 
00196     /*
00197      * Now loop endless for connections.
00198      */
00199     cd.cd_connected = 0;
00200     for (;;) {
00201         /*
00202          * Create a socket and listen for a client.
00203          */
00204         sock = NutTcpCreateSocket();
00205         NutTcpAccept(sock, TCPPORT);
00206 
00207         /*
00208          * Open a stdio stream assigned to the connected socket.
00209          */
00210         cd.cd_tcpip = _fdopen((int) sock, "r+b");
00211         cd.cd_connected = 1;
00212 
00213         /*
00214          * Call RS232 transmit routine. On return we will be
00215          * disconnected again.
00216          */
00217         StreamCopy(cd.cd_tcpip, cd.cd_rs232, &cd.cd_connected);
00218 
00219         /*
00220          * Close the stream.
00221          */
00222         fclose(cd.cd_tcpip);
00223 
00224         /*
00225          * Close our socket.
00226          */
00227         NutTcpCloseSocket(sock);
00228     }
00229 }

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