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.
#ifdef ETHERNUT2
#include <dev/lanc111.h>
#else
#include <dev/nicrtl.h>
#endif
#include <dev/uartavr.h>
#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <pro/dhcp.h>
#define BUFFERSIZE 128
#define TCPPORT 23
typedef struct {
FILE *cd_rs232;
FILE *cd_tcpip;
char cd_connected;
} CHANNEL;
void StreamCopy(FILE * istream, FILE * ostream, char *cop)
{
int cnt;
char *buff;
buff = malloc(BUFFERSIZE);
while (*cop) {
if ((cnt = fread(buff, 1, BUFFERSIZE, istream)) <= 0)
break;
if (*cop && (cnt = fwrite(buff, 1, cnt, ostream)) <= 0)
break;
if (*cop && fflush(ostream))
break;
}
*cop = 0;
free(buff);
}
THREAD(Receiver, arg)
{
CHANNEL *cdp = arg;
for (;;) {
if (cdp->cd_connected) {
NutThreadSetPriority(64);
StreamCopy(cdp->cd_rs232, cdp->cd_tcpip, &cdp->cd_connected);
NutThreadSetPriority(128);
}
NutThreadYield();
}
}
int main(void)
{
TCPSOCKET *sock;
CHANNEL cd;
u_long baud = 9600;
NutRegisterDevice(&devUart0, 0, 0);
NutRegisterDevice(&DEV_ETHER, 0x8300, 5);
cd.cd_rs232 = fopen("uart0", "r+b");
_ioctl(_fileno(cd.cd_rs232), UART_SETSPEED, &baud);
if (NutDhcpIfConfig("eth0", 0, 60000)) {
u_char my_mac[] = { 0x00, 0x06, 0x98, 0x20, 0x00, 0x00 };
if (NutDhcpIfConfig("eth0", my_mac, 60000)) {
u_long ip_addr = inet_addr("192.168.192.100");
u_long ip_mask = inet_addr("255.255.255.0");
NutNetIfConfig("eth0", my_mac, ip_addr, ip_mask);
}
}
NutThreadCreate("xmit", Receiver, &cd, 512);
cd.cd_connected = 0;
for (;;) {
sock = NutTcpCreateSocket();
NutTcpAccept(sock, TCPPORT);
cd.cd_tcpip = _fdopen((int) sock, "r+b");
cd.cd_connected = 1;
StreamCopy(cd.cd_tcpip, cd.cd_rs232, &cd.cd_connected);
fclose(cd.cd_tcpip);
NutTcpCloseSocket(sock);
}
}