Page 1 of 1
Sending diffrent CAN messages with an order by a VBS macro
Posted: Mon 26. Mar 2012, 09:54
by engineer
Hello,
I need to send diffrent CAN messages via one Identifier to request a staus. The comunication is based on three CAN messages with different identifier and contents to fullfill the transfere protocoll. The values which are sent back need to be accsociated to certain Variables. The CAN messages are according to J1939. So I need to send one DM14(IDx1) message to request the value. The Value will sent back by a DM16 (IDx2) message and to finisch the comunication there will be an accnoledgement sent by both participants DM15 (IDx3).
How can I realize such a comunication between two CAN participants?
Re: Sending diffrent CAN messages with an order by a VBS mac
Posted: Mon 26. Mar 2012, 15:43
by K.Wolf
Hi,
The VBS macro to accomplish this task could look like this:
Code: Select all
Sub J1939_Sample()
' Initialize J1939 node
Dim node
Set node = CreateObject("J1939Pcan.PCANJ1939Node")
node.Device = tcdUsb
' Device must be the same as the J1939 connection in your project
node.ClientName = "Macro"
node.NetName = "USB1" ' Same as the connection in your project
If node.Connect Then
' Claim an address
node.PreferredAddress = 100
If node.ClaimAddress Then
' Add a filter for PGN 55040 (DM16)
node.Filters.Add tfmPGN, 55040, 0, 0, 0
Dim XmtMsg, AckMsg, RcvMsg
' Initialize DM14 transmit message
Set XmtMsg = CreateObject("J1939Pcan.PCANJ1939Msg")
With XmtMsg
.PGN = 55552
.Priority = 6
.Length = 8
.Originator = node.ClaimedAddress
End With
' Initialize DM15 acknowledge message
Set AckMsg = CreateObject("J1939Pcan.PCANJ1939Msg")
With AckMsg
.PGN = 55296
.Priority = 6
.Length = 8
.Originator = node.ClaimedAddress
End With
' Initialize message object for receiving messages
Set RcvMsg = CreateObject("J1939Pcan.PCANJ1939RcvMsg")
' Transmit the DM14 request and wait for DM16 response
node.Write(XmtMsg)
While Not node.Read(RcvMsg)
Wait 10
Wend
' A DM16 message was received
PrintToOutputWindow "DM16 received"
' ...
' Finally, send the DM15 acknowledge
node.Write(AckMsg)
End If
End If
End Sub
The received DM16 message also appears in the PCAN-Explorer's Receive List, and the DM16 signal values are updated accordingly.
Re: Sending diffrent CAN messages with an order by a VBS mac
Posted: Thu 29. Mar 2012, 11:32
by engineer
Hi,
thank you very much for this input, this is about what I was searching for. But I've got another question regarding the data which is sent by i.e. the DM14 message. Were is the data specified I need to send via a DM14 message? Is it specified in the symbol file?
Re: Sending diffrent CAN messages with an order by a VBS mac
Posted: Fri 30. Mar 2012, 15:46
by K.Wolf
Hi,
if you use this mechanism, you can set the message data only by assigning raw data byte values, e.g.
XmtMsg.Data(0) = &H11
XmtMsg.Data(1) = &H22
If you would like to use the J1939 symbols file, you can also transmit the message via the PCAN-Explorer signals:
Dim sig
Set sig = Signals("DM14.Command")
sig.Value = 1
Make sure you have the J1939Default.sym or a customized J1939Template.sym symbols file assigned to your connection. Setting the Signal.Value property automatically creates a new message in the Transmit List and sends it. The Source address is automatically set to the claimed address or preferred address of the connection on which the message is sent.
If you would like to set more than one signal and would like to send it only once, disable the AutoTransmit property of the associated Variable:
Dim sig1, sig2, sig3
Set sig1 = Signals("DM14.Command")
Set sig2 = Signals("DM14.PointerType")
Set sig3 = Signals("DM14.PointerExtension")
sig1.Source.AutoTransmit = False
sig2.Source.AutoTransmit = False
sig1.Value = 1 ' No message is sent
sig2.Value = 2 ' No message is sent
sig3.Value = 3 ' One message is sent, containing all signal values, AutoTransmit of sig3 is still on