Skip to content

Commit 0cd5e1d

Browse files
committed
Update the socket file
1 parent c9de158 commit 0cd5e1d

File tree

3 files changed

+75
-23
lines changed

3 files changed

+75
-23
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# ChangeLog for pymycobot
22

3+
## v2.7.1 (2021-11-23)
4+
5+
- release v2.7.1
6+
- Refactor tcp/ip control method
7+
- Optimize tcp/ip control gpio
8+
- Fix the error of get_basic_input() again
9+
- Added instructions for using MyCobotSocket
10+
311
## v2.7.0 (2021-11-15)
412

513
- release v2.7.0

demo/Client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
from pymycobot import MyCobotSocket
55

6-
m = MyCobotSocket("192.168.10.10", "/dev/ttyAMA0", "1000000")
6+
m = MyCobotSocket("192.168.10.10", "9000")
7+
m.connect()
78
print(m.get_coords())

demo/Server.py

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,118 @@
1-
#!/usr/bin/env python3
1+
#!/usr/bin/env python2
22
# coding:utf-8
33
import socket
44
import serial
55
import time
6+
import logging
7+
import logging.handlers
68
import re
9+
import RPi.GPIO as GPIO
710

811

9-
class MycobotServer:
12+
def get_logger(name):
13+
logger = logging.getLogger(name)
14+
logger.setLevel(logging.DEBUG)
15+
16+
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
17+
#DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"
18+
19+
formatter = logging.Formatter(LOG_FORMAT)
20+
# console = logging.StreamHandler()
21+
# console.setFormatter(formatter)
22+
23+
save = logging.handlers.RotatingFileHandler("/home/ubuntu/mycobot_server.log", maxBytes=10485760, backupCount=1)
24+
save.setFormatter(formatter)
25+
26+
logger.addHandler(save)
27+
# logger.addHandler(console)
28+
return logger
29+
30+
class MycobotServer(object):
1031

1132
def __init__(self, host, port):
33+
GPIO.setwarnings(False)
34+
self.logger = get_logger("AS")
1235
self.mc = None
1336
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
14-
self.s.bind((host, port))
15-
print("Binding succeeded!")
37+
self.s.bind((host,port))
38+
print "Binding succeeded!"
1639
self.s.listen(1)
1740
self.connect()
1841

1942
def connect(self):
2043
try:
2144
while True:
45+
print "waiting connect!------------------"
2246
conn, addr = self.s.accept()
2347
port_baud = []
2448
while True:
2549
try:
50+
print "waiting data--------"
2651
data = conn.recv(1024)
2752
command = data.decode('utf-8')
2853
if data.decode('utf-8') == "":
29-
print("client dsiconnect!")
54+
print("close dsiconnect!")
3055
break
31-
res = b'None'
32-
command = command.replace(" ", "")
33-
34-
if len(port_baud) < 3:
35-
56+
res = b'1'
57+
command = command.replace(" ","")
58+
if len(port_baud)<3:
59+
3660
port_baud.append(command)
37-
if len(port_baud) == 3:
38-
print(port_baud)
39-
self.mc = serial.Serial(
40-
port_baud[0], port_baud[1], timeout=float(port_baud[1]))
61+
if len(port_baud)==3:
62+
self.mc = serial.Serial(port_baud[0],port_baud[1],timeout=float(port_baud[1]))
4163
port_baud.append(1)
4264
else:
65+
self.logger.info(command)
4366
command = self.re_data_2(command)
67+
if command[3] == 170:
68+
if command[4] == 0:
69+
GPIO.setmode(GPIO.BCM)
70+
else:
71+
GPIO.setmode(GPIO.BOARD)
72+
elif command[3] == 171:
73+
if command[5]:
74+
GPIO.setup(command[4],GPIO.OUT)
75+
else:
76+
GPIO.setup(command[4],GPIO.IN)
77+
78+
elif command[3] == 172:
79+
GPIO.output(command[4],command[5])
80+
81+
elif command[3] == 173:
82+
res = bytes(GPIO.input(command[4]))
83+
4484
self.write(command)
4585
res = self.read()
46-
86+
if res == None:
87+
res = b'1'
4788
conn.sendall(res)
4889
except Exception as e:
90+
self.logger.error(str(e))
4991
conn.sendall(str.encode("ERROR:"+str(e)))
50-
break
51-
except:
92+
break
93+
except Exception as e:
94+
self.logger.error(str(e))
5295
conn.close()
5396

54-
def write(self, command):
97+
def write(self,command):
5598
self.mc.write(command)
5699
self.mc.flush()
57100
time.sleep(0.05)
58101

59102
def read(self):
60103
data = None
61-
if self.mc.inWaiting() > 0:
104+
if self.mc.inWaiting()>0:
62105
data = self.mc.read(self.mc.inWaiting())
63106
return data
64107

65108
def re_data_2(self, command):
66109
r2 = re.compile(r'[[](.*?)[]]')
67110
data_str = re.findall(r2, command)[0]
68-
data_list = [int(i) for i in data_str.split(',')]
111+
data_list = data_str.split(",")
112+
data_list = [int(i) for i in data_list]
69113
return data_list
70114

71-
72115
if __name__ == "__main__":
73116
HOST = socket.gethostbyname(socket.gethostname())
74117
PORT = 9000
75-
MycobotServer(HOST, PORT)
118+
MycobotServer(HOST,PORT)

0 commit comments

Comments
 (0)