Page 1 of 1

Message counter, VB??

Posted: Mon 4. Jun 2012, 14:56
by Johann
Hello everyone.
I am using a the Explorer 5 to control an application that I am working on, but I need to create a 4bit (0 to 15) counter, that will increment by 1 each time a message is transmitted (every 50 ms). From what I can understand from the different threads in the forum is that I need to create an VB macro, which I have never done before. Therefore I would like to see if there is someone able to help out.

Re: Message counter, VB??

Posted: Mon 4. Jun 2012, 20:51
by PEAK-Support
Please post your request with your Serialnumber of your PE5 via E-Mail to support@peak-system.com
We will then send you a sample how to implement such a function in VBS.

Re: Message counter, VB??

Posted: Tue 5. Jun 2012, 08:55
by PEAK-Support
Here a simple PE5 script that increment a rolling counter (0-15) every time when a CAN ID with 0x100 received. The macro stop when receiving the ID 0x001
Take care that the NET "TestNet" is available and that the Hardware corresponding to this NET is set as active Device in the CPL (Controll Panel Tool from PEAK)

Code: Select all

'---------------------------------------------------------------------------
'FILE DESCRIPTION: CounterDemo Data
'---------------------------------------------------------------------------

Sub CounterDemo()
' DESCRIPTION: Read CAN Data - see if in a defined CAN Msg data one Bit is set
  ' To view the output messages of this macro, open the Output Window and
  '  select the "Macro" tab
  Dim Myclient, MyConn
  Set MyClient = CreateObject("Pcan3.PCANClient")
  MyClient.Name = "CounterDemo"
  If Connections.Count > 0 And Connections(1).Protocol = peProtocolCAN Then
    ' Connect to the same Net that the first connection in the
    '  current project uses
    Dim obj
    Set obj = Connections(1).CommunicationObject
    MyClient.Device = obj.Parent.Device
    Set MyConn = MyClient.Connections.Add(obj.NetName)
  Else
    ' Connect to the default "TestNet"
    Set MyConn = MyClient.Connections.Add("TestNet")
  End IF

  If MyConn.IsConnected = False Then
                MsgBox "Cannot connect to Net"
                Exit Sub
  End If

  ' Set Filter to exact the ID0x000 & 0x800 (you also could change the settings, or remark the complete line for receiving all Messages
  MyConn.RegisterMsg &H000, &H800, False, False
  PrintToOutputWindow "Start the Read...."
  i = 0
  Dim RcvMsg
  Set RcvMsg = MyClient.Messages.Add
  Do
                Do While not RcvMsg.Read
                  ' Wait for a received message
                   Wait(10)           ' Prevent 100% CPU load
    			Loop
                If RcvMsg.LastError = pcanErrorOk Then
                	If RcvMsg.ID = &h100 Then ' only when ID 0x100 received..
                		If RcvMsg.DLC > 0 Then 'end when Data inside the CAN Frame - Yes, >0 DLC ist OK 
               					PrintToOutputWindow "Counter " & i
               					i = i + 1
               					If(i>15) Then
               						 i=0
               					End If
               	    	End If
                    End If  
                End IF
  Loop While RcvMsg.ID <> &H001 ' Stop when receiving ID 0x001
  PrintToOutputWindow "Finished " 
  End Sub