Added preamble to frames
This commit is contained in:
parent
46793e0468
commit
77bd02f0ed
|
@ -0,0 +1,34 @@
|
||||||
|
#!/usr/bin/env python2
|
||||||
|
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
from sss7bus import SSS7Bus
|
||||||
|
|
||||||
|
|
||||||
|
bus = SSS7Bus('/dev/ttyUSB0')
|
||||||
|
|
||||||
|
framecount = 0
|
||||||
|
lost = 0
|
||||||
|
|
||||||
|
while True:
|
||||||
|
bus.send_message("Hello World!")
|
||||||
|
|
||||||
|
more = True
|
||||||
|
while more:
|
||||||
|
msg = bus.read_message()
|
||||||
|
print msg
|
||||||
|
print framecount
|
||||||
|
frame = int(msg.split(':')[1])
|
||||||
|
delta = frame - framecount
|
||||||
|
if delta > 1 and framecount <> 0:
|
||||||
|
lost += delta
|
||||||
|
|
||||||
|
framecount = frame
|
||||||
|
|
||||||
|
if frame % 100 == 0 and frame > 0:
|
||||||
|
print "Lost Frames: %d %d %f" % (frame, lost, 100.0 * lost / frame)
|
||||||
|
|
||||||
|
more = bus.has_message()
|
||||||
|
|
||||||
|
time.sleep((500.0 + random.randint(0,500)) / 1000.0)
|
|
@ -0,0 +1,20 @@
|
||||||
|
#!/usr/bin/env python2
|
||||||
|
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
from sss7bus import SSS7Bus
|
||||||
|
|
||||||
|
|
||||||
|
bus = SSS7Bus('/dev/ttyUSB1')
|
||||||
|
|
||||||
|
counter = 0
|
||||||
|
|
||||||
|
while True:
|
||||||
|
bus.send_message("Timestamp:%d:%d" % (counter, time.time()))
|
||||||
|
while bus.has_message():
|
||||||
|
bus.read_message()
|
||||||
|
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
time.sleep((500.0 + random.randint(0,500)) / 1000.0)
|
|
@ -0,0 +1,3 @@
|
||||||
|
crcmod==1.7
|
||||||
|
pyserial==2.7
|
||||||
|
wheel==0.24.0
|
|
@ -7,13 +7,15 @@ from crcmod.predefined import PredefinedCrc
|
||||||
|
|
||||||
|
|
||||||
# Timings in bit times
|
# Timings in bit times
|
||||||
TIMEOUT = 24
|
TIMEOUT = 48
|
||||||
CLEARCHANNEL = 48
|
CLEARCHANNEL = 2 * TIMEOUT
|
||||||
|
|
||||||
|
|
||||||
class SSS7Bus(object):
|
class SSS7Bus(object):
|
||||||
|
|
||||||
def __init__(self, port, baudrate=9600):
|
def __init__(self, port, baudrate=9600):
|
||||||
|
random.seed()
|
||||||
|
|
||||||
self._bit_time = 1.0 / baudrate
|
self._bit_time = 1.0 / baudrate
|
||||||
|
|
||||||
self._serial = serial.Serial(port, baudrate, timeout=self._bit_time * TIMEOUT)
|
self._serial = serial.Serial(port, baudrate, timeout=self._bit_time * TIMEOUT)
|
||||||
|
@ -35,15 +37,15 @@ class SSS7Bus(object):
|
||||||
self._flush_input_buffer()
|
self._flush_input_buffer()
|
||||||
|
|
||||||
self._serial.timeout = self._bit_time * CLEARCHANNEL
|
self._serial.timeout = self._bit_time * CLEARCHANNEL
|
||||||
self._debug("Checking if bus is idle for %f seconds" % (self._bit_time * CLEARCHANNEL))
|
self._debug("Checking if bus is idle for %f seconds" % (self._serial.timeout))
|
||||||
|
|
||||||
length_byte = self._serial.read(1)
|
first_byte = self._serial.read(1)
|
||||||
if len(length_byte) == 0:
|
if len(first_byte) == 0:
|
||||||
self._debug("Bus seems idle")
|
self._debug("Bus seems idle")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
self._debug("Bus is not idle, reading frames")
|
self._debug("Bus is not idle, reading frames")
|
||||||
self._read_frame_rest(length_byte)
|
self._read_frame_rest(first_byte)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -52,15 +54,34 @@ class SSS7Bus(object):
|
||||||
self._debug("Trying to read a frame from the bus")
|
self._debug("Trying to read a frame from the bus")
|
||||||
self._serial.timeout = timeout=self._bit_time * TIMEOUT
|
self._serial.timeout = timeout=self._bit_time * TIMEOUT
|
||||||
|
|
||||||
length_byte = self._serial.read(1)
|
first_byte = self._serial.read(1)
|
||||||
if len(length_byte) == 0:
|
if len(first_byte) == 0:
|
||||||
self._debug("Timeout reading frame length")
|
self._debug("Timeout reading first byte")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return self._read_frame_rest(length_byte)
|
return self._read_frame_rest(first_byte)
|
||||||
|
|
||||||
|
|
||||||
def _read_frame_rest(self, length_byte):
|
def _read_frame_rest(self, first_byte):
|
||||||
|
if ord(first_byte) <> 0xAA:
|
||||||
|
self._debug("Wrong first byte: %s" % hex(ord(first_byte)))
|
||||||
|
return False
|
||||||
|
|
||||||
|
second_byte = self._serial.read(1)
|
||||||
|
if len(second_byte) == 0:
|
||||||
|
self._debug("Timeout reading second byte")
|
||||||
|
return False
|
||||||
|
|
||||||
|
if ord(second_byte) <> 0xFE:
|
||||||
|
self._debug("Wrong second byte")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
length_byte = self._serial.read(1)
|
||||||
|
if len(length_byte) == 0:
|
||||||
|
self._debug("Timeout reading length byte")
|
||||||
|
return False
|
||||||
|
|
||||||
length = ord(length_byte) - 1 # we read the size length byte already
|
length = ord(length_byte) - 1 # we read the size length byte already
|
||||||
|
|
||||||
self._serial.timeout = timeout=self._bit_time * TIMEOUT
|
self._serial.timeout = timeout=self._bit_time * TIMEOUT
|
||||||
|
@ -103,7 +124,6 @@ class SSS7Bus(object):
|
||||||
for byte in frame:
|
for byte in frame:
|
||||||
result = self._send_byte(byte)
|
result = self._send_byte(byte)
|
||||||
if not result:
|
if not result:
|
||||||
self._serial.flushInput()
|
|
||||||
self._debug("Sending frame failed with collision")
|
self._debug("Sending frame failed with collision")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -119,12 +139,12 @@ class SSS7Bus(object):
|
||||||
crc16.update(msg)
|
crc16.update(msg)
|
||||||
msg_crc = crc16.digest()
|
msg_crc = crc16.digest()
|
||||||
|
|
||||||
frame = chr(len(msg) + 3) + msg + msg_crc
|
frame = chr(0xAA) + chr(0xFE) + chr(len(msg) + 3) + msg + msg_crc
|
||||||
|
|
||||||
result = self._send_frame(frame)
|
result = self._send_frame(frame)
|
||||||
|
|
||||||
while not result:
|
while not result:
|
||||||
backoff = 8 * self._bit_time * (priority + random.randint(1,5))
|
backoff = TIMEOUT * self._bit_time * (4 + priority + random.randint(1,5))
|
||||||
self._debug("Collision occured backing off %f" % backoff)
|
self._debug("Collision occured backing off %f" % backoff)
|
||||||
time.sleep(backoff)
|
time.sleep(backoff)
|
||||||
result = self._send_frame(frame)
|
result = self._send_frame(frame)
|
||||||
|
|
Loading…
Reference in New Issue