I see a lot of documentation on receiving CAN but not a lot on sending. Of course, my project involves ONLY sending.
The following is my test program for sending a sample CAN message. After tweaking out the compile errors, the program does not send.
I did set the internal jumpers to to address 0, DC-DC off, isolation disabled, 120R in, TX enabled.
Any suggestions on this? Thanks.
============================================================================
'Declare Constants
'----------------------------------------------------------------------
ConstTable
Const TQuanta = 2 ' set per SDM-CAN Manual, pg 17
Const TSeg1 = 5 ' 500k bit rate
Const TSeg2 = 2
Const Period = 1
Const Addr = 0
Const ID_11 = -16 ' ID = 0x10; use 11-bit ID when (-)
Const SET_SW = 32 ' Set SDM-CAN switches
Const XMT_CAN = 19 ' Send data as UINT, MSB
EndConstTable
'Declare Public Variables
'----------------------------------------------------------------------
Dim DatOut(80) = { 85, 240, 15, 85, 240, 15, 255, 00 }
Dim Switch(1) = { 0104 } ' Return -99999 if no data; Xmt once, no retry
'Main Program
'----------------------------------------------------------------------
BeginProg
Scan (1,Sec,0,0)
SDMCAN (Switch(),Addr,TQuanta,TSeg1,TSeg2,ID_11,SET_SW,1,64,64,1.0,0)
SDMCAN (DatOut(),Addr,TQuanta,TSeg1,TSeg2,ID_11,XMT_CAN,1,64,64,1.0,0)
NextScan
EndProg
===========================================================================
Here is some code I used in a recent program. It is cut from a larger program but I think it is correct.
It sets the switches and then reads them back to make sure they are set. The loops may not be necessary but if the logger is busy or communications interupted they ensure the code executes. I also get the SDM-CAN's signature and version. This is also a reality check that the modules are alive and well.
Public CanSwitchTx, CanSwitchRx 'Config values to send (Tx) and confirm (Rx) on SDM-CAN
Public SDMCANInfo(2) 'Version and Signature values from SDM-CAN
Alias SDMCANInfo(1)=SDMCanSig
Alias SDMCANInfo(2)=SDMCanVer
Dim MyDataRequest(8), i_CANResponse
Public MyDataOut(8)
'--------------------------------------------------------------
'Preliminary CANBUS Configuration Scan(s)
'--------------------------------------------------------------
CanSwitchTx=0004 '0000 'DisableTransmit, UseOldData
'SDM-CAN1 Switch-Setting Scan (EnableTransmit and NANOldData)
'--------------------------------------------------------------
Scan(1,Sec,0,10) 'Set Switches - NAN and TxEnable
'SDMCAN ID 1 = Config SDM-CAN, DataType32 = SetSwitch, NumVals = 1
SDMCAN(CanSwitchTx,CanAddress,CanQuanta,CanSeg1,CanSeg2,1,32,0,0,1,1.0,0.0)
'SDMCAN ID 1 = Config SDM-CAN, DataType33 = ReadSwitch, NumVals = 1
SDMCAN(CanSwitchRx,CanAddress,CanQuanta,CanSeg1,CanSeg2,1,33,0,0,1,1.0,0.0)
If CanSwitchRx = CanSwitchTx Then ExitScan
NextScan
'SDM-CAN Operating System Signature and Version Scan
Scan(1,Sec,0,10)
SDMCAN(SDMCANInfo(),CanAddress,CanQuanta,CanSeg1,CanSeg2,1,30,0,0,0,1.0,0.0)
If SDMCANInfo(1) > 0 Then ExitScan
NextScan
' Within the main scan the following SDMCAN code makes a request from a controller for data at one PGN and then reads 'the data from another PGN. The loop around the read CAN instruction is to give the controller time to respond to the 'request.
Scan(1,Sec,10,0)
SDMCAN(MyDataRequest(),0,CanQuanta,CanSeg1,CanSeg2,&h1800EA00,19,1,8,8,1.0,0.0)
For i_CANRsp= 1 To 100
SDMCAN(MyDataOut(),0,CanQuanta,CanSeg1,CanSeg2,&h18FEE500,2,1,8,8,1.0,0.0)
If MyDataOut(8) > 0 ExitFor
EndIf
Next i_CANRsp
Next Scan
Thanks for your reply!
A question I have on this is do you consider it important to verify logger operation? Have you had instances where the unit did NOT work as expected?
I still don't understand why Campbell needed to shove all the these parameters into ONE function call. How about 2 or 3 separate functions to handle things like bus configuration at the start instead of having to do it at each invocation?
I did get an output to work by trying almost every permutation under the sun. My code for this is below. The start of this was seeing some code to read the battery voltages. This gave me reliable (and changing) values that I could send and verify at the other end,
'------------------- Physical Network Parameters -----------------
Const TQUANT = 4 ' Set SDM-CAN to 250K network speed
Const TSEG1 = 5
Const TSEG2 = 2
Dim BattVolt(2)
___________________________CANbus Block1_________________________
'Send switch value Data type 32
Const ADDR1 = 0 'SDM address of SDM-CAN Module
Const DTYPE1 = 32 'Send switch value
Const STBIT1 = 0 'Start position in data frame
Const NBITS1 = 0 'Number of bits/value
Const NVALS1 = 0 'Number of values
Const CMULT1 = 1.0 'Multiplier
Const COSET1 = 50 'Offset
'___________________________CANbus Block2_________________________
Const ADDR2 = 0 ' SDM address of SDM-CAN Module
Const CMULT2 = 1 ' Multiplier = 1
Const COSET2 = 50 ' Offset = 0
BeginProg 'Program begins here Scan(PERIOD,P_UNITS,0,0) ' Scan once every 1 Secs, non-burst Battery( BattVolt(1), 0) ' Read battery voltage Battery( BattVolt(2), 2) ' Backup clock voltage '__________________________ CAN Blocks __________________________ 'Load variable with value for switches Switches = 3 SDMCAN(Switches,ADDR1,TQUANT,TSEG1,TSEG2,0,DTYPE1,STBIT1,NBITS1,NVALS1,CMULT1,COSET1) 'Transmit Data onto CAN network SDMCAN(BattVolt,ADDR2,TQUANT,TSEG1,TSEG2, &H30, 23, 1, 32, 2, CMULT2,COSET2) ' Creates 8-byte msg with 2 IEEE signals SDMCAN(BattVolt(2),ADDR2,TQUANT,TSEG1,TSEG2,&H40, 23, 1, 32, 1, CMULT2,COSET2) ' Creates 4-byte msg with 1 IEEE signal Next Scan 'Loop up for the next scan EndProg 'Program ends here
Make sure you are using the latest SDM-CAN manual, Revision 8/13.
Some of my code is a matter of programming style and the way I organize and set up calls to the SDMCAN. Having the calls to verify the function and setup are useful when you don't get the results you are expecting. That may be a hardware issue where a connection has been lost. Sometimes that is within the data logger box itself but most of the time it is on the vehicle I am working with.
I work on off-road vehicles. I will set up a logger package in April and may not see it again until November. I put cell phones on all my loggers working away from me. The vehicles work in all conditions from hot Arizona to the cold of Alberta and North Dakota. Other people are with the vehicles and may be making changes to its hardware and software. Sometimes they remove a connector to the logger when they are changing parts and forget to plug it back in. Having diagnostic code in my program gives me a chance to isolate problems if they occur.
As for the reasons behind the structure of the function call, you may need to ask the engineers at Campbell in the UK. They designed the SDMCAN, hardware and software. However, I can envision having the SDMCAN on a network system where those paramters may change. While I have ot done it with CANBUS, I have had serial devices that through a switching arrangement switched networks between function calls.