求指教有关周立功的usbcan模块的事情,拜谢了……
周立功usbcan模块里面有个vb例程,默认的是01 02 03 04 05 06 07 08数据的发送,现在怎样改程序可以实现数据的实时收发啊,求各路大神不吝赐教啊!现在贴出原程序
Dim m_devtype As Long
Dim m_connect As Byte
Dim m_devind As Long
Dim m_cannum As Long
Private Sub Command1_Click()
If m_connect = 0 Then
MsgBox ("请先打开端口")
Exit Sub
End If
Dim SendType, frameformat, frametype As Byte
Dim ID As Long
Dim data(7) As Byte
Dim frameinfo As VCI_CAN_OBJ
Dim str As String
SendType = Combo3.ListIndex
frameformat = Combo5.ListIndex
frametype = Combo4.ListIndex
str = "&H"
str = str + Text1.Text
ID = Val(str)
str = Text4.Text
strdata = " "
i = 0
For i = 0 To 7
strdata = Left(str, 2)
If Len(strdata) = 0 Then
Exit For
End If
str = Right(str, Len(str) - 3)
data(i) = Val("&H" + strdata)
Next
frameinfo.DataLen = i
frameinfo.ExternFlag = frametype
frameinfo.RemoteFlag = frameformat
frameinfo.SendType = SendType
frameinfo.ID = ID
For j = 0 To i - 1
frameinfo.data(j) = data(j)
Next
If VCI_Transmit(m_devtype, m_devind, m_cannum, frameinfo, 1) <> 1 Then
MsgBox ("发送数据失败")
Else
List1.AddItem "发送数据成功", List1.ListCount
End If
End Sub
Private Sub Command2_Click()
If m_connect = 0 Then
MsgBox ("请先打开端口")
Exit Sub
End If
If VCI_StartCAN(m_devtype, m_devind, m_cannum) <> 1 Then
MsgBox ("启动CAN错误")
Else
List1.AddItem "启动CAN成功", List1.ListCount
End If
End Sub
Private Sub Command3_Click()
If m_connect = 0 Then
MsgBox ("请先打开端口")
Exit Sub
End If
If VCI_ResetCAN(m_devtype, m_devind, m_cannum) <> 1 Then
MsgBox ("复位CAN错误")
Else
List1.AddItem "复位CAN成功", List1.ListCount
End If
End Sub
Private Sub Command4_Click()
If m_connect = 0 Then
MsgBox ("请先打开端口")
Exit Sub
End If
Dim i As Long
i = Combo1.ListIndex
If i <> -1 Then
If VCI_SetReference(m_devtype, m_devind, 0, 1, i) <> 1 Then
MsgBox ("更改CAN波特率错误")
Else
List1.AddItem "更改CAN波特率成功", List1.ListCount
End If
End If
End Sub
Private Sub Command5_Click(index As Integer)
If m_connect = 0 Then
MsgBox ("请先打开端口")
Exit Sub
End If
Dim i As Long
i = Combo2.ListIndex
If i <> -1 Then
i = i + 1
If VCI_SetReference(m_devtype, m_devind, 0, 3, i) <> 1 Then
MsgBox ("更改232波特率错误")
Else
List1.AddItem "更改232波特率成功", List1.ListCount
End If
End If
End Sub
Private Sub Connect_Click()
Dim index As Long
Dim cannum As Long
Dim code, mask As Long
Dim Timing0, Timing1, filtertype, Mode As Byte
Dim InitConfig As VCI_INIT_CONFIG
If m_connect = 1 Then
m_connect = 0
Connect.Caption = "连接"
VCI_CloseDevice m_devtype, m_devind
Exit Sub
End If
If Combo1.ListIndex <> -1 And Combo2.ListIndex <> -1 Then
index = Combo1.ListIndex
cannum = Combo2.ListIndex
filtertype = Combo6.ListIndex
Mode = Combo7.ListIndex
code = Val("&H" + Text2.Text)
mask = Val("&H" + Text3.Text)
Timing0 = Val("&H" + Text5.Text)
Timing1 = Val("&H" + Text6.Text)
InitConfig.AccCode = code
InitConfig.AccMask = mask
InitConfig.Filter = filtertype
InitConfig.Mode = Mode
InitConfig.Timing0 = Timing0
InitConfig.Timing1 = Timing1
If VCI_OpenDevice(m_devtype, index, 0) <> 1 Then
MsgBox ("打开设备错误")
Else
If VCI_InitCAN(m_devtype, index, cannum, InitConfig) = 1 Then
m_connect = 1
m_devind = index
m_cannum = cannum
Connect.Caption = "断开"
Else
MsgBox ("初始化CAN错误")
End If
End If
End If
End Sub
Private Sub Form_Load()
Timer1.Enabled = True
m_devtype = 4 'USBCAN2类型号
m_connect = 0
m_cannum = 0
Combo1.ListIndex = 0
Combo2.ListIndex = 0
Combo3.ListIndex = 2
Combo4.ListIndex = 0
Combo5.ListIndex = 0
Combo6.ListIndex = 0
Combo7.ListIndex = 0
End Sub
Private Sub Form_Unload(Cancel As Integer)
If m_connect = 1 Then
m_connect = 0
VCI_CloseDevice m_devtype, m_devind
End If
End Sub
Private Sub Timer1_Timer()
'Timer1.Enabled = False
Dim ErrInfo As VCI_ERR_INFO
'If m_connect = 0 Then
' Timer1.Enabled = True
'Exit sub
' End If
Dim length As Long
Dim frameinfo(49) As VCI_CAN_OBJ
Dim str As String
length = VCI_Receive(m_devtype, m_devind, m_cannum, frameinfo(0), 50, 10)
If length <= 0 Then
VCI_ReadErrInfo m_devtype, m_devind, m_cannum, ErrInfo '注意:如果没有读到数据则必须调用此函数来读取出当前的错误码,
'千万不能省略这一步(即使你可能不想知道错误码是什么)
Timer1.Enabled = True
Exit Sub
End If
For i = 0 To length - 1
str = "接收到数据帧: "
If frameinfo(i).TimeFlag = 0 Then
tmpstr = "时间标识:无 "
Else
tmpstr = "时间标识:0x" + Hex(frameinfo(i).TimeStamp)
End If
str = str + tmpstr
tmpstr = " 帧ID:0x" + Hex(frameinfo(i).ID)
str = str + tmpstr
str = str + " 帧格式:"
If frameinfo(i).RemoteFlag = 0 Then
tmpstr = "数据帧 "
Else
tmpstr = "远程帧 "
End If
str = str + tmpstr
str = str + " 帧类型:"
If frameinfo(i).ExternFlag = 0 Then
tmpstr = "标准帧 "
Else
tmpstr = "扩展帧 "
End If
str = str + tmpstr
List1.AddItem str, List1.ListCount
If frameinfo(i).RemoteFlag = 0 Then
str = " 数据:"
If frameinfo(i).DataLen > 8 Then
frameinfo(i).DataLen = 8
End If
For j = 0 To frameinfo(i).DataLen - 1
tmpstr = Hex(frameinfo(i).data(j)) + " "
str = str + tmpstr
Next
List1.AddItem str, List1.ListCount
End If
Next
Timer1.Enabled = True
End Sub
调用模块如下
'定义需要用到的数据结构
Public Type VCI_BOARD_INFO
hw_Version As Integer
fw_Version As Integer
dr_Version As Integer
in_Version As Integer
irq_num As Integer
can_num As Byte
str_Serial_Num(19) As Byte
str_hw_Type(39) As Byte
Reserved(3) As Integer
End Type
Public Type VCI_CAN_OBJ
ID As Long
TimeStamp As Long
TimeFlag As Byte
SendType As Byte
RemoteFlag As Byte
ExternFlag As Byte
DataLen As Byte
data(7) As Byte
Reserved(2) As Byte
End Type
Public Type VCI_CAN_STATUS
ErrInterrupt As Byte
regMode As Byte
regStatus As Byte
regALCapture As Byte
regECCapture As Byte
regEWLimit As Byte
regRECounter As Byte
regTECounter As Byte
Reserved As Long
End Type
Public Type VCI_ERR_INFO
ErrCode As Long
Passive_ErrData(2) As Byte
ArLost_ErrData As Byte
End Type
Public Type VCI_INIT_CONFIG
AccCode As Long
AccMask As Long
Reserved As Long
Filter As Byte
Timing0 As Byte
Timing1 As Byte
Mode As Byte
End Type
Declare Function VCI_OpenDevice Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal Reserved As Long) As Long
Declare Function VCI_CloseDevice Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long) As Long
Declare Function VCI_InitCAN Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long, ByRef InitConfig As VCI_INIT_CONFIG) As Long
Declare Function VCI_ReadBoardInfo Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByRef info As VCI_BOARD_INFO) As Long
Declare Function VCI_ReadErrInfo Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long, ByRef ErrInfo As VCI_ERR_INFO) As Long
Declare Function VCI_ReadCANStatus Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long, ByRef CANStatus As VCI_CAN_STATUS) As Long
Declare Function VCI_GetReference Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long, ByVal RefType As Long, ByRef data As Any) As Long
Declare Function VCI_SetReference Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long, ByVal RefType As Long, ByRef data As Any) As Long
Declare Function VCI_GetReceiveNum Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long) As Long
Declare Function VCI_ClearBuffer Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long) As Long
Declare Function VCI_StartCAN Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long) As Long
Declare Function VCI_ResetCAN Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long) As Long
Declare Function VCI_Transmit Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long, ByRef Send As VCI_CAN_OBJ, ByVal length As Long) As Long
Declare Function VCI_Receive Lib "ControlCAN.dll" (ByVal DeviceType As Long, ByVal DeviceInd As Long, ByVal CANInd As Long, ByRef Receive As VCI_CAN_OBJ, ByVal length As Long, ByVal WaitTime As Long) As Long
提问者:uweujvwerw
地点:-
浏览次数:129
提问时间:07-14 07:07
我有更好的答案