I am reading Can data with Pcan, and I am transferring my Can data to my Raspberry Pi 4. With this code

import can 
import serial

# CAN bus setup
can_bus = can.interface.Bus(bustype="pcan", channel="PCAN_USBBUS1", bitrate=9600)

# Serial port setup
ser = serial.Serial("ttySO", baudrate=9600)

while True:
    # Read from CAN bus
    received_msg = can_bus.recv()

    if received_msg.arbitration_id == 0x18904001:
        data_byte = received_msg.data[0]
        inst_voltage = ((received_msg.data[2] << 8) | received_msg.data[3]) / 10
        soc = received_msg.data[4]

        # Print CAN data
        print("CAN - Instant Voltage: {:.1f} V, SOC: {}%".format(inst_voltage, soc))

    # Read from serial port
    if ser.isOpen() and ser.in_waiting > 0:
        serial_data = ser.readline().decode().strip()
        print("Serial - Received:", serial_data)

I am trying to transfer that data to my Android app. I was stuck on the connection between the Raspberry Pi and the Android device. My Android device doesn't see the Raspberry Pi. What is wrong here? By the way, I can only use a wired connection. ( I prefer USB )

  • Please edit your question and add more details. What do you want to achieve at the end? What are the Raspberry Pi and the Android device supposed to do? Why don't you do everything on the Raspberry Pi? What kind of data do you want to transfer? How is it related to CAN or PCAN?
    – Bodo
    Aug 28, 2023 at 12:30
  • In your edit you added "Arduino". Is this a third device in your (planned) setup? Or did you confuse Android and Arduino? As I understand the code, it reads a CAN message, prints signal values if the ID matches a value, reads from a serial port and prints the received data. I don't see any relation to an Android app or an Arduino. Do I understand right that you connected a PCAN interface to your Raspberry Pi and that the Python program will run on the Pi? If you don't clarify my questions I cannot help.
    – Bodo
    Aug 28, 2023 at 16:05
  • Sorry sir u right. I write wrong. Yes my setup is reading CAN message from pcan device on raspberry pi. Python program will read the CAN message and then print that data to my android app(battery soc app ). My setup is android Tablet, Raspberry pi, pcan, usb cable.
    – Alper
    Aug 29, 2023 at 5:13
  • Please add all requested information or clarification to the question. It is still unclear why you want to use the Android app instead of doing all/most of the work on the Raspberry Pi. Why are you restricted to a wired connection? It would be much easier to provide the data by some machine-readable protocol via a HTTP(S) connection via WiFi or maybe bluetooth.
    – Bodo
    Aug 29, 2023 at 10:45
  • Hello ı am working on ev car project for my universty. And ı am responsible of multimedia of car. I need CAN data on my multimedia. I am reading car battery soc and some of datas. I make an android app for battery. And ı am controling my car air conditioner. So ı need communication between multimedia ( android tablet)- CAN LINE. At the begining on the project ı was doing this with arduino and mcp2515 module and it works fine. But we are try use raspberry pi 4 ( bcs we wanna lose multiple cards). I can read CAN datas on raspberry. With my python code. But ı cant send that data to my tablet.
    – Alper
    Aug 29, 2023 at 10:57


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.