************************************************** * adrport.c ************************************************** #include #include #include #include #include #include #include #include #include "adrport.h" static int fd = 0; // opens the serial port // return code: // > 0 = fd for the port // -1 = open failed int OpenAdrPort(char* sPortNumber) { char sPortName[64]; system("clear"); printf("***************************************************************************\n"); printf("* *\n"); printf("* Programa para leer el puerto Serial - Por: Coop. Linux Solutions, RL *\n"); printf("* *\n"); printf("***************************************************************************\n"); printf("En Puerto (OpenAdrPort)#=%s\n", sPortNumber); sprintf(sPortName, "/dev/ttyS%s", sPortNumber); printf("Nombre del Puerto= %s\n", sPortName); // make sure port is closed CloseAdrPort(fd); fd = open(sPortName, O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { printf("Error al abrir el puerto %d %s\n", errno, strerror(errno)); } else { struct termios my_termios; printf("fd es %d\n", fd); tcgetattr(fd, &my_termios); // NOTE: you may want to save the port attributes // here so that you can restore them later //printf("old cflag=%08x\n", my_termios.c_cflag); //printf("old oflag=%08x\n", my_termios.c_oflag); //printf("old iflag=%08x\n", my_termios.c_iflag); //printf("old lflag=%08x\n", my_termios.c_lflag); //printf("old line=%02x\n", my_termios.c_line); tcflush(fd, TCIFLUSH); my_termios.c_cflag = B9600 | CS8 |CREAD | CLOCAL | HUPCL; cfsetospeed(&my_termios, B9600); tcsetattr(fd, TCSANOW, &my_termios); //printf("new cflag=%08x\n", my_termios.c_cflag); //printf("new oflag=%08x\n", my_termios.c_oflag); //printf("new iflag=%08x\n", my_termios.c_iflag); //printf("new lflag=%08x\n", my_termios.c_lflag); //printf("new line=%02x\n", my_termios.c_line); } // end if return fd; } // end OpenAdrPort // writes zero terminated string to the serial port // return code: // >= 0 = number of characters written // -1 = write failed int WriteAdrPort(char* psOutput) { int iOut; if (fd < 1) { printf(" port is not open\n"); return -1; } // end if iOut = write(fd, psOutput, strlen(psOutput)); if (iOut < 0) { printf("write error %d %s\n", errno, strerror(errno)); } else { printf("wrote %d chars: %s\n", iOut, psOutput); } // end if return iOut; } // end WriteAdrPort // read string from the serial port // return code: // >= 0 = number of characters read // -1 = read failed int ReadAdrPort(char* psResponse, int iMax) { int iIn; printf("in ReadAdrPort iMax=%d\n", iMax); if (fd < 1) { printf(" port is not open\n"); return -1; } // end if strncpy (psResponse, "N/A", iMax<4?iMax:4); iIn = read(fd, psResponse, iMax-1); if (iIn < 0) { if (errno == EAGAIN) { return 0; // assume that command generated no response } else { printf("read error %d %s\n", errno, strerror(errno)); } // end if } else { psResponse[iIn #include #include #include #include "adrport.h" // this is the mainline thingee int main(int argc, char *argv[]) { char sCmd[254]; char sResult[254]; if (argc < 2 || argc > 2) { printf("El adrserial requiere 1 parametro que especifique el puerto serial\n"); printf(" ie. use 'adrserial 0' para conectarse a /dev/ttyS0\n"); return 0; } // end if printf("Presione la tecla q para salir.\n\n"); if (OpenAdrPort(argv[1]) < 0) return 0; while (1) { int iSpot; printf("Para salir presione la letra q:"); gets(sCmd); if (sCmd[0] == 'q' || sCmd[0] == 'Q') return 0; iSpot = strlen(sCmd); //sCmd[iSpot] = 0x0d; // stick a after the command //sCmd[iSpot+1] = 0x00; // terminate the string properly //if (WriteAdrPort(sCmd) < 0) return 0; sleep(1); // give the ADR card some time to respond if (ReadAdrPort(sResult,254) > 0) { printf("La respuesta del puerto es %s\n", sResult); } // end if } // end while CloseAdrPort(); } // end main ************************************************************************ ** adrport.h ************************************************************************ // adrport.h // Copyright MMI, MMII by Sisusypro Incorporated int OpenAdrPort (char* sPortNumber); int WriteAdrPort(char* psOutput); int ReadAdrPort(char* psResponse, int iMax); void CloseAdrPort();