PCAN_ERROR_INITIALIZE

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
aniol2001
Posts: 4
Joined: Mon 18. Mar 2024, 12:01

PCAN_ERROR_INITIALIZE

Post by aniol2001 » Tue 16. Apr 2024, 12:49

I'm currently able to Initialize CAN with python (script 1), but my target is to be able to read the messages written by the script 1 with another independently script 2 in the same computer but when I try to Initialize the Peak with this second script it gives me the error (Indicates that the desired PCAN Channel cannot be connected because it is already in use (PCAN-Basic / PCAN-Light environment)). I don't know if it is possible to do what I am trying to do but, for example, PCAN-View is able to do it.

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: PCAN_ERROR_INITIALIZE

Post by M.Heidemann » Tue 16. Apr 2024, 13:00

Hello,

Just randomly copying and pasting code will not really yield the results you want to achieve,
once a channel is initialized, it is given a handle.

What you are trying to do would require a second PCAN-Interface that will read the messages,
in this case you will need to change the TPCAN_Handle. If you want to keep track of the messages
sent out on the same interface, you will have to handle it within the application itself.

If you want to simply access an already initialized handle by another application, you don't need to initialize it again.

Please read the documentation thoroughly.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

aniol2001
Posts: 4
Joined: Mon 18. Mar 2024, 12:01

Re: PCAN_ERROR_INITIALIZE

Post by aniol2001 » Tue 16. Apr 2024, 16:26

Hi,

I already read the documentation. Firstly I didn’t tried to initialize the channel, but when I wanted to acces to this cannel with the same TPCANHandle as it is initialized firstly it returns that this channel wasn’t initialize yet and can’t read the messages.

I can share the code used if it can be usefull.

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: PCAN_ERROR_INITIALIZE

Post by M.Heidemann » Tue 16. Apr 2024, 16:55

Hi,

Please do, maybe i can be of more help to you when i see
what exactly you are trying to do.

Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

aniol2001
Posts: 4
Joined: Mon 18. Mar 2024, 12:01

Re: PCAN_ERROR_INITIALIZE

Post by aniol2001 » Wed 17. Apr 2024, 08:16

This are the two codes, the sender is the first initialized and the reciver the second one, without initializing the peak, only looking for the chanel where is the specified Peak ID.

Sender

Code: Select all

import cantools
import time
import ctypes
from PCANBasic import *

        
class DEVICE:
   
    def __init__(self):      
        
        self.db = cantools.database.load_file('DBC.dbc')
        self.id = [message.frame_id for message in self.db.messages]

        self.channel = PCAN_USBBUS1
        self.baudrate = PCAN_BAUD_500K
                
        self.h = PCANBasic()
        
        self.DEVICE_ID = 0x4
        channel = self.h.LookUpChannel(b"devicetype=pcan_usb, deviceid=0x4")
        if channel[0] != PCAN_ERROR_OK:
            result = self.h.GetErrorText(channel[0])
            print (result[1])
        else:
            if channel[1].value != PCAN_NONEBUS.value:
                print (f"The channel handle for the PCAN-USB with ID={self.DEVICE_ID} was found")
                self.channel = channel[1]
            else:
                print (f"A PCAN-USB device with ID={self.DEVICE_ID} is not available")
                
        print(self.channel)
        result = self.h.Initialize(self.channel, self.baudrate)
        
        self.data = {}

        if result != PCAN_ERROR_OK:
            print(f"Error initializing CAN channel: {result}")
            exit(1)
        else:
            print(f"Initialized CAN channel: OK")

        if self.h == None:
            exit(1)

        self.time = time.time()
        self.msg = TPCANMsg()
        self.msg.ID = 0x5BF
        self.msg.LEN = 8
        self.msg.DATA = (0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11)

    def send_can_message(self,info):
        
        result = self.h.Write(self.channel, info)

MFL = DEVICE()

while True:
    MFL.send_can_message(MFL.msg)
    time.sleep(0.5)
Reciver

Code: Select all

import cantools
import time
import ctypes
from PCANBasic import *

        
class DEVICE:
    
    def __init__(self):      
        
        self.db = cantools.database.load_file('DBC.dbc')
        self.id = [message.frame_id for message in self.db.messages]

        self.channel = PCAN_USBBUS1
        self.baudrate = PCAN_BAUD_500K
                
        self.h = PCANBasic()
        
        self.DEVICE_ID = 0x4
        channel = self.h.LookUpChannel(b"devicetype=pcan_usb, deviceid=0x4")
        if channel[0] != PCAN_ERROR_OK:
            result = self.h.GetErrorText(channel[0])
            print (result[1])
        else:
            if channel[1].value != PCAN_NONEBUS.value:
                print (f"The channel handle for the PCAN-USB with ID={self.DEVICE_ID} was found")
                self.channel = channel[1]
            else:
                print (f"A PCAN-USB device with ID={self.DEVICE_ID} is not available")

        # result = self.h.Initialize(self.channel, self.baudrate)
        
        self.data = {}
        print(self.channel)
        # if result == PCAN_ERROR_INITIALIZE:
        #     print(f"CAN {self.DEVICE_ID} already initialized")
        
        # elif result != PCAN_ERROR_OK:
        #     print(f"Error initializing CAN channel: {result}")
        #     exit(1)
        # else:
        #     print(f"Initialized CAN channel: OK")

        if self.h == None:
            exit(1)

        self.time = time.time()
        self.msg = TPCANMsg()
        self.msg.ID = 0x5BD
        self.msg.LEN = 4
        self.msg.DATA = (0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11)
    
    def receive_can_message(self, ID = None):
        # Receive a CAN message
        result, msg_recived, time = self.h.Read(self.channel)
        if result == PCAN_ERROR_OK:
            pass
        elif result == PCAN_ERROR_QRCVEMPTY:
            print('None')
            return True
        else:
            print(f"Error receiving CAN FF message: {result}")
            return True

        if ID != None:
            while ID != msg_recived.ID:
                result, msg_recived, time = self.h.Read(self.channel)
            
            return True
        
        if msg_recived.ID in self.id: 
            info = self.db.decode_message(msg_recived.ID, bytes(msg_recived.DATA), decode_choices=False)
            self.data[msg_recived.ID] = info

    def send_can_message(self,info):
        
        result = self.h.Write(self.channel, info)
        print(result)

MFL = DEVICE()

while True:
    # MFL.send_can_message(MFL.msg)
    MFL.receive_can_message()
    time.sleep(0.5)

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: PCAN_ERROR_INITIALIZE

Post by M.Heidemann » Wed 17. Apr 2024, 09:23

Hello,

I understand, no that's not possible. PCANBasic can only access the initialized channel
within the application that initialized it. My previous answer was a bit misleading, as
i wrote "application", i was refering to other functions within the same application, my bad.

In your case, you will have to bring both Tx and Rx into the same application in order for it to work in PCANBasic.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

Post Reply