forked from Shellowb/BioMixer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include write_struct.py to control machine arduinos Can send and receive data.
- Loading branch information
Showing
2 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
biomixer_interface/biomixer_interface/arduino/write_struct.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# -*- coding: utf-8 -* | ||
|
||
import serial | ||
import struct | ||
from io import BytesIO | ||
|
||
|
||
class MachineCmd(object): | ||
""" | ||
define the structure of the data, little-endian | ||
h is for 2 byte integer, see the full list here https://docs.python.org/3/library/struct.html#format-characters | ||
add as many data types as you want to send to arduino | ||
""" | ||
|
||
_struct = struct.Struct('<hhhh') | ||
|
||
def __init__(self, port='/dev/ttyUSB0'): | ||
self.arduino = serial.Serial(port, 9600) | ||
self.buffer = BytesIO() | ||
self.d1 = 0 | ||
self.d2 = 0 | ||
self.d3 = 0 | ||
self.d4 = 0 | ||
|
||
def serialize(self): | ||
self.buffer.write(self._struct.pack(self.d1, self.d2, self.d3, self.d4)) | ||
|
||
@staticmethod | ||
def to_hex(data): | ||
return ":".join("{:02x}".format(c) for c in data) | ||
|
||
def set_values(self, d1=0, d2=0, d3=0, d4=0): | ||
pass | ||
|
||
def read(self): | ||
return bytearray(self.buffer.getvalue()) |