Skip to content

Commit 58becf0

Browse files
committed
update demo/Server.py file
1 parent dfbfd2b commit 58becf0

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

demo/Server.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
import struct
1111
import RPi.GPIO as GPIO
1212

13+
"""
14+
Instructions for use:
15+
16+
Please change the parameters passed in MycobotServer in line 141 according to your model.
17+
18+
19+
The default model is the 280PI.
20+
21+
The default parameters are:
22+
23+
serial_num: /dev/ttyAMA0
24+
25+
baud: 1000000
26+
27+
28+
"""
1329

1430
def get_logger(name):
1531
logger = logging.getLogger(name)
@@ -33,13 +49,24 @@ def get_logger(name):
3349

3450
class MycobotServer(object):
3551

36-
def __init__(self, host, port):
52+
def __init__(self, host, port, serial_num = "/dev/ttyAMA0", baud = 1000000):
53+
"""Server class
54+
55+
Args:
56+
host: server ip address.
57+
port: server port.
58+
serial_num: serial number of the robot.The default is /dev/ttyAMA0.
59+
baud: baud rate of the serial port.The default is 1000000.
60+
61+
"""
3762
try:
3863
GPIO.setwarnings(False)
3964
except:
4065
pass
4166
self.logger = get_logger("AS")
4267
self.mc = None
68+
self.serial_num = serial_num
69+
self.baud = baud
4370
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4471
self.s.bind((host, port))
4572
print("Binding succeeded!")
@@ -51,7 +78,6 @@ def connect(self):
5178
try:
5279
print("waiting connect!------------------")
5380
conn, addr = self.s.accept()
54-
port_baud = []
5581
while True:
5682
try:
5783
print("waiting data--------")
@@ -62,13 +88,9 @@ def connect(self):
6288
break
6389
res = b'1'
6490
command = command.replace(" ", "")
65-
if len(port_baud) < 3:
66-
67-
port_baud.append(command)
68-
if len(port_baud) == 3:
69-
self.mc = serial.Serial(
70-
port_baud[0], port_baud[1], timeout=float(port_baud[1]))
71-
port_baud.append(1)
91+
if self.mc is None:
92+
self.mc = serial.Serial(
93+
self.serial_num, self.baud, timeout=0.1)
7294
else:
7395
self.logger.info(command)
7496
command = self.re_data_2(command)
@@ -101,6 +123,7 @@ def connect(self):
101123
except Exception as e:
102124
self.logger.error(str(e))
103125
conn.close()
126+
self.mc.close()
104127

105128
def write(self, command):
106129
self.mc.write(command)
@@ -127,4 +150,4 @@ def re_data_2(self, command):
127150
HOST = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(ifname,encoding="utf8")))[20:24])
128151
PORT = 9000
129152
print("ip: {} port: {}".format(HOST, PORT))
130-
MycobotServer(HOST, PORT)
153+
MycobotServer(HOST, PORT, "/dev/ttyAMA0", 1000000)

0 commit comments

Comments
 (0)