Commit 1d273306 authored by 957dd's avatar 957dd

24 1214

parent f4192267
#include <stdio.h>
#include <time.h>
void Delay_Ms(int sec,int msec)
{
struct timespec ts;
ts.tv_sec = sec; // 秒
ts.tv_nsec = msec*1000000; // 1毫秒 = 1000000纳秒
nanosleep(&ts,NULL);
}
\ No newline at end of file
#ifndef __DELAY_H__
#define __DELAY_H__
void Delay_Ms(int sec,int msec);
#endif
\ No newline at end of file
#include "gps.h"
#include "delay.h"
char glat[20],glon[20];
void parse_gnrmc(const char *nmea_sentence) {
char lat[20], lat_dir, lon[20], lon_dir;
//printf("dsada");
char time[11], status;
if (sscanf(nmea_sentence, "$GNRMC,%10[^,],%c,%[^,],%c,%[^,],%c", time, &status, lat, &lat_dir, lon, &lon_dir) == 6)
{
if (status == 'A')
{ // 定位有效
//double glatitude = convert_to_decimal(glat, glat_dir);
//double glongitude = convert_to_decimal(glon, glon_dir);
printf("Time (UTC): %s\n", time);
printf("glatitude: %s\n", lat);
printf("glongitude: %s\n", lon);
sscanf(lat,"%[^,]",glat);
sscanf(lon,"%[^,]",glon);
}
else
{
printf("GNRMC data is invalid (status: %c)\n", status);
}
}
else
{
printf("Failed to parse GNRMC sentence.\n");
}
}
#ifndef __GPS_H__
#define __GPS_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
extern char glat[20], glon[20];
void parse_gnrmc(const char *nmea_sentence);//定位函数
#endif
\ No newline at end of file
#ifndef __IP_H__
#define __IP_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
int ipaddr();
extern char ip_address[INET_ADDRSTRLEN];
#endif
#ifndef __IP_H__
#define __IP_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
int ipaddr();
extern char ip_address[INET_ADDRSTRLEN];
#endif
File deleted
#include "ip.h"
#include "mqtt.h"
#include "serial.h"
//mosq = mosquitto_new(CLIENT_ID, true, NULL);
void Delay_Ms()
{
struct timespec ts;
ts.tv_sec = 0; // 秒
ts.tv_nsec = 100000000; // 1毫秒 = 1000000纳秒
nanosleep(&ts,NULL);
}
void *serial_usart1(void *arg)
{
serial_Receive();
return NULL;
}
void *serial_usart2(void *arg)
{
serial_Receive_2();
return NULL;
}
void *AppExit(void *arg)
{
unsigned char bufpwm1[3]={0x01,0x01,0x00};
unsigned char bufpwm2[3]={0x01,0x02,0x00};
unsigned char bufpwm3[3]={0x01,0x03,0x00};
while(1)
{
Delay_Ms();
gPwmCount++;
if(gPwmCount>20)
{
gPwmCount=21;
serial_Write(bufpwm1);
serial_Write(bufpwm2);
serial_Write(bufpwm3);
}
}
return NULL;
}
int main(int argc, char *argv[]) {
//struct mosquitto *mosq;
int rc=0;
serial_Init();
gStart=time(NULL);
// 初始化 mosquitto 库
mosquitto_lib_init();
mosq = mosquitto_new(CLIENT_ID, true, NULL);
init_mqtt(mosq);
// 连接到 MQTT 代理
rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60);
if (rc != MOSQ_ERR_SUCCESS) {
fprintf(stderr, "Failed to connect to broker: %s\n", mosquitto_strerror(rc));
mosquitto_destroy(mosq);
return EXIT_FAILURE;
}
ipaddr();
send_mqtt_message(mosq, TOPIC,ip_address);//将ip发送给mqtt
pthread_t thread[3];//线程标识符
if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0)
{
perror("Failed to create thread 1");
return 1;
}
if(pthread_create(&thread[1],NULL,AppExit,NULL)!=0)
{
perror("Failed to create thread 2");
return 1;
}
if(pthread_create(&thread[2],NULL,serial_usart2,NULL)!=0)
{
perror("Failed to create thread 2");
return 1;
}
//开始循环,处理网络流量
//mosquitto_loop_start(mosq);//后台处理
mosquitto_loop_forever(mosq, -1, 1000); // -1 表示无限等待,1 表示处理消息的最大数量
// 清理
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
// 等待线程1结束
pthread_join(thread[0], NULL);
// 等待线程2结束
pthread_join(thread[1], NULL);
// 等待线程3结束
pthread_join(thread[2], NULL);
return EXIT_SUCCESS;
#include "main.h"
#include "ip.h"
#include "mqtt.h"
#include "serial.h"
void *serial_usart1(void *arg)
{
printf("serial_usart 1 start\n");
serial_Receive_1();
return NULL;
}
void *AppExit(void *arg)
{
/*
unsigned char bufpwm1[3]={0x01,0x01,0x00};
unsigned char bufpwm2[3]={0x01,0x02,0x00};
unsigned char bufpwm3[3]={0x01,0x03,0x00};
while(1)
{
Delay_Ms();
gPwmCount++;
if(gPwmCount>20)
{
gPwmCount=21;
serial_Write(bufpwm1);
serial_Write(bufpwm2);
serial_Write(bufpwm3);
}
}*/
return NULL;
}
void *Mqttbeat(void *arg)
{
printf("Mqttbeat start\n");
while(1)
{
mqtt_wirte();
Delay_Ms(3,200);
}
return NULL;
}
void *serial_usart2(void *arg)
{
printf("serial_usart 2 start\n");
serial_Receive_2();
}
int main(int argc, char *argv[]) {
gStart=time(NULL);//开始时间戳
serial_Init();//串口初始化
mqtt_init();//mqtt初始化
mqtt_create(mosq);//创建mqtt客户端
ipaddr();//获取ip
mqtt_wirte();//mqtt心跳首次发送
thread_start(serial_usart1,AppExit,Mqttbeat,serial_usart2);
mqtt_cycle(mosq);
mqtt_clean(mosq);
thread_end();
return EXIT_SUCCESS;
}
\ No newline at end of file
#ifndef __MAIN_H__
#define __MAIN_H__
#include "ip.h"
#include "mqtt.h"
#include "serial.h"
#include "delay.h"
pthread_t thread[4];
int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg),void *Mqttbeat(void *arg),void *serial_usart2(void *arg))
{
if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0)
{
perror("Failed to create thread 1");
return 1;
}
if(pthread_create(&thread[1],NULL,AppExit,NULL)!=0)
{
perror("Failed to create thread 2");
return 2;
}
if(pthread_create(&thread[2],NULL,Mqttbeat,NULL)!=0)
{
perror("Failed to create thread 3");
return 3;
}
if(pthread_create(&thread[3],NULL,serial_usart2,NULL)!=0)
{
perror("Failed to create thread 1");
return 4;
}
}
void thread_end()
{
// 等待线程1结束
pthread_join(thread[0], NULL);
// 等待线程2结束
pthread_join(thread[1], NULL);
// 等待线程1结束
pthread_join(thread[2], NULL);
pthread_join(thread[3], NULL);
}
#endif
\ No newline at end of file
File added
#include "mqtt.h"
#include "gps.h"
time_t gStart;
int gPwmCount = 0; // 计数
int gmessage_type=10086;
int rc=0;
struct mosquitto *mosq;
int init_mqtt(struct mosquitto *mosq)
unsigned char gmodeTmep=0;
unsigned char gtypeTemp=0;
unsigned char gpinTemp=0;
unsigned char gvaltemp=0;
unsigned char gvalt[4];
void on_connect(struct mosquitto *mosq, void *obj, int rc) //回调函数
{
if (rc == 0)
{
printf("Connected to broker\n");
mosquitto_subscribe(mosq, NULL, TOPIC, 0);
time_t start=time(NULL);
}
else
{
fprintf(stderr, "Connection failed with code %d\n", rc);
}
}
int mqtt_init()
{
// 初始化 mosquitto 库
mosquitto_lib_init();
mosq = mosquitto_new(CLIENT_ID, true, NULL);
if (!mosq) {
fprintf(stderr, "Failed to create Mosquitto client\n");
return -1;
}
// 设置连接和消息回调
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
//设置用户名和密码
mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
}
void send_mqtt_message(struct mosquitto *mosq,const char *topic, const char *message)
//int init_mqtt(struct mosquitto *mosq)
void mqtt_wirte()
{
mosquitto_publish(mosq, NULL, topic, strlen(message), message, 0, false) ;
int message=1;
cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject();
cJSON_AddStringToObject(body, "ip", ip_address);
cJSON_AddStringToObject(body, "ID", TOPIC);
cJSON_AddStringToObject(body, "N", glat);
cJSON_AddStringToObject(body, "E", glon);
if(gmessage_type==0)
{
message=0;
}else{
message=1;
}
cJSON_AddNumberToObject(head, "message_type",message);
cJSON_AddItemToObject(root, "body", body);
cJSON_AddItemToObject(root, "head",head);
char *payload = cJSON_PrintUnformatted(root);
printf("%s\n",payload);
mosquitto_publish(mosq, NULL, TOPIC, strlen(payload), payload, 0, false);
}
void on_connect(struct mosquitto *mosq, void *obj, int rc)
void message_0()//message_type为0时候要改变的值
{
if (rc == 0)
{
printf("Connected to broker\n");
mosquitto_subscribe(mosq, NULL, TOPIC, 0);
time_t start=time(NULL);
}
else
for(int i=0;i<2;i++)
{
fprintf(stderr, "Connection failed with code %d\n", rc);
gPwmCount = 0;
switch(i)
{
case 0:
gvalt[0]=gmodeTmep;
gvalt[1]=0;
gvalt[2]=0;
printf("gmodeTmep:%d\n",gvalt[0]);
printf("gtypeTemp:%d\n",gvalt[1]);
printf("gvaltemp:%d\n",gvalt[2]);
serial_Write(gvalt);
break;
case 1:
gvalt[0]=gmodeTmep;
gvalt[1]=1;
gvalt[2]=0;
printf("gmodeTmep:%d\n",gvalt[0]);
printf("gtypeTemp:%d\n",gvalt[1]);
printf("gvaltemp:%d\n",gvalt[2]);
serial_Write(gvalt);
break;
case 2:
gvalt[0]=gmodeTmep;
gvalt[1]=2;
gvalt[2]=0;
printf("gmodeTmep:%d\n",gvalt[0]);
printf("gtypeTemp:%d\n",gvalt[1]);
printf("gvaltemp:%d\n",gvalt[2]);
serial_Write(gvalt);
break;
default:
break;
}
}
printf("已断开连接\n");
}
void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm
{
//pwm控制
// 提取 pin_ctrl_req 对象
//cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
if (!cJSON_IsObject(pwm_ctrl))
{
return;
}
cJSON *mode = cJSON_GetObjectItem(pwm_ctrl, "mode"); //mode=1 速度,mode=2 转向(unsigned char)
cJSON *type = cJSON_GetObjectItem(pwm_ctrl, "type");
cJSON *val = cJSON_GetObjectItem(pwm_ctrl, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char)
gmodeTmep= mode->valueint;
gtypeTemp=type->valueint;
gvaltemp= val->valueint;
gvalt[0]=gmodeTmep;
gvalt[1]=gtypeTemp;
gvalt[2]=gvaltemp;
gPwmCount = 0;
printf("gmodeTmep:%d\n",gvalt[0]);
printf("gtypeTemp:%d\n",gvalt[1]);
printf("gvaltemp:%d\n",gvalt[2]);
serial_Write(gvalt);
}
void message_4(cJSON *body, cJSON *pin_setctrl)
{
//cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl");
if (!cJSON_IsObject(pin_setctrl))
{
return;
}
cJSON *pin = cJSON_GetObjectItem(pin_setctrl, "pin");
cJSON *val = cJSON_GetObjectItem(pin_setctrl, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char)
gpinTemp=pin->valueint;
gvaltemp= val->valueint;
gPwmCount=0;
gvalt[0]=0;
gvalt[1]=gpinTemp;
gvalt[2]=gvaltemp;
printf("gpinTemp:%d\n",gpinTemp);
printf("gtypeTemp:%d\n",gvaltemp);
serial_Write(gvalt);
}
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) {
time_t end = time(NULL);
if(gStart+5>end)
{
printf("正在处理过期消息");
......@@ -56,89 +184,112 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
{
// 将消息内容转换为字符串
char *payload_str = (char *)malloc(message->payloadlen + 1);
if (payload_str)
if (!payload_str)
{
memcpy(payload_str, message->payload, message->payloadlen);
payload_str[message->payloadlen] = '\0';
return;
}
memcpy(payload_str, message->payload, message->payloadlen);
payload_str[message->payloadlen] = '\0';
// 解析 JSON
cJSON *json = cJSON_Parse(payload_str);
if (json == NULL)
// 解析 JSON
cJSON *json = cJSON_Parse(payload_str);
if (json == NULL)
{
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
}
else
{
// 提取 head 对象
cJSON *head = cJSON_GetObjectItem(json, "head");
if (!cJSON_IsObject(head))
{
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
}
else
return;
}
// 提取 message_type
cJSON *message_type = cJSON_GetObjectItem(head, "message_type");
if (!cJSON_IsNumber(message_type))
{
return;
}
cJSON *body = cJSON_GetObjectItem(json, "body");
if (!cJSON_IsObject(body))
{
return;
}
cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl");
if(pwm_ctrl==NULL||pin_setctrl==NULL)
{
free(payload_str);
return;
}
gmessage_type=message_type->valueint;
printf("message_type: %d\n",message_type->valueint);
switch(gmessage_type)
{
// 提取 head 对象
cJSON *head = cJSON_GetObjectItem(json, "head");
if (!cJSON_IsObject(head))
{
return;
}
// 提取 message_type
cJSON *message_type = cJSON_GetObjectItem(head, "message_type");
if (!cJSON_IsNumber(message_type))
{
return;
}
cJSON *body = cJSON_GetObjectItem(json, "body");
if (!cJSON_IsObject(body))
{
return;
}
printf("message_type: %d\n", message_type->valueint);
if (message_type->valueint==3)
{ //pwm控制
// 提取 pin_ctrl_req 对象
cJSON *pwm_ctrl_req = cJSON_GetObjectItem(body, "p_ctrl_req");
if (!cJSON_IsObject(pwm_ctrl_req))
{
return;
}
// 提取 data 数组
cJSON *data = cJSON_GetObjectItem(pwm_ctrl_req, "data");
if (!cJSON_IsArray(data))
{
return;
}
// 遍历数组
for (int i = 0; i < cJSON_GetArraySize(data); ++i)
{
// 提取数组中的每个对象
cJSON *item = cJSON_GetArrayItem(data, i);
if (!cJSON_IsObject(item))
{
continue;
}
cJSON *mode = cJSON_GetObjectItem(item, "mode"); //mode=1 速度,mode=2 转向(unsigned char)
cJSON *type = cJSON_GetObjectItem(item, "type");
cJSON *val = cJSON_GetObjectItem(item, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char)
unsigned char modeTmep= mode->valueint;
unsigned char typeTemp=type->valueint;
unsigned char valtemp= val->valueint;
unsigned char valt[4];
valt[0]=modeTmep;
valt[1]=typeTemp;
valt[2]=valtemp;
gPwmCount = 0;
printf("%d\n",valt[0]);
printf("%d\n",valt[1]);
printf("%d\n",valt[2]);
serial_Write(valt);
}
}
// 释放 payload 字符串
free(payload_str);
}
}
case 0:
message_0();
break;
case 3:
message_3(body,pwm_ctrl);
break;
case 4:
message_4(body,pin_setctrl);
break;
default:
break;
}
}
// 释放 payload 字符串
free(payload_str);
}
}
\ No newline at end of file
int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端
{
// 设置连接和消息回调
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
//设置用户名和密码
mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
// 创建 mosquitto 客户端
rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60);
if (rc != MOSQ_ERR_SUCCESS) {
fprintf(stderr, "Failed to connect to broker: %s\n", mosquitto_strerror(rc));
mosquitto_destroy(mosq);
return EXIT_FAILURE;
}
//mosquitto_subscribe_opt_set(mosq, TOPIC, strlen(TOPIC), 1, MOSQ_OPT_NO_LOCAL);
}
int mqtt_cycle(struct mosquitto *mosq)
{
//mosquitto_message_callback_set(mosq, NULL);
// 开始循环,处理网络流量
//mosquitto_loop_start(mosq);//后台处理
//mosquitto_subscribe_with_options(mosq, NULL, TOPIC, 0, MQTT_SUB_OPT_NO_LOCAL);
mosquitto_loop_forever(mosq, -1, 1); // -1 表示无限等待,1 表示处理消息的最大数量
//mosquitto_message_retry_set(mosq, 10);
}
int mqtt_clean(struct mosquitto *mosq)
{
// 清理
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
}
\ No newline at end of file
......@@ -13,7 +13,7 @@
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883
#define CLIENT_ID "device1car/carer5434"
#define TOPIC "devices/controcar02"
#define TOPIC "controcar0005"
#define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码
......@@ -23,11 +23,14 @@ extern time_t gStart;
extern int gPwmCount; // 计数
extern int gmessage_type;
int init_mqtt(struct mosquitto *mosq) ;
void send_mqtt_message(struct mosquitto *mosq,const char *topic, const char *message);
int mqtt_init();
int mqtt_create(struct mosquitto *mosq);
void on_connect(struct mosquitto *mosq, void *obj, int rc);
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) ;
void mqtt_wirte();
int mqtt_cycle(struct mosquitto *mosq);
int mqtt_clean(struct mosquitto *mosq);
#endif
\ No newline at end of file
#include "serial.h"
#include "mqtt.h"
#include "delay.h"
#include "gps.h"
int fd, fds;
int init_serials(const char *port, int baudrate)
{
int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open serial port");
return -1;
}
struct termios options;
tcgetattr(fd, &options);
// 设置波特率
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// 配置串口
options.c_cflag &= ~PARENB; // 无校验
options.c_cflag &= ~CSTOPB; // 1停止位
options.c_cflag &= ~CSIZE; // 清除数据位设置
options.c_cflag |= CS8; // 8数据位
options.c_cflag |= CLOCAL | CREAD; // 忽略调制解调器状态,开启接收器
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // 原始模式
options.c_iflag &= ~(IXON | IXOFF | IXANY); // 禁用流控
options.c_oflag &= ~OPOST; // 原始输出
tcsetattr(fd, TCSANOW, &options);
return fd;
}
int fd1,fd2;
int init_serial(const char *port, int baudrate) {
int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
......@@ -62,109 +34,112 @@ int init_serial(const char *port, int baudrate) {
return fd;
}
void Delay_Ms1()
{
struct timespec ts1;
ts1.tv_sec = 1; // 秒
ts1.tv_nsec =0 ;//100000000; // 1毫秒 = 1000000纳秒
nanosleep(&ts1,NULL);
}
void serial_Receive_2() {
char buffer[256];
void serial_Receive_2()
{
char buffer[BUFFER_SIZE];
char nmea_sentence[BUFFER_SIZE];
int index=0;
//mosq = mosquitto_new(CLIENT_ID, true, NULL);
while (1) {
int bytes_read = read(fds, buffer, sizeof(buffer)-1);
if (bytes_read > 0) {
//printf("Received chatdate: %s\n", buffer);
while (1)
{
int bytes_read = read(fd2, buffer, BUFFER_SIZE-1);
if (bytes_read > 0)
{
buffer[bytes_read] = '\0';
printf("Received chatdate: %s\n", buffer);
//int receivedNumber = atoi(buffer);
//printf("Received intdate: %d\n", receivedNumber);
//mosquitto_publish(mosq, NULL, TOPIC, strlen(buffer), buffer, 0, false);
//printf("%s\n",mosq);
for (int i = 0; i < bytes_read; i++)
{
if (buffer[i] == '$')
{
index = 0; // 新语句开始
}
if (index < BUFFER_SIZE - 1)
{
nmea_sentence[index++] = buffer[i];
nmea_sentence[index] = '\0';
}
if (buffer[i] == '\n')
{ // 一条语句结束
if (strstr(nmea_sentence, "$GNRMC"))
{
parse_gnrmc(nmea_sentence); // 解析$GNRMC语句
}
}
}
}
Delay_Ms(2,0);
}
close(fds);
//free(buffer);
close(fd2);
}
void serial_Receive() {
void serial_Receive_1()
{
unsigned char buffer[256];
//mosq = mosquitto_new(CLIENT_ID, true, NULL);
while (1) {
int bytes_read = read(fd, buffer, sizeof(buffer)-1);
while(1)
{
//printf("sada\n");
int bytes_read =read(fd1, buffer, sizeof(buffer)-1);
if (bytes_read > 0) {
//printf("Received chatdate: %s\n", buffer);
//buffer[bytes_read] = '0xFF';
printf("Received chatdate:: %d\n", buffer[1]);
//buffer[bytes_read] = '\0';
printf("Received chatdate: %hhn\n", buffer);
//int receivedNumber = atoi(buffer);
//printf("Received intdate: %d\n", receivedNumber);
//mosquitto_publish(mosq, NULL, TOPIC, strlen(buffer), buffer, 0, false);
//printf("%s\n",mosq);
int i=buffer[1];
if(i==1)
{
send_mqtt_message(mosq, TOPIC,"1");
i=0;
}
if(i==2)
{
send_mqtt_message(mosq, TOPIC,"2");
i=0;
}
if(i==3)
{
send_mqtt_message(mosq, TOPIC,"3");
i=0;
}
if(i==4)
{
send_mqtt_message(mosq, TOPIC,"4");
i=0;
}
}
Delay_Ms1();
Delay_Ms(1,0);
}
close(fd);
//free(buffer);
close(fd1);
}
void serial_Write(unsigned char *buf)
{
unsigned char buffers[5];
buffers[0]=0xAA;
//write(fd, buffers[0], sizeof(buffers));
for(int i=1;i<4;i++)
if(gmessage_type==3)
{
buffers[0]=0xAA;
//write(fd, buffers[0], sizeof(buffers));
for(int i=1;i<4;i++)
{
buffers[i]=buf[i-1];
//write(fd, buffers[i], sizeof(buffers));
}
buffers[4]=0xFE;
write(fd1, buffers, sizeof(buffers));
}
if(gmessage_type==4)
{
buffers[i]=buf[i-1];
//write(fd, buffers[i], sizeof(buffers));
buffers[0]=0xAA;
buffers[1]=0xEE;
//write(fd, buffers[0], sizeof(buffers));
for(int i=2;i<4;i++)
{
buffers[i]=buf[i-1];
//write(fd, buffers[i], sizeof(buffers));
}
buffers[4]=0xFE;
write(fd1, buffers, sizeof(buffers));
}
buffers[4]=0xFE;
write(fd, buffers, sizeof(buffers));
//printf("%d\n",buffers[0]);
//printf("%d\n",buffers[1]);
//printf("%d\n",buffers[2]);
//printf("%d\n",buffers[3]);
//printf("%d\n",buffers[4]);
}
int serial_Init()
{
fd = init_serial(SERIAL_PORT, 9600);
fds = init_serials(SERIAL_PORT_2, 9600);
if (fd == -1) return -1;
if (fds == -1) return -1;
printf("Serial communication started...\n");
fd1 = init_serial(SERIAL_PORT1, 9600);
fd2 = init_serial(SERIAL_PORT2, 9600);
if (fd1 == -1) {
return -1;
printf("Serial 1 communication started...\n");
}
if (fd2 == -1) {
return -1;
printf("Serial 2 communication started...\n");
}
}
......@@ -9,15 +9,11 @@
#include <unistd.h>
#include <termios.h>
#define SERIAL_PORT "/dev/ttyS3" // 根据实际设备修改
#define SERIAL_PORT_2 "/dev/ttyS7" // 根据实际设备修改
#define SERIAL_PORT1 "/dev/ttyS3" // 根据实际设备修改
#define SERIAL_PORT2 "/dev/ttyS7"
#define BUFFER_SIZE 256
int serial_Init();
void serial_Receive();
void serial_Receive_2();
void serial_Receive_1();
void serial_Receive_2();
void serial_Write(unsigned char *buf);
//extern int fd;
int init_serial(const char *port, int baudrate);
int init_serials(const char *port, int baudrate);
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment