|  | @@ -11,7 +11,7 @@ import binascii
 | 
	
		
			
				|  |  |  # 定义:
 | 
	
		
			
				|  |  |  DataHeader = np.dtype({'names': ['protocol', 'len', 'cmd'], 'formats': ['u1', 'u4', 'u1']}, align=False)
 | 
	
		
			
				|  |  |  UserInfo = np.dtype({'names': ['userName', 'password'], 'formats': ['|S260', '|S260']}, align=False)
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +LoginResp = np.dtype({'names': ['bStatus', 'szMessage'], 'formats': ['?', '|S260']}, align=False)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  class BaseClient(object):
 | 
	
		
			
				|  |  |      __metaclass__ = abc.ABCMeta
 | 
	
	
		
			
				|  | @@ -88,12 +88,12 @@ class BaseClient(object):
 | 
	
		
			
				|  |  |              print "respone:" + binascii.hexlify(respone)
 | 
	
		
			
				|  |  |              '''小端<I, 大端>I, 网络端 !'''
 | 
	
		
			
				|  |  |              protocol, len, cmd = struct.unpack('<BIB', phead)
 | 
	
		
			
				|  |  | -            loginresult = int(struct.unpack('<c', respone))
 | 
	
		
			
				|  |  | +            status, message = int(struct.unpack('<c', respone))
 | 
	
		
			
				|  |  |              '''返回头是否有效'''
 | 
	
		
			
				|  |  |              if protocol != 0xAA and len != data.__len__():
 | 
	
		
			
				|  |  |                  return None
 | 
	
		
			
				|  |  |              
 | 
	
		
			
				|  |  | -            return loginresult
 | 
	
		
			
				|  |  | +            return status
 | 
	
		
			
				|  |  |          except Exception, e:
 | 
	
		
			
				|  |  |              print "BaseClient=> recv Error:", e
 | 
	
		
			
				|  |  |              return None
 | 
	
	
		
			
				|  | @@ -161,14 +161,14 @@ class SATClient(object):
 | 
	
		
			
				|  |  |              print "BaseClient=> send Error:", e
 | 
	
		
			
				|  |  |              self.disconnect()
 | 
	
		
			
				|  |  |              self.connect()
 | 
	
		
			
				|  |  | -            return None
 | 
	
		
			
				|  |  | +            return False
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          '''接收返回数据'''
 | 
	
		
			
				|  |  |          try:
 | 
	
		
			
				|  |  |              # 等待数据返回;(串口若请求失败,超时值应该设置在3秒以上)
 | 
	
		
			
				|  |  |              self.sock.settimeout(10)
 | 
	
		
			
				|  |  |              data = bytes(self.sock.recv(1024 * 8))
 | 
	
		
			
				|  |  | -            print "recv:" + binascii.hexlify(data)
 | 
	
		
			
				|  |  | +            # print "recv:" + binascii.hexlify(data)
 | 
	
		
			
				|  |  |              self.sock.settimeout(None)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              phead = data[0: np.array([(0xAA, 0, 0x01)], dtype=DataHeader).itemsize]
 | 
	
	
		
			
				|  | @@ -176,21 +176,22 @@ class SATClient(object):
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              '''
 | 
	
		
			
				|  |  |              注意:坑!
 | 
	
		
			
				|  |  | -                respone此时是16进制字符 '\x01',Python的字符与C++的不一样,字符就是字符;
 | 
	
		
			
				|  |  | +                如果respone此时只返回16进制字符 '\x01',Python的字符与C++的不一样,字符就是字符;
 | 
	
		
			
				|  |  |                  要将16进制字符转成整型,使用ord函数
 | 
	
		
			
				|  |  |              '''
 | 
	
		
			
				|  |  | -            print "respone:" + binascii.hexlify(respone), ord(respone)
 | 
	
		
			
				|  |  | +            # print "respone:" + binascii.hexlify(respone)
 | 
	
		
			
				|  |  |              '''小端<I, 大端>I, 网络端 !'''
 | 
	
		
			
				|  |  |              protocol, len, cmd = struct.unpack('<BIB', phead)
 | 
	
		
			
				|  |  | -            loginresult = ord(struct.unpack('<c', respone)[0])
 | 
	
		
			
				|  |  | +            # loginresult = ord(struct.unpack('<c', respone)[0])
 | 
	
		
			
				|  |  | +            status, message = struct.unpack('<?260s', respone)
 | 
	
		
			
				|  |  |              '''返回头是否有效'''
 | 
	
		
			
				|  |  |              if protocol != 0xAA and len != data.__len__():
 | 
	
		
			
				|  |  | -                return None
 | 
	
		
			
				|  |  | +                return False
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            return loginresult
 | 
	
		
			
				|  |  | +            return status
 | 
	
		
			
				|  |  |          except Exception, e:
 | 
	
		
			
				|  |  |              print "BaseClient=> recv Error:", e
 | 
	
		
			
				|  |  | -            return None
 | 
	
		
			
				|  |  | +            return False
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      '''断开连接'''
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -204,6 +205,7 @@ class SATClient(object):
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  if __name__ == "__main__":
 | 
	
		
			
				|  |  |      bc = SATClient()
 | 
	
		
			
				|  |  | -    bc.sendmsg()
 | 
	
		
			
				|  |  | +    while 1:
 | 
	
		
			
				|  |  | +        print bc.sendmsg()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      print "ok!"
 |