|
|
VERSION 5.00 Begin VB.Form Form1 Caption = "USB_CAN Demo(��о�����Ƽ�����˾)" ClientHeight = 6480 ClientLeft = 7155 ClientTop = 2280 ClientWidth = 9420 LinkTopic = "Form1" ScaleHeight = 6480 ScaleWidth = 9420 Begin VB.CommandButton Command4 Caption = "Clear List" Height = 375 Left = 7920 TabIndex = 34 Top = 3480 Width = 1335 End Begin VB.ComboBox Combo2 Height = 300 ItemData = "Form1.frx":0000 Left = 4920 List = "Form1.frx":000A Style = 2 'Dropdown List TabIndex = 19 Top = 600 Width = 1185 End Begin VB.TextBox Text1 Height = 285 Left = 6120 TabIndex = 18 Text = "00000080" Top = 2880 Width = 975 End Begin VB.Timer Timer1 Interval = 10 Left = 7920 Top = 480 End Begin VB.TextBox Text4 Height = 285 Left = 720 TabIndex = 15 Text = "01 02 03 04 05 06 07 08 " Top = 3360 Width = 2415 End Begin VB.ComboBox Combo5 Height = 300 ItemData = "Form1.frx":0022 Left = 3600 List = "Form1.frx":002C Style = 2 'Dropdown List TabIndex = 14 Top = 2805 Width = 1695 End Begin VB.ComboBox Combo4 Height = 300 ItemData = "Form1.frx":004A Left = 960 List = "Form1.frx":0054 Style = 2 'Dropdown List TabIndex = 13 Top = 2820 Width = 1815 End Begin VB.CommandButton Command3 Caption = "Reset CAN" Height = 330 Left = 7920 TabIndex = 12 Top = 3120 Width = 1245 End Begin VB.CommandButton Command2 Caption = "Start CAN" Height = 330 Left = 7920 TabIndex = 11 Top = 2640 Width = 1125 End Begin VB.ComboBox Combo1 Height = 300 ItemData = "Form1.frx":0078 Left = 1680 List = "Form1.frx":0082 Style = 2 'Dropdown List TabIndex = 10 Top = 480 Width = 1290 End Begin VB.CommandButton Connect Caption = "Connection" Height = 330 Left = 6360 TabIndex = 1 Top = 480 Width = 1485 End Begin VB.Frame Frame1 Caption = "Transmitting data frames" Height = 1230 Left = 120 TabIndex = 0 Top = 2520 Width = 7635 Begin VB.CommandButton Command1 Caption = "Send" Height = 330 Left = 6600 TabIndex = 6 Top = 720 Width = 870 End Begin VB.Label Label3 Caption = "Type��" Height = 240 Left = 120 TabIndex = 33 Top = 360 Width = 960 End Begin VB.Label Label7 Caption = "Data��" Height = 240 Left = 120 TabIndex = 5 Top = 840 Width = 555 End Begin VB.Label Label6 Caption = "ID��" Height = 195 Left = 5280 TabIndex = 4 Top = 360 Width = 585 End Begin VB.Label Label5 Caption = "Format��" Height = 195 Left = 2760 TabIndex = 3 Top = 360 Width = 765 End Begin VB.Label Label4 Caption = "֡���ͣ�" Height = 195 Left = 120 TabIndex = 2 Top = 360 Width = 810 End End Begin VB.Frame Frame2 Caption = "Device Parameters" Height = 2295 Left = 120 TabIndex = 7 Top = 120 Width = 8895 Begin VB.Frame Frame3 Caption = "CAN initialization parameters" Height = 1335 Left = 120 TabIndex = 20 Top = 840 Width = 8415 Begin VB.ComboBox Combo7 Height = 300 ItemData = "Form1.frx":0099 Left = 6360 List = "Form1.frx":00A6 Style = 2 'Dropdown List TabIndex = 32 Top = 720 Width = 2055 End Begin VB.ComboBox Combo6 Height = 300 ItemData = "Form1.frx":00D9 Left = 6360 List = "Form1.frx":00E6 Style = 2 'Dropdown List TabIndex = 30 Top = 360 Width = 1935 End Begin VB.TextBox Text6 Height = 285 Left = 3720 TabIndex = 28 Text = "14" Top = 840 Width = 1095 End Begin VB.TextBox Text5 Height = 285 Left = 1200 TabIndex = 26 Text = "00" Top = 840 Width = 855 End Begin VB.TextBox Text3 Height = 285 Left = 3720 TabIndex = 24 Text = "FFFFFFFF" Top = 360 Width = 1095 End Begin VB.TextBox Text2 Height = 285 Left = 1200 TabIndex = 22 Text = "00000000" Top = 360 Width = 855 End Begin VB.Label Label13 Caption = "Mode��" Height = 255 Left = 5640 TabIndex = 31 Top = 720 Width = 615 End Begin VB.Label Label12 Caption = "Filter methods��" Height = 255 Left = 4800 TabIndex = 29 Top = 405 Width = 1575 End Begin VB.Label Label11 Caption = "Timer1��0x" Height = 255 Left = 2280 TabIndex = 27 Top = 840 Width = 975 End Begin VB.Label Label8 Caption = "Timer0:0x" Height = 255 Left = 120 TabIndex = 25 Top = 840 Width = 975 End Begin VB.Label Label2 Caption = "MASK code:0x" Height = 255 Left = 2400 TabIndex = 23 Top = 360 Width = 1335 End Begin VB.Label Label1 Caption = "ACC code��0x" Height = 255 Left = 120 TabIndex = 21 Top = 360 Width = 1095 End End Begin VB.Label Label10 Caption = "CAN channels" Height = 255 Left = 3360 TabIndex = 9 Top = 360 Width = 1095 End Begin VB.Label Label9 Caption = "Device Type" Height = 255 Left = 240 TabIndex = 8 Top = 360 Width = 1095 End End Begin VB.Frame Frame6 Caption = "List" Height = 2535 Left = 0 TabIndex = 16 Top = 3840 Width = 9255 Begin VB.ListBox List1 Height = 2040 Left = 120 TabIndex = 17 Top = 240 Width = 8895 End End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Dim m_devtype As Long Dim m_connect As Byte Dim m_cannum As Long Dim m_bRun As Boolean Public Function EnableUI(bEnable As Boolean) Label1.Enabled = bEnable Label2.Enabled = bEnable Label12.Enabled = bEnable Label8.Enabled = bEnable Label11.Enabled = bEnable Label13.Enabled = bEnable Text2.Enabled = bEnable Text3.Enabled = bEnable Text5.Enabled = bEnable Text6.Enabled = bEnable Combo1.Enabled = bEnable Combo2.Enabled = bEnable Combo6.Enabled = bEnable Combo7.Enabled = bEnable End Function
Private Sub Command1_Click() If m_connect = 0 Then MsgBox ("Please open the port first") 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, tmpstr As String SendType = 0 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.ID = ID For j = 0 To i - 1 frameinfo.data(j) = data(j) Next If VCI_Transmit(m_devtype, 0, m_cannum, frameinfo, 1) <> 1 Then MsgBox ("Failed to send data") Else str = "Successful sending of data: " tmpstr = "Time Mark: None " str = str + tmpstr tmpstr = " ID:0x" + Hex(frameinfo.ID) str = str + tmpstr str = str + " Type:" If frameinfo.RemoteFlag = 0 Then tmpstr = "data frame " Else tmpstr = "Remote Frame " End If str = str + tmpstr str = str + " Format:" If frameinfo.ExternFlag = 0 Then tmpstr = "Standard frame " Else tmpstr = "Extended frame " End If str = str + tmpstr List1.AddItem str, List1.ListCount If frameinfo.RemoteFlag = 0 Then str = " data:" If frameinfo.DataLen > 8 Then frameinfo.DataLen = 8 End If For j = 0 To frameinfo.DataLen - 1 tmpstr = Hex(frameinfo.data(j)) + " " str = str + tmpstr Next List1.AddItem str, List1.ListCount End If List1.ListIndex = List1.ListCount - 1 End If List1.ListIndex = List1.ListCount - 1 End Sub
Private Sub Command2_Click() If m_connect = 0 Then MsgBox ("Please open the port first") Exit Sub End If If VCI_StartCAN(m_devtype, 0, m_cannum) <> 1 Then MsgBox ("Start CAN Error") Else List1.AddItem "Start CAN Successfully", List1.ListCount Command1.Enabled = True End If End Sub
Private Sub Command3_Click() If m_connect = 0 Then MsgBox ("Please open the port first") Exit Sub End If If VCI_ResetCAN(m_devtype, 0, m_cannum) <> 1 Then MsgBox ("Reset CAN error") Else List1.AddItem "Reset CAN Successfully", List1.ListCount Command1.Enabled = False End If
End Sub
Private Sub Command4_Click() Dim i As Integer For i = 0 To List1.ListCount - 1 List1.RemoveItem 0 Next End Sub
Private Sub Connect_Click() 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 = "Connect" VCI_CloseDevice m_devtype, 0 EnableUI True Exit Sub End If If Combo1.ListIndex <> -1 And Combo2.ListIndex <> -1 Then cannum = Combo2.ListIndex filtertype = Combo6.ListIndex + 1 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, 0, 0) <> 1 Then MsgBox ("Error opening device") Else If VCI_InitCAN(m_devtype, 0, cannum, InitConfig) = 1 Then m_connect = 1 m_cannum = cannum Connect.Caption = "Disconnect" Else MsgBox ("Initialization CAN error") End If End If End If EnableUI False End Sub
Private Sub Form_Activate() m_bRun = True End Sub
Private Sub Form_Load() Me.Left = (Screen.Width - Me.Width) / 2 Me.Top = (Screen.Height - Me.Height) / 2 m_bRun = False m_devtype = 4 'USB_CAN2���ͺ� m_connect = 0 m_cannum = 0 Combo1.ListIndex = 1 Combo2.ListIndex = 0 Combo4.ListIndex = 0 Combo5.ListIndex = 0 Combo6.ListIndex = 0 Combo7.ListIndex = 0 EnableUI True Command1.Enabled = False End Sub
Private Sub Form_Unload(Cancel As Integer) If m_connect = 1 Then m_connect = 0 VCI_CloseDevice m_devtype, 0 End If End Sub Private Sub Combo1_Click() If m_bRun = True Then m_devtype = Combo1.ListIndex + 3 If m_devtype = 3 Then Combo2.RemoveItem 1 m_cannum = 0 Combo2.ListIndex = 0 Else Combo2.AddItem "Channel 2" End If 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, 0, m_cannum, frameinfo(0), 50, 10) If Length <= 0 Then Timer1.Enabled = True Exit Sub End If For i = 0 To Length - 1 str = "Received data frame: " If frameinfo(i).TimeFlag = 0 Then tmpstr = "Time Mark: None " Else tmpstr = "Time Mark:0x" + Hex(frameinfo(i).TimeStamp) End If str = str + tmpstr tmpstr = " ID:0x" + Hex(frameinfo(i).ID) str = str + tmpstr str = str + " frame format:" If frameinfo(i).RemoteFlag = 0 Then tmpstr = "data frame " Else tmpstr = "Remote Frame " End If str = str + tmpstr str = str + " Frame type:" If frameinfo(i).ExternFlag = 0 Then tmpstr = "Standard frame " Else tmpstr = "Extended frame " End If str = str + tmpstr List1.AddItem str, List1.ListCount If frameinfo(i).RemoteFlag = 0 Then str = " data:" 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 List1.ListIndex = List1.ListCount - 1 Next Timer1.Enabled = True End Sub
|