Skip to content

Commit

Permalink
Update the socket file
Browse files Browse the repository at this point in the history
  • Loading branch information
anla-xu committed Nov 23, 2021
1 parent c9de158 commit 0cd5e1d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# ChangeLog for pymycobot

## v2.7.1 (2021-11-23)

- release v2.7.1
- Refactor tcp/ip control method
- Optimize tcp/ip control gpio
- Fix the error of get_basic_input() again
- Added instructions for using MyCobotSocket

## v2.7.0 (2021-11-15)

- release v2.7.0
Expand Down
3 changes: 2 additions & 1 deletion demo/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

from pymycobot import MyCobotSocket

m = MyCobotSocket("192.168.10.10", "/dev/ttyAMA0", "1000000")
m = MyCobotSocket("192.168.10.10", "9000")
m.connect()
print(m.get_coords())
87 changes: 65 additions & 22 deletions demo/Server.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,118 @@
#!/usr/bin/env python3
#!/usr/bin/env python2
# coding:utf-8
import socket
import serial
import time
import logging
import logging.handlers
import re
import RPi.GPIO as GPIO


class MycobotServer:
def get_logger(name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)

LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
#DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"

formatter = logging.Formatter(LOG_FORMAT)
# console = logging.StreamHandler()
# console.setFormatter(formatter)

save = logging.handlers.RotatingFileHandler("/home/ubuntu/mycobot_server.log", maxBytes=10485760, backupCount=1)
save.setFormatter(formatter)

logger.addHandler(save)
# logger.addHandler(console)
return logger

class MycobotServer(object):

def __init__(self, host, port):
GPIO.setwarnings(False)
self.logger = get_logger("AS")
self.mc = None
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.bind((host, port))
print("Binding succeeded!")
self.s.bind((host,port))
print "Binding succeeded!"
self.s.listen(1)
self.connect()

def connect(self):
try:
while True:
print "waiting connect!------------------"
conn, addr = self.s.accept()
port_baud = []
while True:
try:
print "waiting data--------"
data = conn.recv(1024)
command = data.decode('utf-8')
if data.decode('utf-8') == "":
print("client dsiconnect!")
print("close dsiconnect!")
break
res = b'None'
command = command.replace(" ", "")

if len(port_baud) < 3:

res = b'1'
command = command.replace(" ","")
if len(port_baud)<3:

port_baud.append(command)
if len(port_baud) == 3:
print(port_baud)
self.mc = serial.Serial(
port_baud[0], port_baud[1], timeout=float(port_baud[1]))
if len(port_baud)==3:
self.mc = serial.Serial(port_baud[0],port_baud[1],timeout=float(port_baud[1]))
port_baud.append(1)
else:
self.logger.info(command)
command = self.re_data_2(command)
if command[3] == 170:
if command[4] == 0:
GPIO.setmode(GPIO.BCM)
else:
GPIO.setmode(GPIO.BOARD)
elif command[3] == 171:
if command[5]:
GPIO.setup(command[4],GPIO.OUT)
else:
GPIO.setup(command[4],GPIO.IN)

elif command[3] == 172:
GPIO.output(command[4],command[5])

elif command[3] == 173:
res = bytes(GPIO.input(command[4]))

self.write(command)
res = self.read()

if res == None:
res = b'1'
conn.sendall(res)
except Exception as e:
self.logger.error(str(e))
conn.sendall(str.encode("ERROR:"+str(e)))
break
except:
break
except Exception as e:
self.logger.error(str(e))
conn.close()

def write(self, command):
def write(self,command):
self.mc.write(command)
self.mc.flush()
time.sleep(0.05)

def read(self):
data = None
if self.mc.inWaiting() > 0:
if self.mc.inWaiting()>0:
data = self.mc.read(self.mc.inWaiting())
return data

def re_data_2(self, command):
r2 = re.compile(r'[[](.*?)[]]')
data_str = re.findall(r2, command)[0]
data_list = [int(i) for i in data_str.split(',')]
data_list = data_str.split(",")
data_list = [int(i) for i in data_list]
return data_list


if __name__ == "__main__":
HOST = socket.gethostbyname(socket.gethostname())
PORT = 9000
MycobotServer(HOST, PORT)
MycobotServer(HOST,PORT)

0 comments on commit 0cd5e1d

Please sign in to comment.