/* TestNullModem - Test a null-modem connection (loopback) to make sure traffic is getting through Copyright (C) 2004 Frank Sorenson (frank@tuxrocks.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Compiling: gcc TestNullModem.cpp -o TestNullModem */ #include #include #include #include #include #include int SetTerminalSettings(int PortNum) { struct termios TermIO; int Result; printf("Setting terminal settings\n"); Result = tcgetattr(PortNum, &TermIO); if (Result) { printf("Error while running tcgetattr\n\r"); } cfsetispeed(&TermIO, B9600); cfsetospeed(&TermIO, B9600); TermIO.c_cflag |= (CREAD | CLOCAL); TermIO.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | ECHOK | ECHOCTL | ECHOKE); TermIO.c_iflag &= ~(ICRNL | IXON); TermIO.c_oflag &= ~(OPOST | ONLCR); TermIO.c_cflag &= ~PARENB; TermIO.c_cflag &= ~CSTOPB; TermIO.c_cflag &= ~CSIZE; TermIO.c_cflag &= ~CRTSCTS; TermIO.c_cflag |= CS8; Result = tcsetattr(PortNum, TCSANOW, &TermIO); if (Result) { printf("Error while running tcsetattr\n\r"); return(-1); } return(0); } int OpenDevice(char *DeviceFile) { int PortNum; printf("Opening %s\n", DeviceFile); PortNum = open(DeviceFile, O_RDWR | O_NOCTTY); if (PortNum == -1) { printf("Could not open device %s\n", DeviceFile); exit(-1); } SetTerminalSettings(PortNum); return(PortNum); } int DataReady(int PortNum) { fd_set rsel; int MaxFDs = 32; int Result; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; FD_ZERO(&rsel); FD_SET(PortNum, &rsel); Result = select(MaxFDs, &rsel, NULL, NULL, &tv); if (!Result) return(0); if (FD_ISSET(PortNum, &rsel) == 0) return(0); return(1); } int ReceiveChar(int PortNum) { int Result; unsigned char Data; Result = read(PortNum, &Data, 1); if (Result == 1) return(Data); return(-1); } int TransmitChar(int PortNum, unsigned char Data) { int Result; Result = write(PortNum, &Data, 1); if (Result == 1) return(1); if (errno == ENODEV) { printf("Lost the connection to the port - ENODEV\n"); exit(-1); } printf("Could not send character - %d\n", errno); return(-1); } int GenTraffic(int PortNum) { unsigned char TempChar; unsigned long BytesSent = 0; unsigned long BytesReceived = 0; double Percentage; printf("Starting traffic generator\n"); while (1) { TransmitChar(PortNum, '#'); BytesSent ++; if (DataReady(PortNum)) { BytesReceived ++; TempChar = ReceiveChar(PortNum); } Percentage = (BytesReceived * 1.0) / (BytesSent * 1.0) * 100.0; printf("%ld sent, %ld received, %ld lost, %.2lf%%\r", BytesSent, BytesReceived, (BytesSent - BytesReceived), Percentage); fflush(stdout); usleep(1000); } } int main(int argc, char *argv[]) { int PortNum; if (argc == 2) { PortNum = OpenDevice(argv[1]); } else { PortNum = OpenDevice("/dev/ttyS0"); } GenTraffic(PortNum); }