📄 ttytest.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "rs232.h"#include <assert.h>#include <sys/time.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>//#define DEBUG#define RS485SENTBEGIN 1
#define RS485SENTOVER 2struct PortInfo ports[MAX_PORTS];struct rs485de_ctl
{
long BaudRate;
int parity;
int dataBits;
int stopBits;
long count;
};/*** Function: OpenCom**** Description:** Opens a serial port with default parameters**** Arguments:** portNo - handle used for further access** deviceName - the name of the device to open**** Returns:** -1 on failure*/int OpenCom(int portNo, const char deviceName[],long baudRate){ return OpenComConfig(portNo, deviceName, baudRate, 1, 8, 1, 0, 0);}/**/long GetBaudRate(long baudRate){ long BaudR; switch(baudRate) { case 115200: BaudR=B115200; break; case 57600: BaudR=B57600; break; case 19200: BaudR=B19200; break; case 38400: BaudR=B38400; break; case 9600: BaudR=B9600; break; default: BaudR=B0; } return BaudR;}/*** Function: OpenComConfig**** Description:** Opens a serial port with the specified parameters**** Arguments:** portNo - handle used for further access** deviceName - the name of the device to open** baudRate - rate to open (57600 for example)** parity - 0 for no parity** dataBits - 7 or 8** stopBits - 1 or 2** iqSize - ignored** oqSize - ignored**** Returns:** -1 on failure**** Limitations:** parity, stopBits, iqSize, and oqSize are ignored*/int OpenComConfig(int port, const char deviceName[], long baudRate, int parity, int dataBits, int stopBits, int iqSize, int oqSize){ struct termios newtio; long BaudR; ports[port].busy = 1; strcpy(ports[port].name,deviceName); if ((ports[port].handle = open(deviceName, O_RDWR, 0666)) == -1) { perror("open"); // assert(0); } /* set the port to raw I/O */ newtio.c_cflag = CS8 | CLOCAL | CREAD ; newtio.c_cflag &= ~PARENB; newtio.c_cflag &= ~CSTOPB; /* 1 stop bit */ //newtio.c_iflag &= ~INPCK; newtio.c_iflag = IGNPAR; newtio.c_iflag &=~(ICANON|ECHO|ECHOE|ISIG) ; //newtio.c_iflag &=(IXON|IXOFF|IXANY); newtio.c_oflag = 0; //newtio.c_oflag &= ~OPOST; newtio.c_lflag = 0; newtio.c_cc[VINTR] = 0; newtio.c_cc[VQUIT] = 0; newtio.c_cc[VERASE] = 0; newtio.c_cc[VKILL] = 0; newtio.c_cc[VTIME] = 1; newtio.c_cc[VMIN] = 18; newtio.c_cc[VSWTC] = 0; newtio.c_cc[VSTART] = 0; newtio.c_cc[VSTOP] = 0; newtio.c_cc[VSUSP] = 0; newtio.c_cc[VEOL] = 0; newtio.c_cc[VREPRINT] = 0; newtio.c_cc[VDISCARD] = 0; newtio.c_cc[VWERASE] = 0; newtio.c_cc[VLNEXT] = 0; newtio.c_cc[VEOL2] = 0; cfsetospeed(&newtio, GetBaudRate(baudRate)); cfsetispeed(&newtio, GetBaudRate(baudRate)); tcsetattr(ports[port].handle, TCSANOW, &newtio); return 0;}/*** Function: CloseCom**** Description:** Closes a previously opened port**** Arguments:** The port handle to close**** Returns:** -1 on failure*/int CloseCom(int portNo){ if (ports[portNo].busy) { close(ports[portNo].handle); ports[portNo].busy = 0; return 0; } else { return -1; }}/*** Function: ComRd**** Description:** Reads the specified number of bytes from the port.** Returns when these bytes have been read, or timeout occurs.**** Arguments:** portNo - the handle** buf - where to store the data** maxCnt - the maximum number of bytes to read**** Returns:** The actual number of bytes read*/int ComRd(int portNo, char buf[], int maxCnt,int Timeout){ int actualRead = 0; fd_set rfds; struct timeval tv; int retval; if (!ports[portNo].busy) { assert(0); } /* /* camp on the port until data appears or 5 seconds have passed */ FD_ZERO(&rfds); FD_SET(ports[portNo].handle, &rfds); tv.tv_sec = Timeout/1000; tv.tv_usec = (Timeout%1000)*1000; retval = select(16, &rfds, NULL, NULL, &tv); if (retval) { actualRead = read(ports[portNo].handle, buf, maxCnt); } #ifdef DEBUG if(actualRead>0) { unsigned int i; for (i = 0; i < actualRead; ++i) { if ((buf[i] > 0x20) && (buf[i] < 0x7f)) { printf("<'%c'", buf[i]); } else { printf("<%02X", buf[i]); } } printf("\n"); } fflush(stdout);#endif /* DEBUG */ return actualRead;}/*** Function: ComWrt**** Description:** Writes out the specified bytes to the port**** Arguments:** portNo - the handle of the port** buf - the bytes to write** maxCnt - how many to write**** Returns:** The actual number of bytes written*/int ComWrt(int portNo, const char *buf, int maxCnt){ int written; if (!ports[portNo].busy) { assert(0); } /*#ifdef DEBUG { int i; for (i = 0; i < maxCnt; ++i) { if ((buf[i] > 0x20) && (buf[i] < 0x7f)) { printf(">'%c'", buf[i]); } else { printf(">%02X", buf[i]); } } printf("\n"); } fflush(stdout);#endif /* DEBUG */ written = write(ports[portNo].handle, buf, maxCnt); return written;}main(int argc, unsigned long *argv[]){ int ret,portno,nWritten,nRead; int i; unsigned long ba; char buf[256]; //portno=2; int fd_de,val; struct rs485de_ctl de_example; portno=2; //printf("argc is %d \n",argc); //printf("argv[0] is %s, argv[1] is %s \n",argv[0],argv[1]); if(argc==1) ba=9600; else if(argc>=2) { ba=atoi(argv[1]); } else; while(1) { printf("\n1:SEND ,2:RECEIVE,0:quit :"); scanf("%d",&val); if(val==1) { for(i=0;i<256;i++) buf[i]=i; ret=OpenCom(portno,"/dev/ttyS1",ba); if(ret==-1) { perror("The /dev/ttyS1 open error."); exit(1); } if((fd_de=open("/dev/rs485driver",O_RDONLY | O_NONBLOCK))<0)
{
perror("can not open device\n");
exit(1);
} memset(&de_example,0,sizeof(struct rs485de_ctl)); de_example.BaudRate=ba; de_example.parity=0; de_example.dataBits=8; de_example.stopBits=1; de_example.count=16; //ioctl(fd_de,RS485SENTBEGIN,NULL); for(i=0;i<4*4;i++) { ioctl(fd_de,RS485SENTBEGIN,NULL); ComWrt(portno,buf,16); ioctl(fd_de,RS485SENTOVER,&de_example); } close(fd_de); CloseCom(portno); } else if(val==2) { ret=OpenCom(portno,"/dev/ttyS1",ba); if(ret==-1) { perror("The /dev/ttyS1 open error."); exit(1); } memset(buf,0,256); fflush(stdout); nRead=ComRd(portno,buf,256,5000); if(nRead>0) { printf("Read %d data\n",nRead); for(i=0;i<nRead;i++) printf("0x%x ",buf[i]); printf("\n"); } else printf("TimeOut\n"); CloseCom(portno); } else { exit(0); } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -