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.5 2008/01/31 09:38:15 haraldkipp Added return statement in main to avoid warnings with latest GCC.

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

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