Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SusPilot3_3
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Robert Piotrowski
SusPilot3_3
Commits
9258e7b0
Commit
9258e7b0
authored
1 month ago
by
Robert Piotrowski
Browse files
Options
Downloads
Patches
Plain Diff
serial_port as an obligatory parameter
parent
5cac86c0
Branches
Branches containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
serial_to_mqtt_bridge/serial_to_mqtt.py
+7
-5
7 additions, 5 deletions
serial_to_mqtt_bridge/serial_to_mqtt.py
with
7 additions
and
5 deletions
serial_to_mqtt_bridge/serial_to_mqtt.py
+
7
−
5
View file @
9258e7b0
...
@@ -24,10 +24,10 @@ def cyclic_file_reader(file_path):
...
@@ -24,10 +24,10 @@ def cyclic_file_reader(file_path):
yield
line
.
strip
()
# Yield each line, stripped of whitespace
yield
line
.
strip
()
# Yield each line, stripped of whitespace
# Main function to handle serial data or send lines from file if no port is provided
# Main function to handle serial data or send lines from file if no port is provided
def
main
(
serial_port
=
None
,
baud_rate
=
115200
,
file_path
=
"
sim_data.txt
"
,
mqtt_broker
=
"
localhost
"
,
mqtt_port
=
1883
,
mqtt_topic
=
"
serial2MQTT
"
,
sim_interval
=
1
):
def
main
(
serial_port
=
None
,
baud_rate
=
115200
,
file_path
=
"
sim_data.txt
"
,
mqtt_broker
=
"
192.168.52.5
"
,
mqtt_port
=
1883
,
mqtt_topic
=
"
serial2MQTT
"
,
sim_interval
=
1
):
connect_mqtt
(
mqtt_broker
,
mqtt_port
)
connect_mqtt
(
mqtt_broker
,
mqtt_port
)
if
serial_port
:
if
serial_port
.
lower
()
!=
"
sim
"
:
# If serial port is provided, read from it
# If serial port is provided, read from it
ser
=
serial
.
Serial
(
serial_port
,
baud_rate
,
timeout
=
1
)
ser
=
serial
.
Serial
(
serial_port
,
baud_rate
,
timeout
=
1
)
try
:
try
:
...
@@ -51,7 +51,7 @@ def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_brok
...
@@ -51,7 +51,7 @@ def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_brok
client
.
loop_stop
()
client
.
loop_stop
()
client
.
disconnect
()
client
.
disconnect
()
else
:
else
:
# If no serial port is provid
ed, send lines from the file cyclically
# If simulation mode is select
ed, send lines from the file cyclically
file_reader
=
cyclic_file_reader
(
file_path
)
file_reader
=
cyclic_file_reader
(
file_path
)
cnt
=
0
cnt
=
0
try
:
try
:
...
@@ -70,13 +70,15 @@ def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_brok
...
@@ -70,13 +70,15 @@ def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_brok
client
.
loop_stop
()
client
.
loop_stop
()
client
.
disconnect
()
client
.
disconnect
()
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
# Load configuration from YAML
# Load configuration from YAML
config
=
load_config
()
config
=
load_config
()
# Argument parser for serial port, baud rate, and file path
# Argument parser for serial port, baud rate, and file path
parser
=
argparse
.
ArgumentParser
(
description
=
"
Read from serial port and publish to MQTT.
"
)
parser
=
argparse
.
ArgumentParser
(
description
=
"
Read
data
from serial port and publish to MQTT.
"
)
parser
.
add_argument
(
"
--
serial_port
"
,
type
=
str
,
default
=
config
[
'
serial
'
].
get
(
'
port
'
),
help
=
"
Serial port to connect to (e.g., /dev/ttyUSB0)
"
)
parser
.
add_argument
(
"
serial_port
"
,
type
=
str
,
help
=
"
Serial port to connect to (e.g., /dev/ttyUSB0)
or
'
sim
'
for simulation mode
"
)
parser
.
add_argument
(
"
--baud_rate
"
,
type
=
int
,
default
=
config
[
'
serial
'
].
get
(
'
baud_rate
'
,
115200
),
help
=
"
Baud rate for serial communication
"
)
parser
.
add_argument
(
"
--baud_rate
"
,
type
=
int
,
default
=
config
[
'
serial
'
].
get
(
'
baud_rate
'
,
115200
),
help
=
"
Baud rate for serial communication
"
)
parser
.
add_argument
(
"
--file_path
"
,
type
=
str
,
default
=
config
[
'
sim
'
].
get
(
'
file_path
'
),
help
=
"
Path to the file with data for MQTT
"
)
parser
.
add_argument
(
"
--file_path
"
,
type
=
str
,
default
=
config
[
'
sim
'
].
get
(
'
file_path
'
),
help
=
"
Path to the file with data for MQTT
"
)
parser
.
add_argument
(
"
--mqtt_broker
"
,
type
=
str
,
default
=
config
[
'
mqtt
'
].
get
(
'
broker
'
,
"
localhost
"
),
help
=
"
MQTT broker address
"
)
parser
.
add_argument
(
"
--mqtt_broker
"
,
type
=
str
,
default
=
config
[
'
mqtt
'
].
get
(
'
broker
'
,
"
localhost
"
),
help
=
"
MQTT broker address
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment