主要用到的知识点,http协议,modbus协议,以及进程间通信,消息队列,共享内存等
框架
数据采集
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <modbus.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <sys/msg.h>#define N 1024 //共享内存空间
int ret; //函数返回值
uint16_t buf[32]; //读取保持寄存器值
uint8_t set[32]; //设置线圈寄存器
modbus_t *sensor; //传感器实例
modbus_t *hardware; //硬件实例
// char c[2]; //00:led关 01:led 开 10: 蜂鸣器关 11:蜂鸣器开
//消息队列结构体
typedef struct msgbuf
{
long mytype; //消息类型
char buf[32]; //消息数据内容
} msg_t;
int msgid;
void *mythread(void *arg)
{
while (1)
{
//读取消息队列
msg_t msg_read; //读取到的消息
msgrcv(msgid, &msg_read, sizeof(msg_read) - sizeof(long), 0, 0); //接收队列中的第一个消息
printf("%s\n", msg_read.buf);
if (msg_read.buf[0] == '0' && msg_read.buf[1] == '0')
{
ret = modbus_write_bit(hardware, 0, 0); //关闭LED
break;
}
else if (msg_read.buf[0] == '0' && msg_read.buf[1] == '1')
{
ret = modbus_write_bit(hardware, 0, 1); //打开LED
}
else if (msg_read.buf[0] == '1' && msg_read.buf[1] == '0')
{
ret = modbus_write_bit(hardware, 1, 0); //关闭蜂鸣器
}
else if (msg_read.buf[0] == '1' && msg_read.buf[1] == '1')
{
ret = modbus_write_bit(hardware, 1, 1); //打开蜂鸣器
}
}
// pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
key_t key = ftok("./a.txt", 'a'); //产生一个key值
int shmid = shmget(key, N, IPC_CREAT | IPC_EXCL | 0777); //创建或打开共享内存
if (shmid < 0)
{
if (errno == EEXIST)
{
printf("shmget eexist\n"); //已创建
shmid = shmget(key, N, 0777);
}
else
{
perror("shmget err.");
return -1;
}
}
//映射共享内存
char *p = (char *)shmat(shmid, NULL, 0666);
if (p == (void *)-1)
{
perror("shmat err.");
return -1;
}
//创建key值
key_t key2 = ftok("./a.txt", 'b');
//创建或打开消息队列
msgid = msgget(key2, IPC_CREAT | IPC_EXCL | 0666);
if (msgid < 0)
{
if (errno == EEXIST)
{
printf("msgget eexist\n"); //已创建
msgid = msgget(key2, 0666);
}
else
{
perror("msgget err.");
return -1;
}
}
// 1.创建实例 modbus_new_tcp,端口号字符型转整型
//设置传感器读取
sensor = modbus_new_tcp(argv[1], atoi(argv[2]));
// 2.设置从机id modbus_set_slave,连接
ret = modbus_set_slave(sensor, 1);
if (ret < 0)
{
printf("set err\n");
}
// 3.建立连接 modbus_connect
ret = modbus_connect(sensor);
if (ret < 0)
{
printf("connect err.\n");
}
//硬件设置
hardware = modbus_new_tcp(argv[1], atoi(argv[2]));
ret = modbus_set_slave(hardware, 2);
if (ret < 0)
{
printf("set err\n");
}
ret = modbus_connect(hardware);
if (ret < 0)
{
printf("connect err.\n");
}
char data[128];
// 4.寄存器操作
pthread_t tid; //创建一个线程
pthread_create(&tid, NULL, mythread, NULL);
pthread_detach(tid);
while (1)
{
sleep(1);
//4.从0开始读四个寄存器值. 0:光线传感器 1:加速度传感器 X 2:加速度传感器 Y 3:加速度传感器 Z
ret = modbus_read_registers(sensor, 0, 4, buf);
//将从设备读取的内容定向输出到共享内存中
sprintf(data, "%d\n%d\n%d\n%d\n", buf[0], buf[1], buf[2], buf[3]);
strcpy(p, data);
printf("%s", p);
putchar(10);
}
//取消映射
shmdt(p);
//删除映射
shmctl(shmid, IPC_RMID, NULL);
// 5.关闭套接字 modbus_close,先关闭套接字,再释放实例
modbus_close(sensor);
modbus_close(hardware);
// 6.释放实例 modbus_free
modbus_free(sensor);
modbus_free(hardware);
return 0;
}
服务器端对网页信号进行处理
//处理获取设备数据请求static int handle_get(int sock, const char *input){ key_t key = ftok("./a.txt", 'a'); //产生一个key值int shmid = shmget(key, N, IPC_CREAT | IPC_EXCL | 0777); //创建或打开共享内存if (shmid < 0){if (errno == EEXIST){printf("shmget eexist\n"); //已创建 shmid = shmget(key, N, 0777);}else{perror("shmget err.");return -1;}}//映射共享内存char *p = (char *)shmat(shmid, NULL, 0666);if (p == (void *)-1){perror("shmat err.");return -1;}char reply_buf[HTML_SIZE] = {0};send(sock, p, strlen(p), 0);return 0;}//处理控制设备数据请求static int handle_post(int sock, const char *input){ msg_t ctl;//创建key值 key_t key2 = ftok("./a.txt", 'b');//创建或打开消息队列int msgid = msgget(key2, IPC_CREAT | IPC_EXCL | 0666);if (msgid < 0){if (errno == EEXIST){printf("msgget eexist\n"); //已创建 msgid = msgget(key2, 0666);}else{perror("msgget err.");return -1;}}char reply_buf[HTML_SIZE] = {0};//分离请求内容为postchar *post = strstr(input, "post");char *p = strstr(input, "change=");*(p - 1) = '\0';printf("请求 = %s\n", post);//分离控制内容为 *changechar *change = p + strlen("change=");*(change+2)='\0';printf("操作change = %s\n", change);//设置消息类型为1 ctl.mytype = 1;//将控制信号放入到消息数据中strcpy(ctl.buf, change);printf("消息内容为:%s\n",ctl.buf);//将控制消息发送到消息队列中msgsnd(msgid, &ctl, sizeof(ctl) - sizeof(long), 0);sprintf(reply_buf, "<script>localStorage.setItem('usr_user_name', '%s');</script>", post);strcat(reply_buf, "<script>window.location.href = '/index.html';</script>");send(sock, reply_buf, strlen(reply_buf), 0);//删除消息队列// msgctl(msgid,IPC_RMID,NULL);return 0;}
网页端对页面信号处理
function get() {//v不仅仅是一个变量,与数组类似var v = document.getElementsByName("light");//v[0]表示的是第一个名字为username的标签// v[0].value="hello";var xhr = new XMLHttpRequest();//新建一个对象var url = "";
xhr.open("post", url, true);
xhr.onreadystatechange = function ()//检测发生变化{//表示响应完成并且成功if (xhr.readyState === 4 && xhr.status === 200) {var response = xhr.responseText;var x = response.split("\n");//将字符串以'\n'为分割分为一个字符串数组
console.log(x);
v[0].value = x[0];//响应正文
v[1].value = x[1];//响应正文
v[2].value = x[2];//响应正文
v[3].value = x[3];//响应正文}}
xhr.send("get");}function fun(obj) {var xhr = new XMLHttpRequest();var url = "";
xhr.open("POST", url, true);
console.log(obj);if (obj == 'on') {
console.log("postchange=01");
xhr.send("postchange=01");}else if (obj == 'off') {
console.log("postchange=00");
xhr.send("postchange=00");}}function funs(obj) {var xhr = new XMLHttpRequest();var url = ""; // 设置正确的URL
xhr.open("POST", url, true);if (obj == 'on') {
console.log("postchange=11");
xhr.send("postchange=11");} else if (obj == 'off') {
console.log("postchange=10");
xhr.send("postchange=10");}}