forked from jt-lab/trigger_handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger_handler.py
71 lines (54 loc) · 1.71 KB
/
trigger_handler.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
# Copyright (c) 2021 Jan Tünnermann. All rights reserved.
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
import subprocess
from flask import Flask
app = Flask(__name__)
# Configuration:
APP_HOST = '127.0.0.1' # Defaul is localhost
APP_PORT = 8000
PARALLEL = False # Whether to open a parallel connection
LPT_INTERFACE = 0xD050 # Address of the parallel port interface
TCP = True # Whether to open a TCP connection
TCP_HOST = '127.0.0.1'
TCP_PORT = 50000
@app.route('/trigger/parallel/<trigger_value>')
def parallel_trigger(trigger_value):
trigger_value = int(trigger_value)
if(PARALLEL == False):
status = '[Fail]'
else:
try:
io.Out32(LPT_INTERFACE, trigger_value)
status = '[Success]'
except:
status = '[Fail]'
msg = status + ' ' + 'Trigger ' + str(trigger_value)
return(msg)
@app.route('/trigger/tcp/<trigger_value>')
def tcp_trigger(trigger_value):
trigger_value = int(trigger_value)
if(TCP):
try:
tcp_socket.send(trigger_value.to_bytes(1, 'little'))
status = '[Success]'
except:
status = '[Fail]'
msg = status + ' ' + 'Trigger ' + str(trigger_value)
return(msg)
if __name__ == '__main__':
if(PARALLEL):
try:
from ctypes import windll
io = windll.LoadLibrary('inpoutx64')
io.Out32(LPT_INTERFACE, 0) # Set to zero
except:
print("[Fail] Could not load Windows parallel port interface.")
if(TCP):
try:
import socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.connect((TCP_HOST, TCP_PORT))
except:
print("[Fail] Cannot connect to TCP port")
app.run(host=APP_HOST, port=APP_PORT)