@@ -20,28 +20,30 @@ def get_logger(name):
2020 # console = logging.StreamHandler()
2121 # console.setFormatter(formatter)
2222
23- save = logging .handlers .RotatingFileHandler ("/home/ubuntu/mycobot_server.log" , maxBytes = 10485760 , backupCount = 1 )
23+ save = logging .handlers .RotatingFileHandler (
24+ "/home/ubuntu/mycobot_server.log" , maxBytes = 10485760 , backupCount = 1 )
2425 save .setFormatter (formatter )
2526
2627 logger .addHandler (save )
2728 # logger.addHandler(console)
2829 return logger
2930
31+
3032class MycobotServer (object ):
3133
3234 def __init__ (self , host , port ):
3335 GPIO .setwarnings (False )
3436 self .logger = get_logger ("AS" )
3537 self .mc = None
3638 self .s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
37- self .s .bind ((host ,port ))
39+ self .s .bind ((host , port ))
3840 print "Binding succeeded!"
3941 self .s .listen (1 )
4042 self .connect ()
4143
4244 def connect (self ):
43- try :
44- while True :
45+ while True :
46+ try :
4547 print "waiting connect!------------------"
4648 conn , addr = self .s .accept ()
4749 port_baud = []
@@ -54,12 +56,13 @@ def connect(self):
5456 print ("close dsiconnect!" )
5557 break
5658 res = b'1'
57- command = command .replace (" " ,"" )
58- if len (port_baud )< 3 :
59-
59+ command = command .replace (" " , "" )
60+ if len (port_baud ) < 3 :
61+
6062 port_baud .append (command )
61- if len (port_baud )== 3 :
62- self .mc = serial .Serial (port_baud [0 ],port_baud [1 ],timeout = float (port_baud [1 ]))
63+ if len (port_baud ) == 3 :
64+ self .mc = serial .Serial (
65+ port_baud [0 ], port_baud [1 ], timeout = float (port_baud [1 ]))
6366 port_baud .append (1 )
6467 else :
6568 self .logger .info (command )
@@ -71,12 +74,12 @@ def connect(self):
7174 GPIO .setmode (GPIO .BOARD )
7275 elif command [3 ] == 171 :
7376 if command [5 ]:
74- GPIO .setup (command [4 ],GPIO .OUT )
77+ GPIO .setup (command [4 ], GPIO .OUT )
7578 else :
76- GPIO .setup (command [4 ],GPIO .IN )
79+ GPIO .setup (command [4 ], GPIO .IN )
7780
7881 elif command [3 ] == 172 :
79- GPIO .output (command [4 ],command [5 ])
82+ GPIO .output (command [4 ], command [5 ])
8083
8184 elif command [3 ] == 173 :
8285 res = bytes (GPIO .input (command [4 ]))
@@ -89,19 +92,19 @@ def connect(self):
8992 except Exception as e :
9093 self .logger .error (str (e ))
9194 conn .sendall (str .encode ("ERROR:" + str (e )))
92- break
93- except Exception as e :
94- self .logger .error (str (e ))
95- conn .close ()
95+ break
96+ except Exception as e :
97+ self .logger .error (str (e ))
98+ conn .close ()
9699
97- def write (self ,command ):
100+ def write (self , command ):
98101 self .mc .write (command )
99102 self .mc .flush ()
100103 time .sleep (0.05 )
101104
102105 def read (self ):
103106 data = None
104- if self .mc .inWaiting ()> 0 :
107+ if self .mc .inWaiting () > 0 :
105108 data = self .mc .read (self .mc .inWaiting ())
106109 return data
107110
@@ -112,7 +115,8 @@ def re_data_2(self, command):
112115 data_list = [int (i ) for i in data_list ]
113116 return data_list
114117
118+
115119if __name__ == "__main__" :
116120 HOST = socket .gethostbyname (socket .gethostname ())
117121 PORT = 9000
118- MycobotServer (HOST ,PORT )
122+ MycobotServer (HOST , PORT )
0 commit comments