这几天在弄V3s boa服务器上运行cgi访问V3s的串口,也就是给串口发消息。在网上查了很多资料,也没有找到合适的,摸索一段时间终于OK了,其实很简单。下面贴出来代码,其实也就是标准的Linux串口打开,配置,发送消息的代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//---- add it for test 20200603—start—
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#define FIFO_NAME “/dev/ttyS2”
int UART_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,char parity)
{
int i;
int status;
int speed_arr[] = {B115200,B19200,B9600,B4800,B2400,B1200,B300};
int name_arr[] = {115200,19200,9600,4800,2400,1200,300};
struct termios opt;
if(tcgetattr(fd,&opt) != 0)
{
perror("Get SerialAttr Error!\n");
return -1;
}
for(i=0;i<sizeof(speed_arr)/sizeof(int); i++)
{
if(speed == name_arr[i])
{
cfsetispeed(&opt,speed_arr[i]);
cfsetospeed(&opt,speed_arr[i]);
}
}
opt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
opt.c_cflag |= CLOCAL | CREAD;
switch(flow_ctrl)
{
case 0 :
opt.c_cflag &= ~CRTSCTS;
break;
case 1 :
opt.c_cflag |= CRTSCTS;
break;
case 2 :
opt.c_cflag |= IXON | IXOFF | IXANY;
break;
default:
return -1;
}
opt.c_cflag &= ~CSIZE;
switch (databits)
{
case 5:
opt.c_cflag |= CS5;
break;
case 6:
opt.c_cflag |= CS6;
break;
case 7:
opt.c_cflag |= CS7;
break;
case 8:
opt.c_cflag |= CS8;
break;
default:
return -1;
}
switch (parity)
{
case 'n':
case 'N':
opt.c_cflag &= ~PARENB;
opt.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
opt.c_cflag |= (PARODD | PARENB);
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;
break;
default:
return -1;
}
switch (stopbits)
{
case 1:
opt.c_cflag &= ~CSTOPB;
break;
case 2:
opt.c_cflag |= CSTOPB;
break;
default:
return -1;
}
opt.c_oflag &= ~OPOST;
opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
opt.c_cc[VTIME] = 0;
opt.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&opt) != 0)
{
perror("Set Serial Error!\n");
return -1;
}
return 0;
}
//----------end---------------------**
char* getcgidata(FILE* fp, char* requestmethod);
int main()
{
int fd;
int w_num;
char *input;
char *req_method;
char name[64];
char pass[64];
int i = 0;
int j = 0;
//---- add it for test 20200603–start----
fd=open(FIFO_NAME,O_RDWR|O_NOCTTY|O_NDELAY);
UART_Set(fd,115200,0,8,1,‘N’);
//------------------end-------------------
// printf(“Content-type: text/plain; charset=iso-8859-1\n\n”);
printf(“Content-type: text/html\n\n”);
printf(“The following is query reuslt:
”);
req_method = getenv(“REQUEST_METHOD”);
input = getcgidata(stdin, req_method);
// 我们获取的input字符串可能像如下的形式
// Username=“admin”&Password=“aaaaa”
// 其中"Username=“和”&Password="都是固定的
// 而"admin"和"aaaaa"都是变化的,也是我们要获取的
// 前面9个字符是UserName=
// 在"UserName="和"&"之间的是我们要取出来的用户名
for ( i = 9; i < (int)strlen(input); i++ )
{
if ( input[i] == '&' )
{
name[j] = '\0';
break;
}
name[j++] = input[i];
}
// 前面9个字符 + "&Password="10个字符 + Username的字符数
// 是我们不要的,故省略掉,不拷贝
for ( i = 19 + strlen(name), j = 0; i < (int)strlen(input); i++ )
{
pass[j++] = input[i];
}
pass[j] = '\0';
printf("Your Username is %s<br>Your Password is %s<br> \n", name, pass);
//---- add it for test 20200603--start---
if (fd)
{
printf("%d\n",fd);
w_num=write(fd,"abcdg\0",6);
printf("%d\n",w_num);
}
//----------end---------------------------
return 0;
}
char* getcgidata(FILE* fp, char* requestmethod)
{
char* input;
int len;
int size = 1024;
int i = 0;
if (!strcmp(requestmethod, "GET"))
{
input = getenv("QUERY_STRING");
return input;
}
else if (!strcmp(requestmethod, "POST"))
{
len = atoi(getenv("CONTENT_LENGTH"));
input = (char*)malloc(sizeof(char)*(size + 1));
if (len == 0)
{
input[0] = '\0';
return input;
}
while(1)
{
input[i] = (char)fgetc(fp);
if (i == size)
{
input[i+1] = '\0';
return input;
}
--len;
if (feof(fp) || (!(len)))
{
i++;
input[i] = '\0';
return input;
}
i++;
}
}
return NULL;
}
该cgi代码是我从网上下载的,还有相关调试的网页没有放置上来,主要是模拟网页上的按键动作,按下后给平台的串口发送一个信息(命令):主要函数如下:
fd=open(FIFO_NAME,O_RDWR|O_NOCTTY|O_NDELAY);//打开串口
UART_Set(fd,115200,0,8,1,‘N’);//设置串口比特率,奇偶校验等参数
//---- add it for test 20200603–start—
if (fd)//打开设备非正常的话,fd的值为-1
{
printf("%d\n",fd);
w_num=write(fd,“abcdg\0”,6);//给串口发送消息
printf("%d\n",w_num);//给串口发送消息,w_num发送成功的话,会返回发给串口的字节数。
}
//----------end---------------------------
以上简单介绍网页发送给串口的范例。其实网页访问串口,在linux上,都是访问一个“文件”,打开,写入。