#/usr/bin/env python
# -*- coding:utf-8 -*-
from ctypes import *
from test import *
import serial
class ScreenControlstru(Structure):
_fields_=[
('stru_command',INTER_SERIAL_COMM_HEAD),
('stru_remotectrl',INTER_SIMULATE_SCREEN_REMOTE_CTRL),
('checknum',c_ubyte)]
class ScreenControl():
def _init_(self):
pass
def AddressSet(self,comNo):
ScreenControl_stru = ScreenControlstru()
ScreenControl_stru.stru_command.wPakageHeader = 0
ScreenControl_stru.stru_command.wLength = 0
ScreenControl_stru.stru_command.dwCommand = COMMAND_SHOW_SCREEN_SIGNATURE
ScreenControl_stru.stru_command.byVersion = 128
ScreenControl_stru.stru_command.struAddress.wHostAddress = 0
ScreenControl_stru.stru_command.struAddress.wBeginAddress =0
ScreenControl_stru.stru_command.struAddress.wEndAddress = 0xFFFF
ScreenControl_stru.stru_remotectrl.struHead.wLength = 6
ScreenControl_stru.stru_remotectrl.struHead.byVersion = 128 #00010000 V1.0
ScreenControl_stru.stru_remotectrl.struHead.byRes = 1
ScreenControl_stru.stru_remotectrl.byControlType = 2
ScreenControl_stru.checknum = 0xFF #求和
buffer1 = (c_ubyte*sizeof(ScreenControlstru))()
memset(buffer1,0xFF,sizeof(ScreenControlstru))
try:
ser = serial.Serial(comNo,9600)
memmove(buffer1,byref(ScreenControl_stru),sizeof(ScreenControlstru))
ser.write(buffer1)
except Exception,e:
print 'open serial failed'
print e
print comNo
exit(1)
总结:前面头文件定义的文件不贴出来了;
1.调试的时候,把错误信息打印出来 print e。
2.将结构体的值赋值给一个缓冲区,注意申请缓冲去的时候要注意值的范围,之前申请的缓冲去是buffer = (c_byte*sizeof(ScreenControlstru)(),c_byte类型的值范围实际是0x7F虽然报错是byte must be in range(0,256),c_ubyte类型可以是0xFF。
3.串口发送数据,不必使用s = '\xaa\xbb\x01'的方式来发送你需要的16进制数,只需要采用C语言常用的方式memory操作即可,只是注意,操作的时候要对指针进行操作,不能直接对数据结构进行操作。