forked from pomeroyb/Create2Control
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate2client.py
63 lines (50 loc) · 1.56 KB
/
create2client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import socket
import sys
import create2api
class RequestError(Exception):
def __init__(self, message, *errors):
# Call the base class constructor with the parameters it needs
super(RequestError, self).__init__(message)
# Now for your custom code...
self.errors = errors
class Client(object):
"""While this is just a simple wrapper around the socket interface
it is meant to be easy to use and also act as an example on how to
write or extend the client interface
"""
def __init__(self, host = '127.0.0.1', port = 8888):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')
#connect socket to host on port
try:
self.sock.connect((host, port))
except socket.error as msg:
print('connection failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
print('Sucessfully Connected')
self.readFile = self.sock.makefile("r")
if self.windows:
atexit.register(self.close)
def __del__(self):
if self.windows:
self.close()
try:
atexit.unregister(self.close)
except:
pass
def close(self):
"""Closes up serial ports and terminates connection to the Create2Server
"""
try:
self.socket.close()
except:
pass
def send(self, *args):
self.conn.send(create2api.make_command_string(args).encode('utf-8'))
def receive(self):
"""Receives data. Note that the trailing newline '\n' is trimmed"""
val = self.readFile.readline().rstrip("\n")
return val
def sendReceive(self, *data):
"""Sends and receive data"""
self.send(*data)
return self.receive()