#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial
Public can As MCP2515
Private timReceive As Timer
Private timSend As Timer
Public bc As ByteConverter
#if Sender
Public MyCAN As ULong = 1
#else if Receiver
Public MyCAN As ULong = 2
#end if
Public StartData As ULong = 0
End Sub
Private Sub AppStart
Serial1.Initialize(256000)
Log("AppStart")
others.Get_MacAddress
Log("Device_ID = ", bc.HexFromBytes(others.MacArray))
'The constants For the baud settings are:
'1 = CAN_BAUD_10K
'2 = CAN_BAUD_50K
'3 = CAN_BAUD_100K
'4 = CAN_BAUD_125K
'5 = CAN_BAUD_250K
'6 = CAN_BAUD_500K
Dim inited As Boolean = can.InitCAN(3)
Log("CAN inited = ", inited)
If inited Then
can.SetCANNormalMode(False)
timReceive.Initialize("timReceive_Tick", 200)
timSend.Initialize("timSend_Tick", 1000)
timReceive.Enabled = True
timSend.Enabled = True
End If
End Sub
Sub timSend_Tick
#if Sender
StartData = StartData + 1
Dim data(4) As Byte
data(0) = StartData
Dim result As Boolean = can.TransmitCANMessage(data, 2, 10)
If result Then
Log("Sent OK: ", StartData)
End If
#end if
End Sub
Sub timReceive_Tick
#if Receiver
Dim result As Boolean = can.ReceiveMessage(10)
'Log("Receiving result=", result)
If result Then
Dim ReceivedAddress As ULong = can.ReceivedAddress
Log("ReceivedAddress=", ReceivedAddress)
If ReceivedAddress = MyCAN Then 'for me
Dim ReceivedData() As Byte = can.ReceivedData
Log("Received from ", ReceivedAddress, ": ", bc.StringFromBytes(ReceivedData))
End If
End If
#end if
End Sub