Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Arduino_python_communication
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
Show more breadcrumbs
CREATE Lab
Lab systems
Arduino_python_communication
Commits
e7522bf1
Commit
e7522bf1
authored
3 years ago
by
Kai Junge
Browse files
Options
Downloads
Patches
Plain Diff
Organised comms_wrapper.py
parent
9c63f4fd
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+1
-1
1 addition, 1 deletion
.gitignore
comms_wrapper.py
+59
-57
59 additions, 57 deletions
comms_wrapper.py
pyComms/pyComms.ino
+2
-1
2 additions, 1 deletion
pyComms/pyComms.ino
pyComms/pyCommsLib.cpp
+1
-0
1 addition, 0 deletions
pyComms/pyCommsLib.cpp
with
63 additions
and
59 deletions
.gitignore
+
1
−
1
View file @
e7522bf1
__pycache__/
__pycache__/
\ No newline at end of file
This diff is collapsed.
Click to expand it.
comms_wrapper.py
+
59
−
57
View file @
e7522bf1
...
...
@@ -4,7 +4,6 @@ from threading import Thread
import
time
from
pynput
import
keyboard
########
### Arduino communication
########
...
...
@@ -23,6 +22,7 @@ class Arduino:
self
.
handshakeStatus
=
False
# Private methods
def
_serial_readline
(
self
):
while
1
:
try
:
...
...
@@ -42,7 +42,7 @@ class Arduino:
self
.
arduino
.
write
(
bytes
(
msg
,
'
utf-8
'
))
def
add_new_message_name
(
self
,
msgName
):
def
_
add_new_message_name
(
self
,
msgName
):
if
msgName
in
self
.
receivedMessages
:
print
(
"
!! Message name
"
,
msgName
,
"
already exists so it cannot be added. !!
"
)
else
:
...
...
@@ -50,14 +50,6 @@ class Arduino:
self
.
messageNames
=
list
(
self
.
receivedMessages
.
keys
())
def
delete_message_name
(
self
,
msgName
):
if
msgName
in
self
.
receivedMessages
:
del
self
.
receivedMessages
[
msgName
]
self
.
messageNames
=
list
(
self
.
receivedMessages
.
keys
())
else
:
print
(
"
!! Message name
"
,
msgName
,
"
does not exist so it cannot be deleted. !!
"
)
def
_just_connect
(
self
):
# Connect to the arduino device
try
:
...
...
@@ -76,8 +68,45 @@ class Arduino:
self
.
arduino
.
dtr
=
False
def
disconnect_arduino
(
self
):
self
.
arduino
.
close
()
def
_disect_and_save_message
(
self
,
msg
):
receivedMessageTemp
=
self
.
receivedMessages
if
msg
[
-
2
]
!=
"
:
"
:
return
False
msg
=
msg
[
1
:
-
2
]
splitMsg
=
msg
.
split
(
"
:
"
)
for
singleMsg
in
splitMsg
:
if
len
(
singleMsg
.
split
(
"
;
"
))
==
2
:
msgName
=
singleMsg
.
split
(
"
;
"
)[
0
]
msgPayload
=
singleMsg
.
split
(
"
;
"
)[
1
]
if
msgName
not
in
self
.
messageNames
:
return
False
receivedMessageTemp
[
msgName
]
=
msgPayload
else
:
return
False
self
.
receivedMessages
=
receivedMessageTemp
return
True
def
_list_connected_serial_devices
():
devices
=
list
(
serial
.
tools
.
list_ports
.
comports
())
for
device
in
devices
:
print
(
device
)
# Public methods
def
define_message_names
(
self
,
msgNames
):
if
type
(
msgNames
)
==
list
:
for
msg
in
msgNames
:
self
.
_add_new_message_name
(
msg
)
else
:
print
(
"
Argument should be of type
'
list
'"
)
def
connect_and_handshake
(
self
):
...
...
@@ -124,11 +153,15 @@ class Arduino:
return
self
.
handshakeStatus
def
disconnect_arduino
(
self
):
self
.
arduino
.
close
()
def
send_message
(
self
,
msg
):
self
.
_serial_write
(
msg
+
"
\n
"
)
def
receive_message
(
self
):
def
receive_message
(
self
,
showOutput
=
False
,
verbose
=
False
):
if
not
self
.
handshakeStatus
:
print
(
"
!! Handshake not completed !!
"
)
return
False
...
...
@@ -144,40 +177,20 @@ class Arduino:
except
:
isMessageValid
=
False
if
isMessageValid
:
isMessageValid
=
self
.
disect_and_save_message
(
msg
)
isMessageValid
=
self
.
_
disect_and_save_message
(
msg
)
else
:
pass
if
showOutput
:
if
verbose
:
print
(
"
Raw message received:
"
,
self
.
_rawReceivedMessage
,
"
| Segmented message:
"
,
self
.
receivedMessages
)
else
:
print
(
"
Received:
"
,
self
.
receivedMessages
)
time
.
sleep
(
0.000001
)
return
True
def
disect_and_save_message
(
self
,
msg
):
receivedMessageTemp
=
self
.
receivedMessages
if
msg
[
-
2
]
!=
"
:
"
:
return
False
msg
=
msg
[
1
:
-
2
]
splitMsg
=
msg
.
split
(
"
:
"
)
for
singleMsg
in
splitMsg
:
if
len
(
singleMsg
.
split
(
"
;
"
))
==
2
:
msgName
=
singleMsg
.
split
(
"
;
"
)[
0
]
msgPayload
=
singleMsg
.
split
(
"
;
"
)[
1
]
if
msgName
not
in
self
.
messageNames
:
return
False
receivedMessageTemp
[
msgName
]
=
msgPayload
else
:
return
False
self
.
receivedMessages
=
receivedMessageTemp
return
True
########
### Key commands
########
...
...
@@ -186,34 +199,23 @@ class Key():
def
__init__
(
self
):
self
.
keyPressValue
=
None
self
.
keyReleaseValue
=
None
self
.
start_keyboard_listener
()
self
.
_
start_keyboard_listener
()
def
on_press
(
self
,
key
):
def
_
on_press
(
self
,
key
):
try
:
self
.
keyPressValue
=
key
.
char
except
AttributeError
:
self
.
keyPressValue
=
key
def
on_release
(
self
,
key
):
def
_
on_release
(
self
,
key
):
try
:
self
.
keyReleaseValue
=
key
.
char
except
AttributeError
:
self
.
keyReleaseValue
=
key
def
start_keyboard_listener
(
self
):
listener
=
keyboard
.
Listener
(
on_press
=
self
.
on_press
,
on_release
=
self
.
on_release
)
def
_
start_keyboard_listener
(
self
):
listener
=
keyboard
.
Listener
(
on_press
=
self
.
_
on_press
,
on_release
=
self
.
_
on_release
)
listener
.
start
()
print
(
"
keyboard listener started
"
)
########
### Other useful functions
########
def
list_connected_serial_devices
():
devices
=
list
(
serial
.
tools
.
list_ports
.
comports
())
for
device
in
devices
:
print
(
device
)
\ No newline at end of file
print
(
"
keyboard listener started
"
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
pyComms/pyComms.ino
+
2
−
1
View file @
e7522bf1
...
...
@@ -12,7 +12,7 @@ String msgName[] = {"item1", "item2", "item3"};
// An array of strings which will be the payload
// Each element corresponds to the message defined in msgName
// Make sure the array length of msgName and Data match
String
D
ata
[]
=
{
"Null"
,
"Null"
,
"Null"
};
String
d
ata
Carrier
[]
=
{
"Null"
,
"Null"
,
"Null"
};
// The length of the msgName and Data array
const
int
numOfMsg
=
3
;
...
...
@@ -31,6 +31,7 @@ void setup() {
void
loop
()
{
////////////////////
// RECEIVING DATA
////////////////////
...
...
This diff is collapsed.
Click to expand it.
pyComms/pyCommsLib.cpp
+
1
−
0
View file @
e7522bf1
#include
"pyCommsLib.h"
#include
"Arduino.h"
void
send_msg
(
String
*
msgName
,
String
*
msg
,
int
numOfMsg
)
{
// If we have the same number of data compared to the message
String
payload
=
"<"
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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