This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainlinkSerial.py
153 lines (129 loc) · 4.85 KB
/
trainlinkSerial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'''
Handles all serial communications
Copyright (C) 2020 TrainLink Organisation (matt-hu)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
'''
import trainlinkUtils
import serial
class comms:
'''Manages the serial communications of trainlink'''
ser = None
prevPacket = {}
line = ""
oldLine = ""
emulator = False
logfile = None
def __init__(self, port, logfile):
self.logfile = logfile
try:
self.ser = serial.Serial(baudrate=115200, port=port, timeout=2)
self.ser.close()
self.ser.open()
except:
logfile.log("Using Emulator")
'''
print("Using Emulator")
'''
self.emulator = True
'''
def updateCabs(self, cabSpeeds, cabDirections):
for cab in cabSpeeds:
address = int(cab)
speed = int(cabSpeeds[cab])
direction = int(cabDirections[cab])
packet = b'<t 1 %d %d %d>'%(address, speed, direction)
try:
if packet != self.prevPacket[address]:
self.write(packet)
self.prevPacket[address] = packet
except KeyError:
self.write(packet)
self.prevPacket[address] = packet
'''
def updateCabs(self, cabs):
for cab in cabs:
address = cabs[cab].getAddress()
speed = cabs[cab].getSpeed()
direction = cabs[cab].getDirection()
packet = b'<t 1 %d %d %d>'%(int(address), int(speed), int(direction))
try:
if packet != self.prevPacket[address]:
self.write(packet)
self.prevPacket[address] = packet
except KeyError:
self.write(packet)
self.prevPacket[address] = packet
async def directCommand(self, packet):
packet = packet.encode('utf-8')
self.write(packet)
async def setPower(self, powerState):
logfile = self.logfile
try:
powerState = int(powerState)
if powerState == 1:
self.write(b'<1>')
elif powerState == 0:
self.write(b'<0>')
else:
logfile.log("Invalid power state", "w")
'''
print("Invalid power state")
'''
except ValueError:
logfile.log("Invalid power state", "w")
'''
print("Invalid power state")
'''
async def setFunction(self, cab, *args, **kwargs):
functionStates = kwargs.get('functionStates', None)
function = kwargs.get('function', None)
state = kwargs.get('state', None)
legacyMode = True
if legacyMode:
bytes = trainlinkUtils.funcToBytes(functionStates, 0)
self.write(b'<f %d %d>'%(int(cab),bytes[0]))
bytes = trainlinkUtils.funcToBytes(functionStates, 1)
self.write(b'<f %d %d>'%(int(cab),bytes[0]))
bytes = trainlinkUtils.funcToBytes(functionStates, 2)
self.write(b'<f %d %d>'%(int(cab),bytes[0]))
bytes = trainlinkUtils.funcToBytes(functionStates, 3)
self.write(b'<f %d %d %d>'%(int(cab),bytes[0],bytes[1]))
bytes = trainlinkUtils.funcToBytes(functionStates, 4)
self.write(b'<f %d %d %d>'%(int(cab),bytes[0],bytes[1]))
else:
self.write(b'<F %s %s %s'%(cab,function,state))
def write(self, packet):
if self.emulator:
pass
else:
self.ser.write(packet)
def read(self, char):
line = self.ser.read_until(char)
return line
def startComms(self):
if not self.emulator:
line1 = self.ser.read_until(b'>')
line2 = self.ser.read_until(b'>')
line1 = line1.decode("utf-8")
line2 = line2.decode("utf-8")
return line1 + line2
def getLatest(self):
if self.line == self.oldLine:
return False
else:
self.oldLine = self.line
return self.line
def readInLoop(self):
if not self.emulator:
line = self.ser.read_until(b'>')
if line != '':
self.line = line