B4R Question CAN-BUS

janderkan

Well-Known Member
Licensed User
Longtime User
Hi, I have tried to follow this thread : https://www.b4x.com/android/forum/threads/can-bus.86516/

This is my receiving code:

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private Can As MCP2515
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Log("CanBus Connected: ", Can.InitCAN(6))
    Can.SetCANNormalMode(False)
    AddLooper("ReceiveCan")
End Sub

Sub ReceiveCan
    If Can.ReceiveMessage(50) Then
        Log("Can.ReceivedAddress: ",Can.ReceivedAddress)
        Log("Can.ReceivedData: ",Can.ReceivedData)
    End If
End Sub

This is my sending code:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public Can As MCP2515
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Log("CanBus Connected: ", Can.InitCAN(6))
    Can.SetCANNormalMode(False)
    Can.TransmitCANMessage(Array As Byte(48,49,50,51,52,53,54,55,56),715,10)
End Sub

Note that I send 9 bytes.
Canbus only allow 8 bytes, but if I send only 8 bytes nothing is received.

Now I add a pin and send data when the pin is activated:

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public Can As MCP2515
    Private pin As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    pin.Initialize(8,pin.MODE_INPUT_PULLUP)
    pin.AddListener("pin_StateChanged")
    Log("CanBus Connected: ", Can.InitCAN(6))
    Can.SetCANNormalMode(False)
End Sub

Sub pin_StateChanged (State As Boolean)
    Delay(10)
    If Not(State) Then
        Can.TransmitCANMessage(Array As Byte(48,49,50,51,52,53,54,55,56),715,10)
    End If
End Sub

This is not working !

But If I add a Transmit to AppStart, then it works?

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public Can As MCP2515
    Private pin As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    pin.Initialize(8,pin.MODE_INPUT_PULLUP)
    pin.AddListener("pin_StateChanged")
    Log("CanBus Connected: ", Can.InitCAN(6))
    Can.SetCANNormalMode(False)
    Can.TransmitCANMessage(Array As Byte(48,49,50,51,52,53,54,55,56),715,10)  '<---'
End Sub

Sub pin_StateChanged (State As Boolean)
    Delay(10)
    If Not(State) Then
        Can.TransmitCANMessage(Array As Byte(48,49,50,51,52,53,54,55,56),715,10)
    End If
End Sub

If I try to change the sender from a pin to a timer, then it is not working again.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public Can As MCP2515
    Private Tim As Timer
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Log("CanBus Connected: ", Can.InitCAN(6))
    Can.SetCANNormalMode(False)
    Can.TransmitCANMessage(Array As Byte(48,49,50,51,52,53,54,55,56),715,10)
    Tim.Initialize("Tim_Tick",5000)
    Tim.Enabled=True
End Sub

Sub Tim_Tick
    Can.TransmitCANMessage(Array As Byte(48,49,50,51,52,53,54,55,56),715,0)
End Sub
 

NoNickName

Member
Licensed User
I don't know the answer, but for what concerns the timer, please note the Tim_tick is fired even at time = 0, in addition to every 5000ms.
I don't know if this has some relevance to your case.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…