Skip to content

Instantly share code, notes, and snippets.

@jdecaron
Created October 22, 2011 00:30
Show Gist options
  • Save jdecaron/1305341 to your computer and use it in GitHub Desktop.
Save jdecaron/1305341 to your computer and use it in GitHub Desktop.
import serial
import socket
import string
import struct
import zlib
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 7050))
mreq = struct.pack("=4sl", socket.inet_aton("230.0.0.1"), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
arduino = serial.Serial("COM5")
def cut(s):
a = []
while len(s) > 190 - 1:
a.append(s[0:190-1])
s = s[190:len(s)]
if len(s) > 0:
a.append(s)
return a
def extract_company(s):
s = s[127:len(s)]
p = string.find(s, "\x00\x00")
if p != -1 and p > 0:
s = s[0:p]
s = string.replace(s, "\x00", "")
return s
def extract_stats(s):
#21
return s[21]
def is_client(client):
clients = ['AL_PMG', 'NOVELIS']
for c in clients:
if string.find(client, c) >= 0:
return 1
return 0
def remove_first_characters(s):
c = 0
while s[c] == "\x00":
c = c + 1
return s[c:len(s)]
c = 0
while True:
s = sock.recv(10240)
#f = open(str(c), 'w')
s = zlib.decompress(s[72:len(s)])
s2 = s
s = remove_first_characters(s)
s = cut(s)
for i in s:
if is_client(extract_company(i)):
stats = int(ord(extract_stats(i)))
if stats >= 1:
arduino.write('x')
else:
arduino.write(0)
#f.write(s2)
#f.close()
c = c + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment