Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import serial
import time
import sys
import socket
import datetime
s = None
sock = None
def setup():
global s
global sock
s = serial.Serial("/dev/ttyS0", 9600, timeout=1)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
def loop():
b = bytearray([0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79])
#print('q');
s.write(b)
time.sleep(1)
result = s.read(9)
#print('a', type(result))
msg = datetime.datetime.now().isoformat()+","+result.encode("hex")+","+str(ord(result[2])*256+ord(result[3]))+'\n'
sock.sendto(msg, ("192.168.1.4", 0xC0C0))
sock.sendto(msg, ("192.168.1.4", 0xC0C1))
for i in result:
sys.stdout.write(hex(ord(i)))
sys.stdout.write(' ')
sys.stdout.write('\n')
sys.stdout.flush
time.sleep(14)
if __name__ == '__main__':
setup()
while True:
loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment