-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserialwatch.py
More file actions
executable file
·99 lines (85 loc) · 2.82 KB
/
serialwatch.py
File metadata and controls
executable file
·99 lines (85 loc) · 2.82 KB
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
#!/usr/bin/python2
# do it as a class
# do it under systemd and specify differenc actions for the different ports
# hoping no need for log file as systemd 'prints' to journal - may not be the case
import time, os, gobject, dbus
from dbus.mainloop.glib import DBusGMainLoop
from serial import Serial
from fcntl import ioctl
from termios import (
TIOCMIWAIT,
TIOCM_RNG,
TIOCM_DSR,
TIOCM_CD,
TIOCM_CTS
)
#dev_ser = '/dev/ttyUSB0'
dev_ser = '/dev/ttyS0'
#logfile = "/var/log/serialwatch.log"
ser = Serial(dev_ser)
wait_signals = (TIOCM_RNG |
TIOCM_DSR |
TIOCM_CD |
TIOCM_CTS)
class SerialWatch():
def getdbus (self):
#dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
print("initialising dbus")
self.bus = dbus.SessionBus()
try:
#print("attempting to connect to MessagePlayer")
self.proxy = self.bus.get_object('com.threedradio.MessagePlayer',
'/MyDbus')
#print("setting the dbus control interface for MessagePlayer")
self.control_interface = dbus.Interface(self.proxy,
'com.threedradio.MessagePlayer')
except:
self.control_interface = ""
print("Failure to connect to ThreedPlayer via DBus")
return self.control_interface
def logging (self, logmessage):
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
self.logentry = timestamp + ": " + logmessage
#f = open(logfile, 'w')
#f.write(logentry)
#f.close()
print self.logentry
def RI(self):
logmessage = "Got the RI"
print(logmessage)
self.logging(logmessage)
def DSR(self):
logmessage = "Got the DSR"
print(logmessage)
self.logging(logmessage)
def CD(self):
logmessage = "Got the CD"
print(logmessage)
self.logging(logmessage)
control_interface = self.getdbus()
if control_interface:
print("Processing signal and attempting to trigger MessagePlayer")
control_interface.signal_received()
else:
print("Looks like the threedplayer is not running or script is buggy")
def CTS(self):
logmessage = "Got the CTS"
print(logmessage)
self.logging(logmessage)
def run(self):
print("Starting to watch")
self.getdbus()
while True:
ioctl(ser.fd, TIOCMIWAIT, wait_signals)
if ser.getRI():
self.RI()
if ser.getDSR():
self.DSR()
if ser.getCD():
self.CD()
if ser.getCTS():
self.CTS()
#self.control_interface.signal_received()
time.sleep(0.5)
sw = SerialWatch()
sw.run()