Commit f4192267 authored by 957dd's avatar 957dd

1204

parent 64f7fe6d
No preview for this file type
...@@ -14,6 +14,9 @@ void Delay_Ms() ...@@ -14,6 +14,9 @@ void Delay_Ms()
} }
void *serial_usart1(void *arg) void *serial_usart1(void *arg)
{ {
...@@ -22,6 +25,14 @@ void *serial_usart1(void *arg) ...@@ -22,6 +25,14 @@ void *serial_usart1(void *arg)
return NULL; return NULL;
} }
void *serial_usart2(void *arg)
{
serial_Receive_2();
return NULL;
}
void *AppExit(void *arg) void *AppExit(void *arg)
{ {
unsigned char bufpwm1[3]={0x01,0x01,0x00}; unsigned char bufpwm1[3]={0x01,0x01,0x00};
...@@ -57,19 +68,6 @@ int main(int argc, char *argv[]) { ...@@ -57,19 +68,6 @@ int main(int argc, char *argv[]) {
mosquitto_lib_init(); mosquitto_lib_init();
mosq = mosquitto_new(CLIENT_ID, true, NULL); mosq = mosquitto_new(CLIENT_ID, true, NULL);
init_mqtt(mosq); init_mqtt(mosq);
// 创建 mosquitto 客户端
//if (!mosq) {
// fprintf(stderr, "Failed to create mosquitto client\n");
// return EXIT_FAILURE;
// }
// 设置连接和消息回调
//mosquitto_connect_callback_set(mosq, on_connect);
// mosquitto_message_callback_set(mosq, on_message);
// 设置用户名和密码
//mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
// 连接到 MQTT 代理 // 连接到 MQTT 代理
rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60); rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60);
...@@ -83,8 +81,8 @@ int main(int argc, char *argv[]) { ...@@ -83,8 +81,8 @@ int main(int argc, char *argv[]) {
send_mqtt_message(mosq, TOPIC,ip_address);//将ip发送给mqtt send_mqtt_message(mosq, TOPIC,ip_address);//将ip发送给mqtt
pthread_t thread[2]; pthread_t thread[3];//线程标识符
if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0) if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0)
{ {
perror("Failed to create thread 1"); perror("Failed to create thread 1");
...@@ -96,8 +94,15 @@ int main(int argc, char *argv[]) { ...@@ -96,8 +94,15 @@ int main(int argc, char *argv[]) {
perror("Failed to create thread 2"); perror("Failed to create thread 2");
return 1; 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_start(mosq);//后台处理
mosquitto_loop_forever(mosq, -1, 1000); // -1 表示无限等待,1 表示处理消息的最大数量 mosquitto_loop_forever(mosq, -1, 1000); // -1 表示无限等待,1 表示处理消息的最大数量
...@@ -110,5 +115,8 @@ int main(int argc, char *argv[]) { ...@@ -110,5 +115,8 @@ int main(int argc, char *argv[]) {
// 等待线程2结束 // 等待线程2结束
pthread_join(thread[1], NULL); pthread_join(thread[1], NULL);
// 等待线程3结束
pthread_join(thread[2], NULL);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
\ No newline at end of file
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#define BROKER_ADDRESS "119.45.167.177" #define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883 #define BROKER_PORT 1883
#define CLIENT_ID "device1car/carer5434" #define CLIENT_ID "device1car/carer5434"
#define TOPIC "devices/controcar01" #define TOPIC "devices/controcar02"
#define USERNAME "admin" // 替换为你的用户名 #define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码 #define PASSWORD "admin" // 替换为你的密码
......
#include "serial.h" #include "serial.h"
#include "mqtt.h" #include "mqtt.h"
int fd; 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 init_serial(const char *port, int baudrate) { int init_serial(const char *port, int baudrate) {
int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY); int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
...@@ -41,6 +71,28 @@ void Delay_Ms1() ...@@ -41,6 +71,28 @@ void Delay_Ms1()
} }
void serial_Receive_2() {
char buffer[256];
//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);
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);
}
}
close(fds);
//free(buffer);
}
void serial_Receive() { void serial_Receive() {
unsigned char buffer[256]; unsigned char buffer[256];
//mosq = mosquitto_new(CLIENT_ID, true, NULL); //mosq = mosquitto_new(CLIENT_ID, true, NULL);
...@@ -110,7 +162,9 @@ void serial_Write(unsigned char *buf) ...@@ -110,7 +162,9 @@ void serial_Write(unsigned char *buf)
int serial_Init() int serial_Init()
{ {
fd = init_serial(SERIAL_PORT, 9600); fd = init_serial(SERIAL_PORT, 9600);
fds = init_serials(SERIAL_PORT_2, 9600);
if (fd == -1) return -1; if (fd == -1) return -1;
if (fds == -1) return -1;
printf("Serial communication started...\n"); printf("Serial communication started...\n");
} }
...@@ -11,9 +11,13 @@ ...@@ -11,9 +11,13 @@
#define SERIAL_PORT "/dev/ttyS3" // 根据实际设备修改 #define SERIAL_PORT "/dev/ttyS3" // 根据实际设备修改
#define SERIAL_PORT_2 "/dev/ttyS7" // 根据实际设备修改
int serial_Init(); int serial_Init();
void serial_Receive(); void serial_Receive();
void serial_Receive_2();
void serial_Write(unsigned char *buf); void serial_Write(unsigned char *buf);
//extern int fd; //extern int fd;
int init_serial(const char *port, int baudrate); int init_serial(const char *port, int baudrate);
int init_serials(const char *port, int baudrate);
#endif #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