Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
car-controlserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wenzhongjian
car-controlserver
Commits
f4192267
Commit
f4192267
authored
Dec 04, 2024
by
957dd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1204
parent
64f7fe6d
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
85 additions
and
17 deletions
+85
-17
main
main
+0
-0
main.c
main.c
+24
-15
mqtt.h
mqtt.h
+1
-1
serial.c
serial.c
+55
-1
serial.h
serial.h
+5
-0
No files found.
main
View file @
f4192267
No preview for this file type
main.c
View file @
f4192267
...
...
@@ -14,6 +14,9 @@ void Delay_Ms()
}
void
*
serial_usart1
(
void
*
arg
)
{
...
...
@@ -22,6 +25,14 @@ void *serial_usart1(void *arg)
return
NULL
;
}
void
*
serial_usart2
(
void
*
arg
)
{
serial_Receive_2
();
return
NULL
;
}
void
*
AppExit
(
void
*
arg
)
{
unsigned
char
bufpwm1
[
3
]
=
{
0x01
,
0x01
,
0x00
};
...
...
@@ -57,19 +68,6 @@ int main(int argc, char *argv[]) {
mosquitto_lib_init
();
mosq
=
mosquitto_new
(
CLIENT_ID
,
true
,
NULL
);
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 代理
rc
=
mosquitto_connect
(
mosq
,
BROKER_ADDRESS
,
BROKER_PORT
,
60
);
...
...
@@ -83,7 +81,7 @@ int main(int argc, char *argv[]) {
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
)
{
...
...
@@ -97,7 +95,14 @@ int main(int argc, char *argv[]) {
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 表示处理消息的最大数量
...
...
@@ -110,5 +115,8 @@ int main(int argc, char *argv[]) {
// 等待线程2结束
pthread_join
(
thread
[
1
],
NULL
);
// 等待线程3结束
pthread_join
(
thread
[
2
],
NULL
);
return
EXIT_SUCCESS
;
}
\ No newline at end of file
mqtt.h
View file @
f4192267
...
...
@@ -13,7 +13,7 @@
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883
#define CLIENT_ID "device1car/carer5434"
#define TOPIC "devices/controcar0
1
"
#define TOPIC "devices/controcar0
2
"
#define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码
...
...
serial.c
View file @
f4192267
#include "serial.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
fd
=
open
(
port
,
O_RDWR
|
O_NOCTTY
|
O_NDELAY
);
...
...
@@ -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
()
{
unsigned
char
buffer
[
256
];
//mosq = mosquitto_new(CLIENT_ID, true, NULL);
...
...
@@ -110,7 +162,9 @@ void serial_Write(unsigned char *buf)
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
"
);
}
serial.h
View file @
f4192267
...
...
@@ -11,9 +11,13 @@
#define SERIAL_PORT "/dev/ttyS3" // 根据实际设备修改
#define SERIAL_PORT_2 "/dev/ttyS7" // 根据实际设备修改
int
serial_Init
();
void
serial_Receive
();
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment