1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
| '(c) SYSTEC electronic GmbH, D-07973 Greiz, August-Bebel-Str. 29
' <a href="http://www.systec-electronic.de" target="_blank">www.systec-electronic.de</a>
'Project: Demoapplet to test .NET Interface for USB-CANmodul GW-00x
'Description: Demoapplet
'-------------------------------------------------------------------------
' $RCSfile:$
' $Author:$
' $Revision:$ $Date:$
' $State:$
' Build Environment:
' Microsoft Visual Studio .NET (Visual Basic .NET)
'-------------------------------------------------------------------------
'Revision History:
' 2005/23/11 d.k.: new
'ToDo:
'- show bits of CanStatus from GetStatus() in separate StatusBarPanels
Imports System.Threading
Public Class MainForm
Inherits System.Windows.Forms.Form
#Region " Variable ######################################################################## "
' delegate for listview updates
Delegate Sub DelegateListViewItem(ByRef item As ListViewItem)
' the wrapper object to USBCAN32.DLL
Protected WithEvents m_USBcanInst1 As UcanDotNET.USBcanServer
Protected WithEvents m_USBcanInst2 As UcanDotNET.USBcanServer
' are the hardware and all existing CAN channels initialized
Protected m_fIsInst1Initialized As Boolean = False
Protected m_fIsInst2Initialized As Boolean = False
' does exist the 2nd CAN channel, i.e. USBCAN_CHANNEL_CH1
Protected m_f2ndChannelInst1 As Boolean = False
Protected m_f2ndChannelInst2 As Boolean = False
' device number is used to check the instances
Protected m_DeviceNrInst1 As Byte = 255
Protected m_DeviceNrInst2 As Byte = 255
' USB-CANmodul is in TxEchoMode
Protected m_fTxEchoMode As Boolean = False
' CAN message which will be sent
Protected m_canMsgStruct(0) As UcanDotNET.USBcanServer.tCanMsgStruct
' message counter (of sent messages)
Protected m_dwMsgCount As Integer = 0
' receiver thread
Protected m_RxThread As Thread
' wait handles for receiver thread (0 = do exit, 1 = received msg, 2 = status changed)
Protected m_RxWaitHandles() As WaitHandle = {New AutoResetEvent(False), New AutoResetEvent(False), New AutoResetEvent(False)}
#End Region
#Region " Vom Windows Form Designer generierter Code "
Public Sub New()
MyBase.New()
' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
InitializeComponent()
' Initialisierungen nach dem Aufruf InitializeComponent() hinzufügen
m_canMsgStruct(0) = UcanDotNET.USBcanServer.tCanMsgStruct.CreateInstance(&H441)
' test with Extended Frame Format
'm_canMsgStruct(0) = USBcanLib.USBcanServer.tCanMsgStruct.CreateInstance(&H4441)
'm_canMsgStruct(0).m_bFF = USBcanLib.USBcanServer.USBCAN_MSG_FF_EXT
m_canMsgStruct(0).m_bDLC = 8
End Sub
' Die Form überschreibt den Löschvorgang der Basisklasse, um Komponenten zu bereinigen.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' Für Windows Form-Designer erforderlich
Private components As System.ComponentModel.IContainer
'HINWEIS: Die folgende Prozedur ist für den Windows Form-Designer erforderlich
'Sie kann mit dem Windows Form-Designer modifiziert werden.
'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
Friend WithEvents MainMenu As System.Windows.Forms.MainMenu
Friend WithEvents MenuItemFile As System.Windows.Forms.MenuItem
Friend WithEvents MenuItemExit As System.Windows.Forms.MenuItem
Friend WithEvents StatusBar As System.Windows.Forms.StatusBar
Friend WithEvents CanMsgListView As System.Windows.Forms.ListView
Friend WithEvents ColumnHeaderMsg As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeaderLength As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeaderData As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeaderTime As System.Windows.Forms.ColumnHeader
Friend WithEvents MenuItemDevice As System.Windows.Forms.MenuItem
Friend WithEvents MenuItemConnect As System.Windows.Forms.MenuItem
Friend WithEvents MenuItemSendMsg As System.Windows.Forms.MenuItem
Friend WithEvents StatusBarPanelText As System.Windows.Forms.StatusBarPanel
Friend WithEvents StatusBarPanelCode As System.Windows.Forms.StatusBarPanel
Friend WithEvents MenuItemResetCan As System.Windows.Forms.MenuItem
Friend WithEvents MenuItemHwInfo As System.Windows.Forms.MenuItem
Friend WithEvents ColumnHeaderChannel As System.Windows.Forms.ColumnHeader
Friend WithEvents StatusBarPanelPID As System.Windows.Forms.StatusBarPanel
Friend WithEvents MenuItemShutdown As System.Windows.Forms.MenuItem
Friend WithEvents MenuItemTxEchoMode As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.MainMenu = New System.Windows.Forms.MainMenu(Me.components)
Me.MenuItemFile = New System.Windows.Forms.MenuItem
Me.MenuItemExit = New System.Windows.Forms.MenuItem
Me.MenuItemDevice = New System.Windows.Forms.MenuItem
Me.MenuItemTxEchoMode = New System.Windows.Forms.MenuItem
Me.MenuItemConnect = New System.Windows.Forms.MenuItem
Me.MenuItemSendMsg = New System.Windows.Forms.MenuItem
Me.MenuItemResetCan = New System.Windows.Forms.MenuItem
Me.MenuItemHwInfo = New System.Windows.Forms.MenuItem
Me.MenuItemShutdown = New System.Windows.Forms.MenuItem
Me.StatusBar = New System.Windows.Forms.StatusBar
Me.StatusBarPanelText = New System.Windows.Forms.StatusBarPanel
Me.StatusBarPanelCode = New System.Windows.Forms.StatusBarPanel
Me.StatusBarPanelPID = New System.Windows.Forms.StatusBarPanel
Me.CanMsgListView = New System.Windows.Forms.ListView
Me.ColumnHeaderChannel = New System.Windows.Forms.ColumnHeader
Me.ColumnHeaderMsg = New System.Windows.Forms.ColumnHeader
Me.ColumnHeaderLength = New System.Windows.Forms.ColumnHeader
Me.ColumnHeaderData = New System.Windows.Forms.ColumnHeader
Me.ColumnHeaderTime = New System.Windows.Forms.ColumnHeader
CType(Me.StatusBarPanelText, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.StatusBarPanelCode, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.StatusBarPanelPID, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'MainMenu
'
Me.MainMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItemFile, Me.MenuItemDevice})
'
'MenuItemFile
'
Me.MenuItemFile.Index = 0
Me.MenuItemFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItemExit})
Me.MenuItemFile.Text = "&File"
'
'MenuItemExit
'
Me.MenuItemExit.Index = 0
Me.MenuItemExit.Text = "E&xit"
'
'MenuItemDevice
'
Me.MenuItemDevice.Index = 1
Me.MenuItemDevice.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItemTxEchoMode, Me.MenuItemConnect, Me.MenuItemSendMsg, Me.MenuItemResetCan, Me.MenuItemHwInfo, Me.MenuItemShutdown})
Me.MenuItemDevice.Text = "&Device"
'
'MenuItemTxEchoMode
'
Me.MenuItemTxEchoMode.Index = 0
Me.MenuItemTxEchoMode.Shortcut = System.Windows.Forms.Shortcut.CtrlT
Me.MenuItemTxEchoMode.Text = "&Tx Echo Mode"
'
'MenuItemConnect
'
Me.MenuItemConnect.Index = 1
Me.MenuItemConnect.Shortcut = System.Windows.Forms.Shortcut.CtrlC
Me.MenuItemConnect.Text = "&Connect"
'
'MenuItemSendMsg
'
Me.MenuItemSendMsg.Index = 2
Me.MenuItemSendMsg.Shortcut = System.Windows.Forms.Shortcut.CtrlS
Me.MenuItemSendMsg.Text = "&Send CAN Message"
'
'MenuItemResetCan
'
Me.MenuItemResetCan.Index = 3
Me.MenuItemResetCan.Shortcut = System.Windows.Forms.Shortcut.CtrlR
Me.MenuItemResetCan.Text = "&Reset CAN"
'
'MenuItemHwInfo
'
Me.MenuItemHwInfo.Index = 4
Me.MenuItemHwInfo.Shortcut = System.Windows.Forms.Shortcut.CtrlI
Me.MenuItemHwInfo.Text = "Hardware &Info"
'
'MenuItemShutdown
'
Me.MenuItemShutdown.Index = 5
Me.MenuItemShutdown.Shortcut = System.Windows.Forms.Shortcut.CtrlD
Me.MenuItemShutdown.Text = "&Disconnect"
'
'StatusBar
'
Me.StatusBar.Location = New System.Drawing.Point(0, 166)
Me.StatusBar.Name = "StatusBar"
Me.StatusBar.Panels.AddRange(New System.Windows.Forms.StatusBarPanel() {Me.StatusBarPanelText, Me.StatusBarPanelCode, Me.StatusBarPanelPID})
Me.StatusBar.ShowPanels = True
Me.StatusBar.Size = New System.Drawing.Size(769, 24)
Me.StatusBar.TabIndex = 0
Me.StatusBar.Text = "Not Connected"
'
'StatusBarPanelText
'
Me.StatusBarPanelText.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring
Me.StatusBarPanelText.Name = "StatusBarPanelText"
Me.StatusBarPanelText.Text = "NotConnected"
Me.StatusBarPanelText.Width = 458
'
'StatusBarPanelCode
'
Me.StatusBarPanelCode.Alignment = System.Windows.Forms.HorizontalAlignment.Center
Me.StatusBarPanelCode.Name = "StatusBarPanelCode"
Me.StatusBarPanelCode.Text = "00"
Me.StatusBarPanelCode.ToolTipText = "USBCAN32.DLL ErrorCode"
Me.StatusBarPanelCode.Width = 23
'
'StatusBarPanelPID
'
Me.StatusBarPanelPID.Alignment = System.Windows.Forms.HorizontalAlignment.Center
Me.StatusBarPanelPID.Name = "StatusBarPanelPID"
Me.StatusBarPanelPID.Text = "0ABC"
Me.StatusBarPanelPID.ToolTipText = "ProcessID"
Me.StatusBarPanelPID.Width = 36
'
'CanMsgListView
'
Me.CanMsgListView.AllowColumnReorder = True
Me.CanMsgListView.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeaderChannel, Me.ColumnHeaderMsg, Me.ColumnHeaderLength, Me.ColumnHeaderData, Me.ColumnHeaderTime})
Me.CanMsgListView.Dock = System.Windows.Forms.DockStyle.Fill
Me.CanMsgListView.FullRowSelect = True
Me.CanMsgListView.GridLines = True
Me.CanMsgListView.Location = New System.Drawing.Point(0, 0)
Me.CanMsgListView.Name = "CanMsgListView"
Me.CanMsgListView.Size = New System.Drawing.Size(769, 166)
Me.CanMsgListView.TabIndex = 1
Me.CanMsgListView.UseCompatibleStateImageBehavior = False
Me.CanMsgListView.View = System.Windows.Forms.View.Details
'
'ColumnHeaderChannel
'
Me.ColumnHeaderChannel.Text = "Ch"
Me.ColumnHeaderChannel.Width = 25
'
'ColumnHeaderMsg
'
Me.ColumnHeaderMsg.Text = "Msg"
Me.ColumnHeaderMsg.Width = 75
'
'ColumnHeaderLength
'
Me.ColumnHeaderLength.Text = "Length"
Me.ColumnHeaderLength.Width = 47
'
'ColumnHeaderData
'
Me.ColumnHeaderData.Text = "Data"
Me.ColumnHeaderData.Width = 146
'
'ColumnHeaderTime
'
Me.ColumnHeaderTime.Text = "Time"
Me.ColumnHeaderTime.Width = 82
'
'MainForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(769, 190)
Me.Controls.Add(Me.CanMsgListView)
Me.Controls.Add(Me.StatusBar)
Me.Menu = Me.MainMenu
Me.Name = "MainForm"
Me.Text = "USBcanDemoVB.Net"
CType(Me.StatusBarPanelText, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.StatusBarPanelCode, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.StatusBarPanelPID, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
#Region " MainForm events ################################################################# "
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim currentProcess As Process = Process.GetCurrentProcess()
StatusBarPanelPID.Text = currentProcess.Id.ToString()
MenuItemConnect.Enabled = True
MenuItemShutdown.Enabled = False
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub MainForm_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
' tidy up all resources
DeleteAllResources()
End Sub
#End Region
#Region " MenuItem events ################################################################# "
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub MenuItemConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemConnect.Click
' connect to USB-CANmodul (any modul)
Dim bRet As Byte = 0
Dim ucanMode As UcanDotNET.USBcanServer.tUcanMode
If Me.m_fIsInst1Initialized <> True Then
' check if TxEchoMode should be enabled
If (MenuItemTxEchoMode.Checked() = True) Then
ucanMode = UcanDotNET.USBcanServer.tUcanMode.kUcanModeTxEcho
m_fTxEchoMode = True
Else
ucanMode = UcanDotNET.USBcanServer.tUcanMode.kUcanModeNormal
m_fTxEchoMode = False
End If
' init hardware
Me.m_USBcanInst1 = New UcanDotNET.USBcanServer
Me.m_USBcanInst2 = New UcanDotNET.USBcanServer ' you need to call InitHardware/InitCan methods to initialize the second instance, not done in this demo!
UcanDotNET.USBcanServer.SetDebugMode(&HC0000306, ".\MyLogFile.log")
bRet = Me.m_USBcanInst1.InitHardware(UcanDotNET.USBcanServer.USBCAN_ANY_MODULE)
If (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) Then
Debug.WriteLine("InitCan(): baudrate is " + UcanDotNET.USBcanServer.GetBaudrateMessage(UcanDotNET.USBcanServer.eUcanBaudrate.USBCAN_BAUD_500kBit))
' init CAN channel 0
'UcanDotNET.USBcanServer.USBCAN_BAUD_1MBit, _
bRet = Me.m_USBcanInst1.InitCan(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH0, _
UcanDotNET.USBcanServer.eUcanBaudrate.USBCAN_BAUD_1MBit, _
UcanDotNET.USBcanServer.eUcanBaudrateEx.USBCAN_BAUDEX_USE_BTR01, _
UcanDotNET.USBcanServer.USBCAN_AMR_ALL, _
UcanDotNET.USBcanServer.USBCAN_ACR_ALL, _
ucanMode)
If (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) Then
Dim HwInfo As UcanDotNET.USBcanServer.tUcanHardwareInfoEx
Dim CanInfoCh0 As UcanDotNET.USBcanServer.tUcanChannelInfo
Dim CanInfoCh1 As UcanDotNET.USBcanServer.tUcanChannelInfo
bRet = Me.m_USBcanInst1.GetHardwareInfo(HwInfo, CanInfoCh0, CanInfoCh1)
If (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) Then
m_DeviceNrInst1 = HwInfo.m_bDeviceNr ' is used to check the instance in event handles
End If
If ((bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) AndAlso _
(UcanDotNET.USBcanServer.CheckSupportCyclicMsg(HwInfo) = True)) Then
Dim aCyclicCanMsg(1) As UcanDotNET.USBcanServer.tCanMsgStruct
Dim aCyclicCanMsgReadBack(1) As UcanDotNET.USBcanServer.tCanMsgStruct
aCyclicCanMsg(0) = UcanDotNET.USBcanServer.tCanMsgStruct.CreateInstance(&H61F)
aCyclicCanMsg(0).m_bData(0) = &H40
aCyclicCanMsg(0).m_bData(1) = &H0
aCyclicCanMsg(0).m_bData(2) = &H10
aCyclicCanMsg(0).m_bData(3) = &H0
aCyclicCanMsg(0).m_dwTime = 1000 ' 1 second
aCyclicCanMsg(1) = UcanDotNET.USBcanServer.tCanMsgStruct.CreateInstance(&H71F, UcanDotNET.USBcanServer.eUcanMsgFrameFormat.USBCAN_MSG_FF_RTR)
aCyclicCanMsg(1).m_bDLC = 1
aCyclicCanMsg(1).m_dwTime = 1200 ' 1.2 seconds
bRet = Me.m_USBcanInst1.DefineCyclicCanMsg(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH0, aCyclicCanMsg)
bRet = Me.m_USBcanInst1.ReadCyclicCanMsg(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH0, aCyclicCanMsgReadBack)
bRet = Me.m_USBcanInst1.EnableCyclicCanMsg(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH0, _
UcanDotNET.USBcanServer.eUcanCyclicFlags.USBCAN_CYCLIC_FLAG_START)
End If
' init CAN channel 1
bRet = Me.m_USBcanInst1.InitCan(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH1, _
UcanDotNET.USBcanServer.eUcanBaudrate.USBCAN_BAUD_1MBit, _
UcanDotNET.USBcanServer.eUcanBaudrateEx.USBCAN_BAUDEX_USE_BTR01, _
UcanDotNET.USBcanServer.USBCAN_AMR_ALL, _
UcanDotNET.USBcanServer.USBCAN_ACR_ALL, _
ucanMode)
If (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) Then
' multi channel USB-CANmodul is present
Me.m_f2ndChannelInst1 = True
Me.StatusBarPanelText.Text = "Successfully connected to multi-channel USB-CANmodul"
Else
' single channel USB-CANmodul is present
Me.StatusBarPanelText.Text = "Successfully connected to single-channel USB-CANmodul"
End If
Me.m_fIsInst1Initialized = True
Me.m_RxThread = New Thread(AddressOf Me.ThreadProcReceive)
Me.m_RxThread.Start()
Else ' error while CAN channel 0 init
Me.m_USBcanInst1.Dispose()
Me.m_USBcanInst1 = Nothing
Me.StatusBarPanelText.Text = "Initialisation of CAN failed"
End If
Else ' error while hardware init
Me.m_USBcanInst1.Dispose()
Me.m_USBcanInst1 = Nothing
Me.StatusBarPanelText.Text = "Initialisation of Hardware failed"
End If
Me.StatusBarPanelCode.Text = bRet.ToString("X2")
If m_fIsInst1Initialized = True Then
MenuItemConnect.Enabled = False
MenuItemShutdown.Enabled = True
End If
Else
Me.StatusBarPanelText.Text = "Already Connected"
End If
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub MenuItemExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemExit.Click
' close the window
Me.Close()
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub CanMsgListView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As EventArgs) Handles CanMsgListView.SelectedIndexChanged
' show the number of selected messages in the statusbar
Dim iCount As Integer = CanMsgListView.SelectedItems.Count
If iCount > 0 Then
Me.StatusBarPanelText.Text = iCount & " messages were selected"
Else
Me.StatusBarPanelText.Text = "No messages are selected"
End If
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub MenuItemSendMsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemSendMsg.Click
' User wishes to send a CAN message
If (m_fIsInst1Initialized = True) Then
Dim bRet As Byte = 0
' fill message data with current counter value
m_canMsgStruct(0).m_bData(0) = (m_dwMsgCount >> 24) And &HFF
m_canMsgStruct(0).m_bData(1) = (m_dwMsgCount >> 16) And &HFF
m_canMsgStruct(0).m_bData(2) = (m_dwMsgCount >> 8) And &HFF
m_canMsgStruct(0).m_bData(3) = (m_dwMsgCount) And &HFF
' send message
bRet = m_USBcanInst1.WriteCanMsg(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH0, m_canMsgStruct)
If (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) Then
Me.StatusBarPanelText.Text = "Sent CAN message"
Me.StatusBarPanelCode.Text = bRet.ToString("X2")
SyncLock (Me.m_USBcanInst1)
If (m_fTxEchoMode = False) Then
' add this message to list
addCanMessage(m_canMsgStruct(0), 0, False)
' ensure visibility of last message in list
CanMsgListView.EnsureVisible(CanMsgListView.Items.Count() - 1)
End If
End SyncLock
Else ' error
Me.StatusBarPanelText.Text = "Error while sending CAN message"
Me.StatusBarPanelCode.Text = bRet.ToString("X2")
End If
' increment message counter
m_dwMsgCount += 1
End If
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub MenuItemResetCan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemResetCan.Click
' User wants to reset the USB-CANmodul
If (m_fIsInst1Initialized = True) Then
' reset CAN channel 0
Dim bRet As Byte = 0
If (Me.m_USBcanInst1.IsCan0Initialized()) Then
bRet = m_USBcanInst1.ResetCan(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH0, UcanDotNET.USBcanServer.eUcanResetFlags.USBCAN_RESET_ONLY_ALL_BUFF)
If (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) Then
Me.StatusBarPanelText.Text = "Successfully reset CAN channel 0"
Me.StatusBarPanelCode.Text = bRet.ToString("X2")
' delete all messages in list
CanMsgListView.Items.Clear()
Else
Me.StatusBarPanelText.Text = "Error while reseting CAN channel 0"
Me.StatusBarPanelCode.Text = bRet.ToString("X2")
End If
End If
'If (m_f2ndChannelInst1 = True) Then
If (Me.m_USBcanInst1.IsCan1Initialized()) Then
bRet = m_USBcanInst1.ResetCan(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH1)
If (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) Then
Me.StatusBarPanelText.Text = "Successfully reset CAN channel 1"
Me.StatusBarPanelCode.Text = bRet.ToString("X2")
' delete all messages in list
CanMsgListView.Items.Clear()
Else
Me.StatusBarPanelText.Text = "Error while reseting CAN channel 1"
Me.StatusBarPanelCode.Text = bRet.ToString("X2")
End If
End If
End If
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub MenuItemHwInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemHwInfo.Click
' Show Hardware Info Dialog
If m_fIsInst1Initialized = True Then
Dim hwInfoForm As HwInfoForm = New HwInfoForm
Me.StatusBarPanelText.Text = "Hardware Info"
hwInfoForm.SetUSBcan(m_USBcanInst1)
hwInfoForm.ShowDialog(Me)
End If
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub MenuItemShutdown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemShutdown.Click
Me.StatusBarPanelText.Text = "USB-CANmodul is being shut down"
' tidy up all resources
DeleteAllResources()
If (Me.m_fIsInst1Initialized = True) Then
Me.MenuItemTxEchoMode.Enabled = True
Me.StatusBarPanelText.Text = "Not Connected"
End If
MenuItemConnect.Enabled = True
MenuItemShutdown.Enabled = False
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub MenuItemTxEchoMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemTxEchoMode.Click
' toggle menu item
If (Me.MenuItemTxEchoMode.Checked() = True) Then
Me.MenuItemTxEchoMode.Checked = False
Else
Me.MenuItemTxEchoMode.Checked = True
End If
End Sub
#End Region
#Region " USBCAN events ################################################################### "
' NOTE:
' Do not call any API functions within an USBCAN event handler to prevent dead locks
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub USBcan_CanMsgReceived(ByVal bDeviceNr_p As Byte, ByVal bChannel_p As Byte) Handles m_USBcanInst1.CanMsgReceivedEvent
If (Me.m_DeviceNrInst1 = bDeviceNr_p) Then
' signal event for received CAN message(s)
Dim are As AutoResetEvent = CType(Me.m_RxWaitHandles(1), AutoResetEvent)
are.Set()
End If
End Sub
' NOTE:
' Do not call any API functions within an USBCAN event handler to prevent dead locks
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
'Private Sub USBcan_CanMsgReceived(ByVal bChannel_p As Byte) Handles m_USBcanInst1.CanMsgReceivedEvent
'
' ' at least one message can be read
' Dim canMsgStruct(4) As UcanDotNET.USBcanServer.tCanMsgStruct
' Dim dwCount As Integer = 0
' Dim bRet As Byte = 0
' Dim iIndexCanMsgStruct As Integer = 0
' Dim bOldChannel As Integer = bChannel_p
'
' Do ' loop until there are no more messages in queue
'
' bChannel_p = bOldChannel
'
' ' read message from queue
' bRet = Me.m_USBcanInst1.ReadCanMsg(bChannel_p, canMsgStruct, dwCount)
' If (bRet = UcanDotNET.USBcanServer.USBCAN_SUCCESSFUL) Then
'
' Me.StatusBarPanelCode.Text = bRet.ToString("X2")
' Me.StatusBarPanelText.Text = dwCount.ToString() + " new CAN message received"
'
' SyncLock (Me.m_USBcanInst1)
' ' add messages to list
' For iIndexCanMsgStruct = 0 To dwCount - 1
' addCanMessage(canMsgStruct(iIndexCanMsgStruct), bChannel_p)
' Next
' End SyncLock
'
' ElseIf (bRet = UcanDotNET.USBcanServer.USBCAN_WARN_NODATA) Then
' ' Ok, but message queue is empty
' Else ' error
' Me.StatusBarPanelCode.Text = bRet.ToString("X2")
' Me.StatusBarPanelText.Text = "Error while reading CAN message"
' End If
'
' Loop While (bRet = UcanDotNET.USBcanServer.USBCAN_SUCCESSFUL)
'
' ' ensure visibility of last message in list
' If (CanMsgListView.Items.Count() > 0) Then
' CanMsgListView.EnsureVisible(CanMsgListView.Items.Count() - 1)
' End If
'
'End Sub
' NOTE:
' Do not call any API functions within an USBCAN event handler to prevent dead locks
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub USBcan_FatalDisconnect(ByVal bDeviceNr_p As Byte) Handles m_USBcanInst1.FatalDisconnectEvent
' USB-CANmodul was disconnected
Me.EditStatusBar(0, "USB-CANmodul (Device No. " + bDeviceNr_p.ToString() + ") was manually disconnected!")
' tidy up all resources
DeleteAllResources()
MenuItemConnect.Enabled = True
MenuItemShutdown.Enabled = False
End Sub
' NOTE:
' Do not call any API functions within an USBCAN event handler to prevent dead locks
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub USBcan_Status(ByVal bDeviceNr_p As Byte, ByVal bChannel_p As Byte) Handles m_USBcanInst1.StatusEvent
' signal event for status changed
Dim are As AutoResetEvent = CType(Me.m_RxWaitHandles(2), AutoResetEvent)
are.Set()
End Sub
' NOTE:
' Do not call any API functions within an USBCAN event handler to prevent dead locks
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
'Private Sub USBcan_Status(ByVal bChannel_p As Byte) Handles m_USBcanInst1.StatusEvent
'
' ' status of USB-CANmodul changed
' Dim status As UcanDotNET.USBcanServer.tStatusStruct
' Dim bRet As Byte = 0
'
' Console.WriteLine("Status channel: " + bChannel_p.ToString())
' bRet = m_USBcanInst1.GetStatus(bChannel_p, status)
' If (bRet = UcanDotNET.USBcanServer.USBCAN_SUCCESSFUL) Then
' Me.StatusBarPanelText.Text = "CAN status of channel " + bChannel_p.ToString() + ": " + status.m_wCanStatus.ToString("X4")
' Me.StatusBarPanelCode.Text = bRet.ToString("X2")
' Else
' Me.StatusBarPanelText.Text = "Error while reading status"
' Me.StatusBarPanelCode.Text = bRet.ToString("X2")
' End If
'
'End Sub
#End Region
#Region " Receiver thread ################################################################# "
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Public Sub ThreadProcReceive()
Dim fDoExit As Boolean = False
Do
' wait for receiving messages or exiting this thread
Dim index As Integer = WaitHandle.WaitAny(Me.m_RxWaitHandles)
If (index = 0) Then
' exit this thread
fDoExit = True
ElseIf (index = 1) Then
' receive all messages
ReceiveAllCanMessages()
ElseIf (index = 2) Then
' exit this thread
ReceiveStatus(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH0)
ReceiveStatus(UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_CH1)
End If
Loop While (fDoExit = False)
End Sub
#End Region
#Region " Subroutines and functions ####################################################### "
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub ReceiveAllCanMessages()
' at least one message can be read
Dim canMsgStruct(4) As UcanDotNET.USBcanServer.tCanMsgStruct
Dim dwCount As Integer = 0
Dim bRet As Byte = 0
Dim iIndexCanMsgStruct As Integer = 0
Dim bChannel As Byte
Do ' loop until there are no more messages in queue
' read message from queue
bChannel = UcanDotNET.USBcanServer.eUcanChannel.USBCAN_CHANNEL_ANY
bRet = Me.m_USBcanInst1.ReadCanMsg(bChannel, canMsgStruct, dwCount)
If (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) Then
'Me.StatusBarPanelCode.Text = bRet.ToString("X2")
'Me.StatusBarPanelText.Text = dwCount.ToString() + " new CAN message received"
Me.EditStatusBar(bRet, dwCount.ToString() + " new CAN message received")
' add messages to list
For iIndexCanMsgStruct = 0 To dwCount - 1
addCanMessage(canMsgStruct(iIndexCanMsgStruct), bChannel)
Next
ElseIf (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_WARN_NODATA) Then
' Ok, but message queue is empty
Else ' error
'Me.StatusBarPanelCode.Text = bRet.ToString("X2")
'Me.StatusBarPanelText.Text = "Error while reading CAN message"
Me.EditStatusBar(bRet, "Error while reading CAN message")
End If
Loop While (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL)
' ensure visibility of last message in list
CanMsgListView.Invoke(New MethodInvoker(AddressOf EnsureVisibleListViewItem))
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Protected Sub addCanMessage(ByRef canMsgStruct_p As UcanDotNET.USBcanServer.tCanMsgStruct, ByVal bChannel_p As Byte, Optional ByVal fRecvd As Boolean = True)
' add the supplied CAN message to the list
Dim astrItems(4) As String
Dim iIndex As Integer
Dim aListViewItem As ListViewItem() ' array
' column 0: Channel
astrItems(0) = bChannel_p.ToString()
' column 1: MsgID
If ((canMsgStruct_p.m_bFF And CType(UcanDotNET.USBcanServer.eUcanMsgFrameFormat.USBCAN_MSG_FF_EXT, Byte)) <> 0) Then
astrItems(1) = "0x" + canMsgStruct_p.m_dwID.ToString("X8")
Else
astrItems(1) = "0x" + canMsgStruct_p.m_dwID.ToString("X3")
End If
' column 2: Length
astrItems(2) = canMsgStruct_p.m_bDLC
' column 3: Data
If (canMsgStruct_p.m_bFF And UcanDotNET.USBcanServer.eUcanMsgFrameFormat.USBCAN_MSG_FF_RTR) <> 0 Then
astrItems(3) = "Remote Request"
Else
astrItems(3) = ""
For iIndex = 0 To canMsgStruct_p.m_bDLC - 1
astrItems(3) = astrItems(3) + canMsgStruct_p.m_bData(iIndex).ToString("X2") + " "
Next
End If
' column 4: Time
If ((canMsgStruct_p.m_bFF And UcanDotNET.USBcanServer.eUcanMsgFrameFormat.USBCAN_MSG_FF_ECHO) <> 0) Then
astrItems(4) = "Ex " + canMsgStruct_p.m_dwTime.ToString()
ElseIf fRecvd = True Then
astrItems(4) = "Rx " + canMsgStruct_p.m_dwTime.ToString()
Else
astrItems(4) = "Tx"
End If
' add new item to list
''''CanMsgListView.Invoke(New DelegateListViewItem(AddressOf AddListViewItem), New ListViewItem(astrItems))
aListViewItem = New ListViewItem() {} ' r.d.: this way is needed because VS2003 results in error "error BC30311" when using the line before
aListViewItem.SetValue(New ListViewItem(astrItems), 0)
CanMsgListView.Invoke(New DelegateListViewItem(AddressOf AddListViewItem), aListViewItem)
End Sub
Private Sub AddListViewItem(ByRef item As ListViewItem)
CanMsgListView.Items.Add(item)
End Sub
Private Sub EnsureVisibleListViewItem()
If (Me.CanMsgListView.Items.Count() > 0) Then
Me.CanMsgListView.EnsureVisible(CanMsgListView.Items.Count() - 1)
End If
End Sub
'--------------------------------------------------------------------------------------------
'
'--------------------------------------------------------------------------------------------
Private Sub ReceiveStatus(ByVal bChannel_p As Integer)
' status of USB-CANmodul changed
Dim status As UcanDotNET.USBcanServer.tStatusStruct
Dim bRet As Byte = 0
'Console.WriteLine("Status channel: " + bChannel_p.ToString())
bRet = Me.m_USBcanInst1.GetStatus(bChannel_p, status)
If (bRet = UcanDotNET.USBcanServer.eUcanReturn.USBCAN_SUCCESSFUL) Then
'Me.StatusBarPanelText.Text = "CAN status of channel " + bChannel_p.ToString() + ": " + status.m_wCanStatus.ToString("X4")
'Me.StatusBarPanelCode.Text = bRet.ToString("X2")
Me.EditStatusBar(bRet, "CAN status of channel " + bChannel_p.ToString() + ": " + status.m_wCanStatus.ToString("X4"))
' debug output to test the conversion funtion
Debug.WriteLine("CAN status: " + UcanDotNET.USBcanServer.GetCanStatusMessage(status.m_wCanStatus))
Else
'Me.StatusBarPanelText.Text = "Error while reading status"
'Me.StatusBarPanelCode.Text = bRet.ToString("X2")
Me.EditStatusBar(bRet, "Error while reading status")
End If
End Sub
Private Sub DeleteAllResources()
' tidy up all resources
If (Me.m_fIsInst1Initialized = True) Then
' abort the receiver thread
Dim are As AutoResetEvent = CType(Me.m_RxWaitHandles(0), AutoResetEvent)
For i As Integer = 1 To 5
are.Set()
'Me.m_RxThread.Abort()
If (m_RxThread.Join(500) = True) Then
Exit For
End If
Next i
Me.m_RxThread = Nothing
' shut down the complete USBCAN interface (means all CAN channels and hardware)
Me.m_USBcanInst1.Shutdown()
Me.m_fIsInst1Initialized = False
Thread.Sleep(500) ' sleep some time to prevent that some other threads are using some form recources
Me.m_USBcanInst1 = Nothing
End If
End Sub
Private m_bErrorCode As Byte
Private m_strStatusText As String
Private Sub ThreadSafeEditStatusBar()
Me.StatusBarPanelCode.Text = m_bErrorCode.ToString("X2")
Me.StatusBarPanelText.Text = m_strStatusText
End Sub
Public Sub EditStatusBar(ByVal bErrorCode_p As Byte, ByVal strStatus_p As String)
SyncLock Me
Me.m_bErrorCode = bErrorCode_p
Me.m_strStatusText = strStatus_p
Me.Invoke(New MethodInvoker(AddressOf ThreadSafeEditStatusBar))
End SyncLock
End Sub
#End Region
Private Sub StatusBar_PanelClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.StatusBarPanelClickEventArgs) Handles StatusBar.PanelClick
End Sub
End Class |