#include #include #include #include #include #include #define BAUDRATE B38400 #define MODEMDEVICE "/dev/ttyS1" #define _POSIX_SOURCE 1 //POSIX compliant source #define FALSE 0 #define TRUE 1 volatile int STOP=FALSE; void signal_handler_IO (int status); //definition of signal handler int wait_flag=TRUE; //TRUE while no signal received char devicename[80]; long Baud_Rate = 38400; // default Baud Rate (110 through 38400) long BAUD; // derived baud rate from command line long DATABITS; long STOPBITS; long PARITYON; long PARITY; int Data_Bits = 8; // Number of data bits int Stop_Bits = 1; // Number of stop bits int Parity = 0; // Parity as follows: // 00 = NONE, 01 = Odd, 02 = Even, 03 = Mark, 04 = Space int Format = 4; FILE *input; FILE *output; int status; main(int Parm_Count, char *Parms[]) { char version[80] = " POSIX compliant Communications test program version 1.00 4-25-1999\r\n"; char version1[80] = " Copyright(C) Mark Zehner/Peter Baumann 1999\r\n"; char version2[80] = " This code is based on a DOS based test program by Mark Zehner and a Serial\r\n"; char version3[80] = " Programming POSIX howto by Peter Baumann, integrated by Mark Zehner\r\n"; char version4[80] = " This program allows you to send characters out the specified port by typing\r\n"; char version5[80] = " on the keyboard. Characters typed will be echoed to the console, and \r\n"; char version6[80] = " characters received will be echoed to the console.\r\n"; char version7[80] = " The setup parameters for the device name, receive data format, baud rate\r\n"; char version8[80] = " and other serial port parameters must be entered on the command line \r\n"; char version9[80] = " To see how to do this, just type the name of this program. \r\n"; char version10[80] = " This program is free software; you can redistribute it and/or modify it\r\n"; char version11[80] = " under the terms of the GNU General Public License as published by the \r\n"; char version12[80] = " Free Software Foundation, version 2.\r\n"; char version13[80] = " This program comes with ABSOLUTELY NO WARRANTY.\r\n"; char instr[100] ="\r\nOn the command you must include six items in the following order, they are:\r\n"; char instr1[80] =" 1. The device name Ex: ttyS0 for com1, ttyS1 for com2, etc\r\n"; char instr2[80] =" 2. Baud Rate Ex: 38400 \r\n"; char instr3[80] =" 3. Number of Data Bits Ex: 8 \r\n"; char instr4[80] =" 4. Number of Stop Bits Ex: 0 or 1\r\n"; char instr5[80] =" 5. Parity Ex: 0=none, 1=odd, 2=even\r\n"; char instr6[80] =" 6. Format of data received: 1=hex, 2=dec, 3=hex/asc, 4=dec/asc, 5=asc\r\n"; char instr7[80] =" Example command line: com ttyS0 38400 8 0 0 4 \r\n"; char Param_strings[7][80]; char message[90]; int fd, tty, c, res, i, error; char In1, Key; struct termios oldtio, newtio; //place for old and new port settings for serial port struct termios oldkey, newkey; //place tor old and new port settings for keyboard teletype struct sigaction saio; //definition of signal action char buf[255]; //buffer for where data is put input = fopen("/dev/tty", "r"); //open the terminal keyboard output = fopen("/dev/tty", "w"); //open the terminal screen if (!input || !output) { fprintf(stderr, "Unable to open /dev/tty\n"); exit(1); } error=0; fputs(version,output); //display the program introduction fputs(version1,output); fputs(version2,output); fputs(version3,output); fputs(version4,output); fputs(version5,output); fputs(version6,output); fputs(version7,output); fputs(version8,output); fputs(version9,output); fputs(version10,output); fputs(version11,output); fputs(version12,output); fputs(version13,output); //read the parameters from the command line if (Parm_Count==7) //if there are the right number of parameters on the command line { for (i=1; i0) { for (i=0; i125)) { sprintf(message,"%x",In1); fputs(message,output); } else fputc ((int) In1, output); break; case 4: //decimal and asc default: if ((In1<32) || (In1>125)) { sprintf(message,"%d",In1); fputs(message,output); } else fputc ((int) In1, output); break; case 5: //asc fputc ((int) In1, output); break; } //end of switch format } //end of for all chars in string } //end if res>0 // buf[res]=0; // printf(":%s:%d\n", buf, res); // if (res==1) STOP=TRUE; /* stop loop if only a CR was input */ wait_flag = TRUE; /* wait for new input */ } //end if wait flag == FALSE } //while stop==FALSE // restore old port settings tcsetattr(fd,TCSANOW,&oldtio); tcsetattr(tty,TCSANOW,&oldkey); close(tty); close(fd); //close the com port } //end if command line entrys were correct else //give instructions on how to use the command line { fputs(instr,output); fputs(instr1,output); fputs(instr2,output); fputs(instr3,output); fputs(instr4,output); fputs(instr5,output); fputs(instr6,output); fputs(instr7,output); } fclose(input); fclose(output); } //end of main /*************************************************************************** * signal handler. sets wait_flag to FALSE, to indicate above loop that * * characters have been received. * ***************************************************************************/ void signal_handler_IO (int status) { // printf("received SIGIO signal.\n"); wait_flag = FALSE; }