Skip to content

Commit

Permalink
Add gitignore
Browse files Browse the repository at this point in the history
Add automatic ESU finding code
Add exception handlers in the main loop to make the system transparently restart if something dies
  • Loading branch information
ndholmes committed Jan 3, 2018
1 parent 97773e7 commit 4962846
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*~
*.pyc
77 changes: 55 additions & 22 deletions esu-bridge.py
Expand Up @@ -4,41 +4,74 @@
import mrbus
import sys
import time
import socket
import MRBusThrottle

import netUtils

statusInterval = 2
baseAddress = 0xD4

mrbee = mrbus.mrbus("/dev/ttyUSB2", baseAddress, logall=True, logfile=sys.stdout, busType='mrbee')
cmdStn = esu.ESUConnection()
cmdStn.connect('192.168.7.191')
# Big loop - runs as long as the program is alive
while 1:

# Initialization loop - runs until both ESU and MRBus are connected
while 1:
try:
throttles = { }
print "Looking for ESU CabControl command station"
esuIP = ""
esuIP = netUtils.esuFind()
if esuIP is None:
print "No command station found, waiting and retrying..."
sleep(1)
continue

cmdStn = esu.ESUConnection()
cmdStn.connect(esuIP)

mrbee = mrbus.mrbus("/dev/ttyUSB2", baseAddress, logall=True, logfile=sys.stdout, busType='mrbee')
break

throttles = { }
except(KeyboardInterrupt):
cmdStn.disconnect()
mrbee.disconnect()
sys.exit()
except:
cmdStn.disconnect()
mrbee.disconnect()
continue

lastStatusTime = time.time()

lastStatusTime = time.time()

while 1:
pkt = mrbee.getpkt()
# Main Run Loop - runs until something weird happens
while 1:
try:
pkt = mrbee.getpkt()

if time.time() > lastStatusTime + statusInterval:
mrbee.sendpkt(0xFF, [ord('A')])
lastStatusTime = time.time()
if time.time() > lastStatusTime + statusInterval:
mrbee.sendpkt(0xFF, [ord('A')])
lastStatusTime = time.time()

if pkt is None:
continue
if pkt is None:
continue

# Bypass anything that doesn't look like a throttle packet
if pkt.cmd != 0x53 or len(pkt.data) != 10 or baseAddress != pkt.dest:
continue
# Bypass anything that doesn't look like a throttle packet
if pkt.cmd != 0x53 or len(pkt.data) != 10 or baseAddress != pkt.dest:
continue

if pkt.src not in throttles:
throttles[pkt.src] = MRBusThrottle.MRBusThrottle()
if pkt.src not in throttles:
throttles[pkt.src] = MRBusThrottle.MRBusThrottle()

throttles[pkt.src].update(cmdStn, pkt)


throttles[pkt.src].update(cmdStn, pkt)

cmdStn.disconnect()
except (KeyboardInterrupt):
cmdStn.disconnect()
mrbee.disconnect()
sys.exit()
except:
print "Caught some sort of exception, restarting the whole thing"
cmdStn.disconnect()
mrbee.disconnect()
break

8 changes: 8 additions & 0 deletions mrbus.py
Expand Up @@ -94,6 +94,8 @@ def __init__(self, port, addr, logfile=None, logall=False, extra=False):
self.addr=addr
# self.buf=deque()

def disconnect(self):
self.serial.close()

def log(self, error, msg):
if not self.logfile:
Expand Down Expand Up @@ -193,6 +195,9 @@ def log(self, error, msg):
s=" log:"
self.logfile.write(s+repr(msg)+'\n')

def disconnect(self):
self.serial.close()

def getpkt(self):

while 1:
Expand Down Expand Up @@ -353,6 +358,9 @@ def mrbusCRC16Update(crc, a):
return (crc16_h<<8) | crc16_l

class mrbus(object):
def disconnect(self):
self.mrbs.disconnect()

def __init__(self, port, addr=None, logfile=None, logall=False, extra=False, busType='mrbus'):
if type(port)==str:
port = serial.Serial(port, 115200, rtscts=True)
Expand Down
36 changes: 36 additions & 0 deletions netUtils.py
@@ -0,0 +1,36 @@
from time import time
import socket

def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except:
IP = '127.0.0.1'
finally:
s.close()
return IP

def testPort(ip, port, timeout=0.3):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
s.connect((ip, port))
return 1
except(socket.timeout,socket.error):
return 0

def esuFind():
defaultIP = get_ip()
o1,o2,o3,o4 = defaultIP.split('.')
print "Starting Scan"
for i in range(0,254):
scanIP = "%s.%s.%s.%d" % (o1, o2, o3, i)
result = testPort(scanIP, 15471, 0.01)
if result:
print "IP %s has ESU port open" % (scanIP)
return scanIP
return None

0 comments on commit 4962846

Please sign in to comment.