现在帮北京三一重工搞个gps的软件,涉及到用vb6读取CAN口数据(DLL)的问题。
原始的DLL文件是CAN的"IEICAN02.dll",C函数如下:
Function:INT32 CAN_ReadMSG(UINT16 card_idx,UINT8 can_no,UINT16 count,CAN_MSG*p_obj) Description:
Read receive message form RX QueueParameter:
card_idx: card index(0...n),一般都是0
can_no: CANbus port index(0...n),一般都是0
count: message request to get,表示如果有10个id的数据,就一次读10个数据。
p_obj: pointer to the request message bufferReturn:
>0: Physical number of message retrived
<0: CAN_ERR_PARA
the structure of the CAN_MSG will be following:
typedef struct{
UINT32 time_stamp:
UINT32 id;
UINT8 len;
UINT8 rtr;
UINT8 a_data[8];
}CAN_MSG;
我在vb6中声明如下:
Declare Function CAN_ReadMsg Lib "IEICAN02.dll" Alias "_CAN_ReadMsg@16" (ByVal card_idx As Integer, ByVal can_no As Byte, ByVal count As Integer, ByRef p_obj As CAN_MSG) As LongDim cmOut As CAN_MSG
nReturn = CAN_ReadMsg(0, 0, 10, cmOut) '假设一次读取10个数据。
CAN_MSG我是这样定义的:
Public Type CAN_MSG
time_stamp As Long
id As Long
len As Byte
rtr As Byte
a_data(7) As Byte
End Type读取后发现,10个数据中的第一个的id和长度len,还有8个字节的数据都没有问题,但是从第二个开始,全部的数据都偏移了,具体可能是2-4个字节之类的,请问究竟为什么会产生这样的偏移?
明明Uint32和long, uint8和byte是一一对应的啊,难道是数组的问题?请高手指点迷津!!! 急!!!
原始的DLL文件是CAN的"IEICAN02.dll",C函数如下:
Function:INT32 CAN_ReadMSG(UINT16 card_idx,UINT8 can_no,UINT16 count,CAN_MSG*p_obj) Description:
Read receive message form RX QueueParameter:
card_idx: card index(0...n),一般都是0
can_no: CANbus port index(0...n),一般都是0
count: message request to get,表示如果有10个id的数据,就一次读10个数据。
p_obj: pointer to the request message bufferReturn:
>0: Physical number of message retrived
<0: CAN_ERR_PARA
the structure of the CAN_MSG will be following:
typedef struct{
UINT32 time_stamp:
UINT32 id;
UINT8 len;
UINT8 rtr;
UINT8 a_data[8];
}CAN_MSG;
我在vb6中声明如下:
Declare Function CAN_ReadMsg Lib "IEICAN02.dll" Alias "_CAN_ReadMsg@16" (ByVal card_idx As Integer, ByVal can_no As Byte, ByVal count As Integer, ByRef p_obj As CAN_MSG) As LongDim cmOut As CAN_MSG
nReturn = CAN_ReadMsg(0, 0, 10, cmOut) '假设一次读取10个数据。
CAN_MSG我是这样定义的:
Public Type CAN_MSG
time_stamp As Long
id As Long
len As Byte
rtr As Byte
a_data(7) As Byte
End Type读取后发现,10个数据中的第一个的id和长度len,还有8个字节的数据都没有问题,但是从第二个开始,全部的数据都偏移了,具体可能是2-4个字节之类的,请问究竟为什么会产生这样的偏移?
明明Uint32和long, uint8和byte是一一对应的啊,难道是数组的问题?请高手指点迷津!!! 急!!!
time_stamp As Integer
id As Integer
len As Byte
rtr As Byte
a_data(7) As Byte
End Typetime_stamp、id 类型定义不对,应该是我上边的类型
我发现好像vb的多了两个字节,但是又有点混乱不知道在哪里多了,是否应该用下面的?
time_stamp As Integer
id As Long
因为id常常是0x1C1F1220,id定义成integar应该会溢出。另外想请问为什么会出现这种位数不对齐的情况? 谢谢高手!!!
id 存储这两个类型就是对的
多读几个看看,吧边界在哪里?
因为很多vb的人都不了解C,而了解C的一般都了解vb,所以来这里了提问了。
msgLen=Len(CAN_MSG)
vb好像没有结构对齐的(可能我不知道)!