DeaD_EyE Mar 7th, 2019 (edited) 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- #!/usr/bin/env python3
- This script reads data from K-XC1 (https://www.rfbeam.ch/product?id=24)
- and sends the data as zmq_publisher to connected clients.
- The data is sent as bytes and have to be interpreted on the receiver side.
- The struct is: '>512h'
- Subscriber: https://pastebin.com/ueHWgw2a
- import serial
- import zmq
- SYNC_LEN = 8
- DATA_LEN = 1024
- FRAME_LEN = SYNC_LEN + DATA_LEN
- buffer = bytearray(FRAME_LEN)
- view = memoryview(buffer)
- sync = bytearray([0x24, 0x02, 0xa2, 0xe1, 0x5a, 0xd6, 0x19, 0x73])
- def reader(port):
- with serial.Serial(port, baudrate=921600) as ser:
- while True:
- idx = buffer.find(sync)
- if idx > 0:
- ser.read(FRAME_LEN + idx)
- elif idx == 0:
- yield buffer[SYNC_LEN:]
- def sender(serial_port, publisher_address, topic):
- with zmq.Context() as ctx:
- sock = ctx.socket(zmq.PUB)
- for frame in reader(serial_port):
- sock.send_multipart([topic, frame])
- if __name__ == '__main__':
- port = '/dev/ttyAMA0'
- address = 'tcp://192.168.0.254:5050'
- sender(port, address, b'sensor')
RAW Paste Data