Advertisement
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:
- ser.readinto(buffer)
- 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)
- sock.bind(publisher_address)
- 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')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement