Skip to content
Snippets Groups Projects
Commit f78b1d7a authored by Peter Švihra's avatar Peter Švihra
Browse files

fixed bytearray access

- getByteArray func for all dataclasses (removed decoding form CommsFormat)
- naming convention updates
parent d2ebfd9b
Branches
No related merge requests found
......@@ -46,6 +46,10 @@ class BaseFormat():
def getDict(self) -> Dict:
return asdict(self)
def getByteArray(self) -> bytearray:
self.toByteArray()
return self._byteArray
# at the minute not generalised. needs to be overridden
def fromByteArray(self, byteArray) -> None:
......
......@@ -41,8 +41,8 @@ class CommsControl():
self._timeLastTransmission = int(round(time.time() * 1000))
# packet counter checker
self._sequenceSend = 0
self._sequenceReceive = 0
self._sequence_send = 0
self._sequence_receive = 0
# initialize of the multithreading
self._lockSerial = threading.Lock()
......@@ -103,11 +103,11 @@ class CommsControl():
with self._dvlock:
if len(queue) > 0:
logging.debug(f'Queue length: {len(queue)}')
currentTime = int(round(time.time() * 1000))
if currentTime > (self._timeLastTransmission + timeout):
current_time = int(round(time.time() * 1000))
if current_time > (self._timeLastTransmission + timeout):
with self._lockSerial:
self._timeLastTransmission = currentTime
queue[0].setSequenceSend(self._sequenceSend)
self._timeLastTransmission = current_time
queue[0].setSequenceSend(self._sequence_send)
self.sendPacket(queue[0])
def getQueue(self, payload_type):
......@@ -150,11 +150,11 @@ class CommsControl():
tmp_comms = CommsFormat.commsFromBytes(decoded)
if tmp_comms.compareCrc():
control = tmp_comms.getData()[tmp_comms.getControl()+1]
self._sequenceReceive = (tmp_comms.getData()[tmp_comms.getControl()] >> 1) & 0x7F
self._sequence_receive = (tmp_comms.getData()[tmp_comms.getControl()] >> 1) & 0x7F
# get type of payload and corresponding queue
payload_type = self.getInfoType(tmp_comms.getData()[tmp_comms.getAddress()])
tmpQueue = self.getQueue(payload_type)
queue = self.getQueue(payload_type)
# get type of packet
ctrl_flag = control & 0x0F
......@@ -164,9 +164,9 @@ class CommsControl():
elif ctrl_flag == 0x01:
logging.debug("Received ACK")
# received ACK
self.finishPacket(tmpQueue)
self.finishPacket(queue)
else:
sequenceReceive = ((control >> 1) & 0x7F) + 1
sequence_receive = ((control >> 1) & 0x7F) + 1
address = tmp_comms.getData()[tmp_comms.getAddress():tmp_comms.getControl()]
if self.receivePacket(payload_type, tmp_comms):
......@@ -175,7 +175,7 @@ class CommsControl():
else:
logging.debug("Preparing NACK")
comms_response = CommsFormat.CommsNACK(address = address)
comms_response.setSequenceReceive(sequenceReceive)
comms_response.setSequenceReceive(sequence_receive)
self.sendPacket(comms_response)
self._received.clear()
......@@ -192,8 +192,7 @@ class CommsControl():
elif payload_type == CommsCommon.PAYLOAD_TYPE.DATA:
tmp_comms = CommsFormat.generateData(payload)
else:
return False
tmp_comms.setInformation(payload)
return False
with self._dvlock:
queue = self.getQueue(payload_type)
......@@ -212,8 +211,8 @@ class CommsControl():
with self._dvlock:
if len(queue) > 0:
# 0x7F to deal with possible overflows (0 should follow after 127)
if ((queue[0].getSequenceSend() + 1) & 0x7F) == self._sequenceReceive:
self._sequenceSend = (self._sequenceSend + 1) % 128
if ((queue[0].getSequenceSend() + 1) & 0x7F) == self._sequence_receive:
self._sequence_send = (self._sequence_send + 1) % 128
queue.popleft()
except:
logging.debug("Queue is probably empty")
......@@ -317,20 +316,11 @@ if __name__ == "__main__" :
except:
pass
comms_ctrl = CommsControl(port = port)
example = Dependant(comms_ctrl)
comms = CommsControl(port = port)
example = Dependant(comms)
payload_send = CommsCommon.CommandFormat(CommsCommon.CMD_TYPE.GENERAL.value, CommsCommon.CMD_GENERAL.START.value, param=0)
comms_ctrl.writePayload(payload_send)
# commsCtrl.payloadrecv = "testpacket1"
# commsCtrl.payloadrecv = "testpacket2"
# LEDs = [3,5,7]
# for _ in range(30):
# for led in LEDs:
# commsCtrl.registerData(led)
# time.sleep(5)
# while True:
# time.sleep(60)
\ No newline at end of file
cmd = CommsCommon.CommandFormat(cmd_type = CommsCommon.CMD_TYPE.GENERAL.value, cmd_code = CommsCommon.CMD_GENERAL.START.value, param=0)
time.sleep(4)
comms.writePayload(cmd)
print('sent cmd start')
print(cmd)
\ No newline at end of file
......@@ -12,32 +12,32 @@ def commsFromBytes(byteArray):
return comms
def generateAlarm(payload):
comms = CommsFormat(infoSize = payload.getSize(), address = 0xC0)
comms.setInformation(payload)
comms = CommsFormat(info_size = payload.getSize(), address = 0xC0)
comms.setInformation(payload.getByteArray())
return comms
def generateCmd(payload):
comms = CommsFormat(infoSize = payload.getSize(), address = 0x80)
comms.setInformation(payload)
comms = CommsFormat(info_size = payload.getSize(), address = 0x80)
comms.setInformation(payload.getByteArray())
return comms
def generateData(payload):
comms = CommsFormat(infoSize = payload.getSize(), address = 0x40)
comms.setInformation(payload)
comms = CommsFormat(info_size = payload.getSize(), address = 0x40)
comms.setInformation(payload.getByteArray())
return comms
# basic format based on HDLC
class CommsFormat:
def __init__(self, infoSize = 0, address = 0x00, control = [0x00, 0x00]):
self._data = bytearray(7 + infoSize)
self._infoSize = infoSize
def __init__(self, info_size = 0, address = 0x00, control = [0x00, 0x00]):
self._data = bytearray(7 + info_size)
self._info_size = info_size
self._crc = None
self.assignBytes(self.getStart() , bytes([0x7E]) , calcCrc = False)
self.assignBytes(self.getAddress(), bytes([address]), calcCrc = False)
self.assignBytes(self.getControl(), bytes(control) , calcCrc = False)
self.assignBytes(self.getStop() , bytes([0x7E]) , calcCrc = False)
self.assignBytes(self.getStart() , bytes([0x7E]) , calc_crc = False)
self.assignBytes(self.getAddress(), bytes([address]), calc_crc = False)
self.assignBytes(self.getControl(), bytes(control) , calc_crc = False)
self.assignBytes(self.getStop() , bytes([0x7E]) , calc_crc = False)
self.generateCrc()
......@@ -50,9 +50,9 @@ class CommsFormat:
def getInformation(self):
return 4
def getFcs(self):
return 4 + self._infoSize
return 4 + self._info_size
def getStop(self):
return 4 + self._infoSize + 2
return 4 + self._info_size + 2
def setAddress(self, address):
self.assignBytes(self.getAddress(), bytes([address]), 1)
......@@ -60,9 +60,9 @@ class CommsFormat:
def setControl(self, control):
self.assignBytes(self.getControl(), bytes(control), 2)
def setInformation(self, payload):
def setInformation(self, bytes_array):
# convert provided value
self.assignBytes(self.getInformation(), payload.byteArray)
self.assignBytes(self.getInformation(), bytes_array)
def setSequenceSend(self, value):
# sequence sent valid only for info frames (not supervisory ACK/NACK)
......@@ -84,17 +84,17 @@ class CommsFormat:
def getSequenceReceive(self):
return (self._data[self.getControl()] >> 1) & 0x7F
def assignBytes(self, start, values, calcCrc = True):
def assignBytes(self, start, values, calc_crc = True):
for idx in range(len(values)):
self._data[start + idx] = values[idx]
if calcCrc:
if calc_crc:
self.generateCrc()
# generate checksum
def generateCrc(self, assign = True):
self._crc = libscrc.x25(bytes(self._data[self.getAddress():self.getFcs()])).to_bytes(2, byteorder='little')
if assign:
self.assignBytes(self.getFcs(), self._crc, calcCrc = False)
self.assignBytes(self.getFcs(), self._crc, calc_crc = False)
def compareCrc(self):
self.generateCrc(False)
......@@ -105,12 +105,12 @@ class CommsFormat:
def getData(self):
return self._data
def copyData(self, dataArray):
self.copyBytes(dataArray.to_bytes(self._infoSize, byteorder='little'))
def copyData(self, data_array):
self.copyBytes(data_array.to_bytes(self._info_size, byteorder='little'))
def copyBytes(self, bytesArray):
self._infoSize = len(bytesArray) - 7
self._data = bytesArray
def copyBytes(self, bytes_array):
self._info_size = len(bytes_array) - 7
self._data = bytes_array
# ACK specific formating
class CommsACK(CommsFormat):
......
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