65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
import socket
|
|
import sys
|
|
import time
|
|
import serial
|
|
|
|
#ser = serial.Serial(
|
|
# port='/dev/XPort',
|
|
# baudrate = 9600,
|
|
# parity=serial.PARITY_NONE,
|
|
# stopbits=serial.STOPBITS_ONE,
|
|
# bytesize=serial.EIGHTBITS,
|
|
# timeout=2
|
|
#)
|
|
|
|
# Create a TCP/IP socket
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
# Bind the socket to the port
|
|
server_address = ('localhost', 10000)
|
|
print >>sys.stderr, 'starting up on %s port %s' % server_address
|
|
sock.bind(server_address)
|
|
# Listen for incoming connections
|
|
sock.listen(1)
|
|
|
|
while True:
|
|
# Wait for a connection
|
|
print >>sys.stderr, 'waiting for a connection'
|
|
connection, client_address = sock.accept()
|
|
try:
|
|
print >>sys.stderr, 'connection from', client_address
|
|
# Receive the data in small chunks and retransmit it
|
|
while True:
|
|
data = connection.recv(16)
|
|
print >>sys.stderr, 'received "%s"' % data
|
|
if data:
|
|
line = data.split("\r\n")
|
|
print >>sys.stderr, 'start "%s"' % line[0]
|
|
if line[0]=="status":
|
|
x = ''
|
|
ser =serial.Serial('/dev/XPort',9600)
|
|
ser.flushInput()
|
|
time.sleep(0.5)
|
|
x = ser.readline()
|
|
connection.sendall(x)
|
|
ser.close()
|
|
if line[0]=='F1':
|
|
my_s = ''
|
|
ser =serial.Serial('/dev/XPort',9600)
|
|
ser.write("1")
|
|
ser.flush()
|
|
ser.flushInput()
|
|
time.sleep(2)
|
|
my_s = ser.readline();
|
|
lx = [x.strip() for x in my_s.split(',')]
|
|
print >>sys.stderr, ' lx ', lx
|
|
connection.sendall(my_s)
|
|
ser.close()
|
|
else:
|
|
print >>sys.stderr, 'no more data from', client_address
|
|
break
|
|
|
|
finally:
|
|
# Clean up the connection
|
|
ser.close()
|
|
connection.close() |