在 Linux 下,移植一個 boa 伺服器,編寫相應 cgi,在網頁上動态重新整理序列槽資料(使用虛拟序列槽),使用到序列槽程式設計,資料庫程式設計及簡單前端知識。
該測控系統測量資料通過虛拟序列槽傳遞(由于條件限制未使用傳感器),目前隻有測的功能,未來會進一步改進。
以下是具體内容
讀取序列槽資料程式:
這裡用到序列槽程式設計,寫一個程式讀取序列槽資料,然後将讀取資料插入資料庫,有用到資料庫程式設計的知識。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sqlite3.h>
#define BUFFER_SIZE 200
int open_port()
{
int fd;
fd = open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY);//打開序列槽
if(fd < )
{
perror("open serial port");
return (-);
}
if(fcntl(fd,F_SETFL,) < )//回複序列槽為阻塞狀态
{
perror("fcntl F_SETFL\n");
}
if(isatty(fd) == )//測試打開的檔案是否為終端裝置
{
perror("This is not a terminal device");
}
return fd;
}
int set_com_config(int fd)
{
struct termios new_cfg,old_cfg;
int speed;
if(tcgetattr(fd,&old_cfg) != )//保留原序列槽配置
{
perror("tcgetattr");
return -;
}
new_cfg = old_cfg;
cfmakeraw(&new_cfg);//将終端設定為原始模式
new_cfg.c_cflag &= ~CSIZE;//用資料位掩碼清空資料位設定
speed = B115200;
cfsetispeed(&new_cfg,speed);//設定波特率
cfsetospeed(&new_cfg,speed);
new_cfg.c_cflag |= CS8;//重新設定字元大小
new_cfg.c_cflag &= ~PARENB;//設定奇偶校驗位
new_cfg.c_iflag &= ~INPCK;
new_cfg.c_cflag &= ~CSTOPB;//設定停止位
new_cfg.c_cc[VTIME] = ;//設定最少字元和等待時間
new_cfg.c_cc[VMIN] = ;
tcflush(fd,TCIFLUSH);//清空緩沖區
if((tcsetattr(fd,TCSANOW,&new_cfg)) != )//激活配置
{
perror("tcsetattr");
return -;
}
return ;
}
int main(void)
{
int fd;
int nread;
char buff[BUFFER_SIZE];
int rc,i;
sqlite3 *db = NULL;
sqlite3_stmt *stmt;
char *errmsg = NULL;
char sql[];
rc = sqlite3_open("ph.db",&db);//打開資料庫
if((fd = open_port()) < )//打開序列槽
{
perror("open_port");
return ;
}
if(set_com_config(fd) < )//序列槽設定
{
perror("set_com_config");
return ;
}
while()
{
printf("receive is \n");
memset(buff,,BUFFER_SIZE);
read(fd,buff,BUFFER_SIZE);
printf("%s\n",buff);
sprintf(sql,"update PHdata set id = %c,Pid = %c,Pph = %c,we = %c",buff[],buff[],buff[],buff[]);
sqlite3_exec(db,sql,NULL,NULL,errmsg);//向資料庫插入資料
printf("%s\n",sql);
}
sqlite3_close(db);
close(fd);
return ;
}
簡易CGI:由于前端隻是還在起步階段,是以這個網頁界面做的比較簡單,未來會進一步改進。
代碼:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int main()
{
sqlite3 *pdb=NULL;
sqlite3_stmt *stmt;
char *szErrMsg=;
int rc,i;
rc=sqlite3_open("ph.db",&pdb);
if(rc)
{
fprintf(stderr,"can't open database: %s",sqlite3_errmsg(pdb));
sqlite3_close(pdb);
return -;
}
printf("Content-type:text/html\n\n");
printf("<html>\n<head>\n<Meta http-equiv=\"Refresh\" Content=\"1;\"><title>test</title></head>\n<body>\n");
printf("<h1 align=\"center\">PHDATA</h1>\n");
printf("<table 1\" align=\"center\">\n");
printf("<tr>\n");
printf("<th>id</th><th>Pid</th><th>Pph</th><th>we</th>\n");
printf("</tr>\n");
char sql[];
//char *sql="select *from wsd where id=(select max(id)from wsd where dnum=1)";
// int j=1;
// for(;j<=3;j++)
// {
sprintf(sql,"select * from PHdata;");
sqlite3_prepare(pdb,sql,-,&stmt,);
//sqlite3_bind_int(stmt,kk,i);
//sqlite3_bind_int(stmt,1,i);
/*
rc=sqlite3_column_count(stmt);
for(i=0;i<rc;i++)
{
printf("%s\t",sqlite3_column_name(stmt,i));
}
printf("\n");
*/ printf("<tr>\n");
while(sqlite3_step(stmt)==SQLITE_ROW)
{
printf("<td>%d</td>",sqlite3_column_int(stmt,));
printf("<td>%d</td>",sqlite3_column_int(stmt,));
printf("<td>%.1f</td>",sqlite3_column_double(stmt,));
printf("<td>%s</td>\n",sqlite3_column_text(stmt,));
/*
for(i=0;i<rc-1;i++)
{
printf("%d\t",sqlite3_column_int(stmt,i));
}
printf("%s\n",sqlite3_column_text(stmt,i));
//sqlite3_reset(stmt);
*/
}
printf("</tr>\n");
// }
printf("</table>\n</body>\n</html>");
sqlite3_finalize(stmt);
sqlite3_close(pdb);
return ;
}
通過boa伺服器對資料庫内容定時重新整理,資料庫内容由序列槽收到資料定時重新整理。