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
This diff is collapsed.
......@@ -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