📄 sendchar.c
字号:
#include "cgic.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#include <termios.h>#include <errno.h>#include <time.h>#define TRUE 1 #define FALSE 0/*定义波特率的合法取值*/static int speed_arr[]={ B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, };/*定义对应的波特率数值*/static int name_arr[]={ 115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300,};int init_serial(char* dev) { int fd=open(dev,O_RDWR); if(fd<0) { printf("cannot open %s\n",dev); exit(-1); } else printf("%s open successfully.\n",dev); return fd;}void set_speed(int fd,int speed) { int i,status; struct termios opt; tcgetattr(fd,&opt); for(i=0;i<sizeof(speed_arr)/sizeof(int);i++) { if(speed == name_arr[i]) { tcflush(fd,TCIOFLUSH);/*TCIOFLUSH flushes both data received but not read, and data written but not transmitted.*/ cfsetispeed(&opt,speed_arr[i]);/*设置串口输入速度*/ cfsetospeed(&opt,speed_arr[i]);/*设置串口输出速度*/ status = tcsetattr(fd,TCSANOW,&opt); /*设置设备工作模式*/ if(status != 0) perror("tcsetattr fd error!"); break; } tcflush(fd,TCIOFLUSH); }}int set_parity(int fd,int databits,int stopbits,int parity) { /** *@brief 设置串口数据位,停止位和效验位 *@param fd 类型 int 打开的串口文件句柄 *@param databits 类型 int 数据位 取值 为 7 或者8 *@param stopbits 类型 int 停止位 取值为 1 或者2 *@param parity 类型 int 效验类型 取值为N,E,O,,S */ struct termios opt; if(tcgetattr(fd,&opt) != 0) { /*得到设备当前模式*/ perror("setup serial..."); return 0; } /* 设置数据位数前,首先使用‘CSIZE’屏蔽数据位数 */ opt.c_cflag &= ~CSIZE; /*CSIZE Character size mask. Values are CS5, CS6, CS7, or CS8.*/ switch(databits) { /*设置数据位数*/ case 7: opt.c_cflag |= CS7; /*设置数据位数=7*/ break; case 8: opt.c_cflag |= CS8; /*设置数据位数=8*/ break; default: printf("unsupport data size.\n"); return 0; } switch(parity) { /*设置串口校验位*/ case 'n': case 'N': opt.c_cflag &= ~PARENB; /*set the PARENB bit to zero-------disable parity checked*/ opt.c_iflag &= ~INPCK; /*set the INPCK bit to zero--------INPCK means inparitycheck(not paritychecked)*/ break; case 'o': case 'O': opt.c_cflag |= (PARENB|PARODD); opt.c_iflag |= INPCK; break; case 'e': case 'E': opt.c_cflag |= PARENB; opt.c_cflag &= ~PARODD; opt.c_iflag |= INPCK; break; case 's': case 'S': opt.c_cflag &= ~PARENB; opt.c_cflag &= ~CSTOPB; /*CSTOPB---Set two stop bits, rather than one.*/ break; default: printf("unsupported parity.\n"); return 0; } switch(stopbits) { /*设置停止位*/ case 1: opt.c_cflag &= ~CSTOPB; break; case 2: opt.c_cflag &= CSTOPB; break; default: printf("unsupported stopbits.\n"); return 0; } if(parity !='n') opt.c_iflag |= INPCK; tcflush(fd,TCIFLUSH); opt.c_cc[VTIME] = 150; opt.c_cc[VMIN] = 0; if(tcsetattr(fd,TCSANOW,&opt) != 0) { perror("\n"); return 0; } return 1;}void send(char * sendstr){ int fd,len; char ch; char *dev ="/dev/ttyS1"; len = strlen(sendstr); fd = init_serial(dev); if (fd>0) set_speed(fd,115200); else { printf("Can't Open Serial Port!\n"); exit(0); } if (set_parity(fd,8,1,'N')== FALSE) { printf("Set Parity Error\n"); exit(1); } printf("successfully set\n"); //ch = 'a'; //while(1) { //ch=getchar(); cgiHtmlEscape(sendstr); write(fd, sendstr,len+1); //write(fd, sendstr,len+1); //read(fd,&ch,1); //printf("%d:\t %d",i++,(int)ch); //putchar(ch); //} close(fd); }void HandleSubmit(){ char tx[100]; cgiFormStringNoNewlines("sendchar",tx,100); cgiHtmlEscape (tx); send(tx);}int cgiMain(){ cgiHeaderContentType("text/html"); fprintf(cgiOut, "<HTML><HEAD>\n"); fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n"); if(cgiFormSubmitClicked("send") == cgiFormSuccess) { HandleSubmit(); fprintf(cgiOut, "<hr>\n"); } else { printf("no submit clicked!"); } fprintf(cgiOut,"Send Success!"); fprintf(cgiOut, "</BODY></HTML>\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -