Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import bluetooth
00036
00037
00038 def print_hex(data):
00039 for i,s in enumerate(data):
00040 print "%02x" % ord(s),
00041 if i%16 == 15:
00042 print
00043 print
00044
00045 def print_hex_msg(data):
00046 count = 0
00047 for s in data:
00048 val = ord(s)
00049 if val == 2:
00050 count = 0
00051 print '\n\n'
00052 if val == 3:
00053 count = 0
00054 print '\n\n'
00055 print ("%02x" % val),
00056 count += 1
00057 if count == 16:
00058 count = 0
00059 print
00060 for s in data:
00061 val = ord(s)
00062 if val == 2:
00063 count = 0
00064 print '\n\n'
00065 if val == 3:
00066 count = 0
00067 print '\n\n'
00068 print ("%02x" % val),
00069 count += 1
00070 if count == 16:
00071 count = 0
00072 print
00073
00074 print
00075
00076 def convert_str(data):
00077 result = []
00078 for c in data:
00079 result.append(ord(c))
00080 return result
00081
00082 def convert_hex(data):
00083 s = ""
00084 for val in data:
00085 s+=chr(val)
00086 return s
00087
00088 def hex_to_short(data):
00089 return (data[0] << 8) | data[1]
00090
00091 seqnum = 1
00092 def make_msg(typ, body):
00093 global seqnum
00094 data = [ 0 for i in range(8)]
00095 length = len(body)+8
00096 data[0] = (length >> 8) & 0xFF
00097 data[1] = length & 0xFF
00098 data[2] = 0
00099 data[3] = (seqnum >> 8) & 0xFF
00100 data[4] = seqnum & 0xFF
00101 seqnum+=1
00102 data[5] = 0
00103 data[6] = (typ >> 8) & 0xFF
00104 data[7] = (typ & 0xFF)
00105 data += body
00106 data[2] = (-sum(data)) & 0xff
00107 return [2] + data + [3]
00108
00109 def make_action_msg(action,speed,stops,do_blowout,mix_cycles):
00110 body = [ 0 for i in range(6) ]
00111 body[0] = action
00112 body[1] = speed
00113 body[2] = (stops>>8)&0xff
00114 body[3] = stops&0xff
00115 body[4] = do_blowout
00116 body[5] = mix_cycles
00117 return make_msg(4,body)
00118
00119 def make_voyager_move(position):
00120 body = [ (position>>8)&0xff, position&0xff ]
00121 return make_msg(12,body)
00122
00123 class PipetteOutputHeader:
00124 def __init__(self,data):
00125 data = data[1:-2]
00126 self.length = (data[0] << 8) | (data[1])
00127 if (sum(data)&0xFF) != 0:
00128 print sum(data)
00129 print (sum(data) & 0xff)
00130 print "bad checksum"
00131
00132 self.checksum = data[2]
00133 self.seqnum = (data[3] << 8) | (data[4])
00134 self.resend = data[5]
00135 self.msg_type = (data[6] << 8) | (data[7])
00136 self.error_code = data[8]
00137 self.body = data[9:]
00138 print "Body = ", self.body
00139
00140 def __str__(self):
00141 s = "PipetteOutputHeader\n"
00142 s += " length %d\n" % self.length
00143 s += " cksum 0x%02x\n" % self.checksum
00144 s += " seqnum %d\n" % self.seqnum
00145 s += " resend %d\n" % self.resend
00146 s += " type %d\n" % self.msg_type
00147 s += " error %d\n" % self.error_code
00148 s += " body "
00149 for v in self.body:
00150 s += "%02x" % v
00151 s += '\n'
00152 return s
00153
00154 class PipetteInfoOutput:
00155 def __init__(self, hdr):
00156 self.hdr = hdr
00157 body = hdr.body
00158 print " owner : '%s'" % convert_hex(body[0:11+1])
00159 print " serial : '%s'" % convert_hex(body[12:23+1])
00160 print " fwmajor : ", hex_to_short(body[24:25+1])
00161 print " fwminor : ", hex_to_short(body[26:27+1])
00162 print " motorsteps millions : ", hex_to_short(body[58:61+1])
00163 print " motorsteps : ", hex_to_short(body[62:65+1])
00164
00165
00166
00167
00168 def send_msg(sock,data):
00169 bytes_sent = sock.send( convert_hex(data) )
00170 if bytes_sent != len(data):
00171 raise RuntimeError("Sent only %d of %d bytes"%(bytes_sent, len(data)))
00172
00173 def recv_msg(sock):
00174 data = []
00175 while (len(data) == 0) or (data[-1] != chr(3)):
00176 data += sock.recv(1024)
00177
00178
00179
00180 print "Recieved : %d bytes" % (len(data))
00181 print_hex(data)
00182 h = PipetteOutputHeader(convert_str(data))
00183 print h
00184 return h
00185
00186
00187 bd_addr = "00:80:98:e6:2b:30"
00188 port = 1
00189
00190
00191
00192
00193 sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
00194 sock.connect((bd_addr, port))
00195
00196
00197 print "Connected"
00198 data = sock.recv(1024)
00199 print "Recieved", convert_str(data)
00200
00201 send_msg(sock, make_msg(1, []))
00202 h = recv_msg(sock)
00203 i = PipetteInfoOutput(h)
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 sock.close()