Commit 19b1556f authored by Pedro Henrique Kopper's avatar Pedro Henrique Kopper

Added new protobuf module

parent 347b7107
# Este arquivo define a interface a ser utilizada
device:
type: arduino
type: mock
location: "/dev/ttyUSB0"
channels:
- id: 0
......@@ -10,4 +10,4 @@ device:
- id: 1
name: "Corrente"
color: "#0000FF"
unit: "mA"
\ No newline at end of file
unit: "mA"
from serial import Serial
from interfaces.Protocol.sadapmap_pb2 import MultiChannelReading, ReqMultiChannelReading
class InstrumentInterface(object):
def __init__(self, logger, location, channels):
self.channels = channels
self.logger = logger
logger("[ARDUINO]", "Opening serial port...")
try:
self.ser = Serial(location, 115200, timeout=0.05)
except Exception as e:
logger("[ARDUINO]", "ERROR: Could not open serial port")
raise FileNotFoundError("Serial port not found") from e
logger("[ARDUINO]", "Board started, good luck!")
def read(self):
pass
def readAll(self):
self.ser.flushInput() # Limpeza do buffer de entrada
req = ReqMultiChannelReading()
[req.id.append(i['id']) for i in self.channels]
self.ser.flushInput()
self.ser.write(req.SerializeToString())
reading = MultiChannelReading()
data = self.ser.read(128)
try:
reading.ParseFromString(data)
except:
self.logger("[ARDUINO]", "Got invalid data")
return [0 for i in range(len(self.channels))]
if len(reading.channels) != len(self.channels):
self.logger("[ARDUINO]", "Got null data")
return [0 for i in range(len(self.channels))]
return [i.value for i in reading.channels]
\ No newline at end of file
......@@ -2,8 +2,10 @@
from interfaces.Mock import *
from interfaces.Arduino import *
from interfaces.InstrumentInterface import *
INTERFACES = {
'mock': Mock,
'arduino': Arduino
'arduino': Arduino,
'instrument-interface': InstrumentInterface,
}
......@@ -6,7 +6,7 @@ with serial.Serial("/dev/ttyUSB0", 115200, timeout=0.1) as ser:
print("Opened serial")
while True:
req = ReqMultiChannelReading()
req.id.append(0)
req.id.append(1)
ser.flushInput()
ser.write(req.SerializeToString())
reading = MultiChannelReading()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment