📄 clientpos.c
字号:
// 对串口进行操作的程序#include <stdio.h> /* Standard input/output definitions */#include <string.h> /* String function definitions */#include <unistd.h> /* UNIX standard function definitions */#include <fcntl.h> /* File control definitions */#include <errno.h> /* Error number definitions */#include <termio.h> /* POSIX terminal control definitions */#include <sys/time.h>#include <sys/select.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <time.h>#include <math.h>#include <stdlib.h>#include <sys/signal.h>#define ETX 0x03#define STX 0x02#define PROMPT "你想终止程序吗?\n"
char *prompt = PROMPT;
long getCurrTime(){ long currTime; time( &currTime ); //printf( "Time in seconds since UTC 1/1/70:\t%ld\n", currTime ); return currTime;}//读二进制文件
int readfile(char *filename,unsigned char *buf){
FILE *stream;
int ch ,len = 0;
if( (stream = fopen( filename,"rb")) == NULL ){
printf( "文件打开失败!\n" );
return -1;
}
while ((ch = fgetc(stream)) != EOF){
buf[len++] = (unsigned char)ch;
printf( "[%02x]",ch );
}
printf( "\n");
fclose( stream );
return len;
}
//write to serial portvoid writePort(int fd,unsigned char *buffer,int len){ int k; printf("Writing data:\n"); for ( k = 0; k < len; k++) { printf("[%02x]",buffer[k]); while (write(fd,buffer + k,1) != 1 ); }}//read data from serial port and check data int readPort(int fd,unsigned char *buffer,int *length){ int i = 0, n, len, flag, timeout = 0; unsigned char lrc; //unsigned char lrc; long currTime1,currTime2; printf("read begin .....\n"); if ( read(fd, buffer, 1) > 0 ) { if (buffer[0] == STX){ printf("[%02x]", buffer[0] );// printf("OK Head!\n"); }else{ printf("Error Head!\n"); return 0; } }else{ printf("No data!\n"); return 0; } currTime1=getCurrTime(); for ( i = 0, n = 1, flag = 0, len = 0, lrc = '\x00'; ; ) { if (read(fd, &buffer[n], 1) > 0 ) { printf("[%02x]", buffer[n] ); fflush(stdout); if ( flag < 2 ) { len = len*100+(((buffer[n] & 0xf0) >> 4) * 10 +(buffer[n] & 0x0f)); lrc ^= buffer[n]; flag++; n++; }else if ( flag == 2 && i++ < len ){ *length=len+5; lrc ^= buffer[n]; n++; }else if ( flag == 2 && i >= len ){ if ( buffer[n] == ETX ){ lrc ^= buffer[n]; flag++; n++; } else { printf("\nETX not received\n" ); return( -1 ); } }else if ( flag == 3 ){ if ( lrc != buffer[n] ) { printf("2 resp LRC: %2x, cacl LRC: %02x\n", buffer[n], lrc ); return( -2 ); } else { return( len ); } } }else { printf("\nNot data\n" ); currTime2 = getCurrTime(); printf("\ntime=%d-%d\n",currTime2,currTime1); if( (currTime2-currTime1)>timeout){ printf("\nTime out=%d\n", currTime2-currTime1); printf("\nTime out\n" ); return( 0 ); } } }}int setAttr(int fdread){ struct termio comtty; if(ioctl(fdread,TCGETA,&comtty)){ printf("\n[ERROR]: Get tty error\n" ); return (-1); } comtty.c_iflag = 1; comtty.c_oflag = 0; comtty.c_cflag =(B9600|CS8|CREAD|HUPCL|CLOCAL); comtty.c_lflag = 0; comtty.c_cc[VMIN]=0; comtty.c_cc[VTIME]=1; if(ioctl(fdread,TCSETA,&comtty)==-1){ printf("\n[ERROR]: Set tty error\n" ); return (-1); }}int setNowait(int fd){ struct termio term_attr; if(fd<0) return -1; if(ioctl(fd,TCGETA,&term_attr)<0) return -1; term_attr.c_cc[VMIN]=0; term_attr.c_cc[VTIME]=1; if(ioctl(fd,TCSETAW,&term_attr)<0) return -1; return 0;}// 处理信号void ctrl_c_op(int signo){
char c;
write(STDERR_FILENO,prompt,strlen(prompt));
fflush(stdin);
c = getchar();
if (c == 'y'|| c == 'Y')
exit(0);
}
//主程序int main(int argc,char *argv[]){ char *pCOM1,*pCOM2;//send use param 发送 unsigned char buffer[1024]; int fdr,fdw,ret,i=0,j;//rcv use param 接收 int rcvLen[3] = {0,0,0},packlen,starting=1; unsigned char rcvPack[3][1024],tpdu[6];// 设置退出信号 struct sigaction act;
act.sa_handler = ctrl_c_op;
sigemptyset(&act.sa_mask); // 将信号集合设置为空
act.sa_flags = 0;
if(sigaction(SIGQUIT,&act,NULL) < 0){ // CTRL-\ 退出当前进程,并生成主存储文件
fprintf(stderr,"Install Signal Action Error:%s\n\a",strerror(errno));
exit(1);
}
// 得到输入参数(串口设备名) if(argc>2){ pCOM1=argv[1]; pCOM2=argv[2]; }else{ printf("\n请指明串口设备名! eg:\nclientpos /dev/tty2a /dev/tty1a\n");//读2,写1 exit(0); }//open port for read and write 打开端口读写 printf("打开读写端口...\n"); if( (fdr=open(pCOM1, O_RDONLY))<0 || (fdw=open(pCOM2,O_WRONLY))<0){ printf("\n\r[ERROR]: Open serial port for read or write %s failure....\n" ,argv[1]); return (-1); } printf("端口已成功打开...\n"); setAttr(fdr); setNowait(fdr); setAttr(fdw); setNowait(fdw); printf("打开文件读数据...\n"); rcvLen[0] = readfile("0400.log",rcvPack[0]); rcvLen[1] = readfile("IN98SEND",rcvPack[1]); rcvLen[2] = readfile("IN99SEND",rcvPack[2]); while(1){ if(i>=3) i=0; printf("\n开始写串口:%s........\n",pCOM2); writePort(fdw,rcvPack[i],rcvLen[i]); //向串口写数据 printf("写完串口!\n"); i++; for (j=0;j<100000000;j++) ;// printf("开始读串口:%s...\n",pCOM1);// ret=readPort(fdr,buffer,&packlen); //从串口读数据// printf("\nlenth:%i\n",ret);// if(ret>0)// printf("\nlenth:%i\n",ret);// printf("\n读完串口!\n"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -