Simple 1:1 CAN Router script for 2 CAN Channels in PE5

Comprehensive CAN monitor for Windows® and its add-ins: Plotter, CANdb Import, Instruments Panel, and J1939
Post Reply
User avatar
U.Wilhelm
Sales & Support
Sales & Support
Posts: 1317
Joined: Fri 10. Sep 2010, 19:34

Simple 1:1 CAN Router script for 2 CAN Channels in PE5

Post by U.Wilhelm » Fri 8. Mar 2013, 10:20

Here you find a very simple script for PE5 that Route CAN Frames from CAN Channel1 to CAN Channel2. It will help you to setup your own routing project very easy. Please take care that you define the 2 needed Nets up in front in your project file. More info could be found as comment inside the code. The project directory is attached.

Code: Select all

'------------------------------------------------------------------------------
'
' FILE DESCRIPTION: Routefrom CAN1 1:1 to CAN2
' (c) 2013 by PEAK-System Technik GmbH
'
'------------------------------------------------------------------------------

Sub Router1to1()

	' Dim the needed VARs (not a must in VBS)
	Dim MyClient1, MyClient2
	Dim MyConn1, MyConn2
	Dim obj
	Dim RecvMsgClient1, SendMsgClient2
		
	' Create a real PCAN Client for every communication (see PCAN-Stat after start the macro -> very interesting!)
	Set MyClient1 = CreateObject("Pcan3.PCANClient")
	Set MyClient2 = CreateObject("Pcan3.PCANClient")

	' use the PCAN-USB.SYS driver - could also be replaced by PCAN_PCI, PCAN_PCC etc. or you mixed up
	' if you remove this two lines the default driver, which is set in controll panel PCAN2.CPL, will be used
	MyClient1.Device = "pcan_usb"
	MyClient2.Device = "pcan_usb" 

	' give them a name - this is up to you...(you will se the name again in PCAN-Stat)
	MyClient1.Name = "RouterCAN1"
	MyClient2.Name = "RouterCAN2"

	' Connect to the pre defined Nets
	' These Nets have to be setup up in front with the NetCfg Tool - here you could define which USB Device use which NET/Baudrate
	Set MyConn1 = MyClient1.Connections.Add("500K-1")
	Set MyConn2 = MyClient2.Connections.Add("500K-2")

	' If the NET is not available or not present, ERROR and END
	If MyConn1.IsConnected = False Then
   		MsgBox "Cannot connect to Net 500K-1"
   		Exit Sub
	End If
	
	' Aslo for the 2nd Channel
	If MyConn2.IsConnected = False Then
   		MsgBox "Cannot connect to Net 500K-2"
   		Exit Sub
	End If
	
	
	' Now we have generate 2 real CANAPI Clients that are connnected to each CAN Channel (500K-1 and 500K-2)
	' To Simulate a Router 1to1 Device, we need to forward all CAN Frames from NET 500K-1 to NET 500K-2
	' That means, that we read MyConn1 and forward all Messages directly to MyConn2

	' We define 2 CAN Messages, for each Client  
	' For CAN1 we need to receice messages...
	Set RcvMsgClient1  = MyClient1.Messages.Add
	' For CAN2 we need a send message
	Set SendMsgClient2 = MyClient2.Messages.Add
	
	' Now we open the incomming Message Filter for CAN1 complete (if you do not want to forward all Messages
	' this is the right place to use incomming filter setting)
	MyConn1.RegisterMsg &H000, &H1fffffff, TRUE, TRUE  ' open Filter for ALL Messages...
	
	' We use the Macro Windows for text outputs...
	PrintToOutputWindow "Now start 1:1 Routing"
	  
	Do ' main loop forever - until a special CAN ID is receoved or a CAN Error happened 
		Do While not RcvMsgClient1.Read ' Here we read the driver queue until we received a MSG
			Wait(5) ' Prevent 100% CPU load
    	Loop
        If RcvMsgClient1.LastError = pcanErrorOk Then ' did we get a real Message? 
	        With SendMsgClient2 ' now we work with the received Message from CAN1
				.ID = RcvMsgClient1.ID  ' and copy the Values 1:1 to the Messagew for CAN2
				.DLC = RcvMsgClient1.DLC '..you see it is very easy to chage some Data here
				.MsgType = RcvMsgClient1.MsgType 
				.Data(0) = RcvMsgClient1.Data(0) ' you could swap DATA Bytes
				.Data(1) = RcvMsgClient1.Data(1) ' or change the ID etc...
				.Data(2) = RcvMsgClient1.Data(2)
				.Data(3) = RcvMsgClient1.Data(3)
				.Data(4) = RcvMsgClient1.Data(4)
				.Data(5) = RcvMsgClient1.Data(5)
				.Data(6) = RcvMsgClient1.Data(6)
				.Data(7) = RcvMsgClient1.Data(7)
				.Write MyConn2, 0 ' and here we send the CAN Frame (0 = with no delay)
			End With
			PrintToOutputWindow "Receiced ID:" & RcvMsgClient1.ID ' small info in the Macro Window
    	End If    	
    Loop While (RcvMsgClient1.LastError = pcanErrorOk AND RcvMsgClient1.ID <> &H001) ' Stop when receiving ID 0x001 or when CAN 1 have an Error
  	PrintToOutputWindow "Finished " 
	Wait(1000) ' wait one second before we end, so that the driver have the chance to send the last CAN Message
End Sub ' end of Sub
Attachments
Router1to1.zip
Router1:1 PE Project - need 2 CAN Connections
(3.5 KiB) Downloaded 476 times
--------------------------------
PEAK-System Technik
Technical Support Team
support@peak-system.com
-------------------------------

Post Reply