// adrport.c - Serial Port Handler // Copyright MMI, MMII by Sisusypro Incorporated // Permission is hereby granted to freely copy, // modify, utilize and distribute this example in // whatever manner you desire without restriction. #include #include #include #include #include #include #include #include #include "adrport.h" static int fd = 0; main(){ char buf[100]; int i; OpenAdrPort("6"); WriteAdrPort("DATETIME?\r\n"); usleep(1000000); ReadAdrPort(buf,100); printf("%s",buf); for(i=0;buf[i];i++) { printf("0x%2.2x\n",buf[i]); } CloseAdrPort(); } // opens the serial port // return code: // > 0 = fd for the port // -1 = open failed int OpenAdrPort(char* sPortNumber) { char sPortName[64]; printf("in OpenAdrPort port#=%s\n", sPortNumber); sprintf(sPortName, "/dev/ttyS%s", sPortNumber); printf("sPortName=%s\n", sPortName); // make sure port is closed CloseAdrPort(fd); fd = open(sPortName, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { printf("open error %d %s\n", errno, strerror(errno)); } else { struct termios my_termios; printf("fd is %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=%08o\n", my_termios.c_cflag); printf("old oflag=%08o\n", my_termios.c_oflag); printf("old iflag=%08o\n", my_termios.c_iflag); printf("old lflag=%08o\n", my_termios.c_lflag); printf("old line=%03o\n", my_termios.c_line); */ tcflush(fd, TCIFLUSH); tcflush(fd, TCOFLUSH); my_termios.c_cflag = CS7 | CREAD | CLOCAL | HUPCL| PARENB | PARODD; // my_termios.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); my_termios.c_iflag &= ~ICRNL; my_termios.c_lflag = 0; cfsetospeed(&my_termios, B9600); tcsetattr(fd, TCSANOW, &my_termios); /* printf("new cflag=%08o\n", my_termios.c_cflag); printf("new oflag=%08o\n", my_termios.c_oflag); printf("new iflag=%08o\n", my_termios.c_iflag); printf("new lflag=%08o\n", my_termios.c_lflag); printf("new line=%03o\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; int i; 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 0) { close(fd); } // end if } // end CloseAdrPort