-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patharcom.py
218 lines (169 loc) · 6.59 KB
/
arcom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# !/usr/bin/python3
# -*- coding: utf-8 -*-
import logging
import serial
import numpy as np
import struct
logger = logging.getLogger(__name__)
class DataType(object):
def __init__(self, name, size):
self.name = name
self.size = size
def __str__(self):
return self.name
class ArduinoTypes(object):
BYTE = DataType("byte", 1)
CHAR = DataType("char", 1)
UINT8 = DataType("uint8", 1)
INT16 = DataType("int16", 2)
UINT16 = DataType("uint16", 2)
UINT32 = DataType("uint32", 4)
UINT64 = DataType("uint64", 8)
FLOAT32 = DataType("float32", 4)
FLOAT64 = DataType("float64", 8)
@staticmethod
def get_array(array, dtype):
if dtype == ArduinoTypes.CHAR or dtype == ArduinoTypes.UINT8:
return ArduinoTypes.get_uint8_array(array)
elif dtype == ArduinoTypes.UINT16:
return ArduinoTypes.get_uint16_array(array)
elif dtype == ArduinoTypes.UINT32 or dtype == ArduinoTypes.FLOAT:
return ArduinoTypes.get_uint32_array(array)
else:
return None
@staticmethod
def get_uint8_array(array):
return np.array(array, dtype=str(ArduinoTypes.UINT8)).tobytes()
@staticmethod
def get_int16_array(array):
return np.array(array, dtype=str(ArduinoTypes.INT16)).tobytes()
@staticmethod
def get_uint16_array(array):
return np.array(array, dtype=str(ArduinoTypes.UINT16)).tobytes()
@staticmethod
def get_uint32_array(array):
return np.array(array, dtype=str(ArduinoTypes.UINT32)).tobytes()
@staticmethod
def get_float(value):
return struct.pack("<f", value)
@staticmethod
def cvt_float32(message_bytes):
return struct.unpack("<f", message_bytes)[0]
@staticmethod
def cvt_float64(message_bytes):
return struct.unpack("<d", message_bytes)[0]
@staticmethod
def cvt_int64(message_bytes):
return int.from_bytes(message_bytes, byteorder="little")
@staticmethod
def cvt_uint32(message_bytes):
return struct.unpack("<L", message_bytes)[0]
@staticmethod
def cvt_uint64(message_bytes):
return struct.unpack("<Q", message_bytes)[0]
class ArCOM(object):
"""
ArCOM is an interface to simplify data transactions between Arduino and Python.
"""
def open(self, serial_port, baudrate=115200, timeout=1):
"""
Open serial connection
:param serialPortName:
:param baudRate:
:return:
"""
self.serial_object = serial.Serial(
serial_port, baudrate=baudrate, timeout=timeout
)
return self
def close(self):
"""
Close serial connection
:return:
"""
self.serial_object.close()
def bytes_available(self):
"""
:return:
"""
return self.serial_object.inWaiting()
##############################################################
## WRITE #####################################################
##############################################################
def write_char(self, value):
self.serial_object.write(str.encode(value))
def write_array(self, array):
self.serial_object.write(array)
##############################################################
## READ BYTE #################################################
##############################################################
def read_byte(self):
message_bytes = self.serial_object.read(ArduinoTypes.BYTE.size)
return message_bytes
def read_char(self):
message_bytes = self.serial_object.read(ArduinoTypes.CHAR.size)
return message_bytes.decode("utf-8")
def read_uint8(self):
message_bytes = self.serial_object.read(ArduinoTypes.UINT8.size)
# logger.debug("Read %s bytes: %s", len(message_bytes), message_bytes)
message = int.from_bytes(message_bytes, byteorder="little")
return message
def read_uint16(self):
message_bytes = self.serial_object.read(ArduinoTypes.UINT16.size)
# logger.debug("Read %s bytes: %s", ArduinoTypes.UINT16.size, message_bytes)
message = int.from_bytes(message_bytes, byteorder="little")
return message
def read_uint32(self):
message_bytes = self.serial_object.read(ArduinoTypes.UINT32.size)
# logger.debug("Read %s bytes: %s", ArduinoTypes.UINT32.size, message_bytes)
message = int.from_bytes(message_bytes, byteorder="little")
return message
def read_uint64(self):
message_bytes = self.serial_object.read(ArduinoTypes.UINT64.size)
# logger.debug("Read %s bytes: %s", ArduinoTypes.UINT32.size, message_bytes)
message = int.from_bytes(message_bytes, byteorder="little")
return message
def read_float32(self):
message_bytes = self.serial_object.read(ArduinoTypes.FLOAT32.size)
# logger.debug("Read %s bytes: %s", ArduinoTypes.UINT32.size, message_bytes)
message = struct.unpack("<f", message_bytes)
return message[0]
##############################################################
## READ ARRAY ################################################
##############################################################
def read_bytes_array(self, array_len=1):
message_array = []
for pos in range(0, array_len):
message_bytes = self.read_byte()
message_array.append(message_bytes)
return message_array
def read_char_array(self, array_len=1):
message_array = []
for pos in range(0, array_len):
message_bytes = self.read_char()
message_array.append(message_bytes)
return message_array
def read_uint8_array(self, array_len=1):
message_array = []
for pos in range(0, array_len):
message_bytes = self.read_uint8()
message_array.append(message_bytes)
return message_array
def read_uint16_array(self, array_len=1):
message_array = []
for pos in range(0, array_len):
message_bytes = self.read_uint16()
message_array.append(message_bytes)
return message_array
def read_uint32_array(self, array_len=1):
message_array = []
for pos in range(0, array_len):
message_bytes = self.read_uint32()
message_array.append(message_bytes)
return message_array
def read_float32_array(self, array_len=1):
message_array = []
for pos in range(0, array_len):
message_bytes = self.read_float32()
message_array.append(message_bytes)
return message_array