Page 1 of 1

Problem with Write function

Posted: Thu 16. Jul 2020, 11:36
by MOUHOUBI
I have executed the following code to write a CAN message, it has been initialized (we can see it on the message after debugging) but it can't manage to Write a CAN message it puts the translation in French (system language d 'exploitation) as indicated on the documentation screenshot


the code used is as follows:

Code: Select all

from PCANBasic import *        ## PCAN-Basic library import

# The Plug & Play Channel (PCAN-USB) is initialized

objPCAN = PCANBasic()
result = objPCAN.Initialize(PCAN_USBBUS2, PCAN_BAUD_500K)
if result != PCAN_ERROR_OK:
    
    # An error occurred, get a text describing the error and show it

    result = objPCAN.GetErrorText(result)
    print (result[1])
else:
    print ("PCAN-USB (Ch-1) was initialized")


# All initialized channels are released
#objPCAN.Uninitialize(PCAN_NONEBUS)

""""""""""""""""""""""""""""""""""""""""""""""""""

Code: Select all

# A CAN message is configured
msg = TPCANMsg()
msg.ID = 0x100
msg.MSGTYPE = PCAN_MESSAGE_STANDARD
msg.LEN = 3
msg.DATA[0] = 1
msg.DATA[1] = 2
msg.DATA[2] = 3
# The message is sent using the PCAN-USB Channel 1

result = objPCAN.Write(PCAN_USBBUS1,msg)

if result != PCAN_ERROR_OK:
# An error occurred, get a text describing the error and #
    result = objPCAN.GetErrorText(result)
    print (result)
else:
    print ("Message sent successfully")


""""""""""""""""""""""""""""""""""""""""""""""""""""""

Code: Select all

# The USB Channel is released
#
result = objPCAN.Uninitialize(PCAN_USBBUS1)
if result != PCAN_ERROR_OK:
    # An error occurred, get a text describing the error and show it
    #
    result = objPCAN.GetErrorText(result)
    print (result[1])
else:
    print ("PCAN-USB (Ch-1) was released")

You have already helped me a lot before thank you very much

Re: Problem with Write function

Posted: Thu 16. Jul 2020, 11:49
by K.Wagner
Hello,

if you check the help you will see that you cannot send/receive messages using an uninitialized channel. Moreover, you should also check in the documentation for the error codes you are receiving and check yourself what is going wrong (the codes are very descriptive).

At the end of the first code block you are just uninitializing the channel right after initializing it (and before writing anything).
MOUHOUBI wrote:
Thu 16. Jul 2020, 11:36
from PCANBasic import * ## PCAN-Basic library import

# The Plug & Play Channel (PCAN-USB) is initialized

objPCAN = PCANBasic()
result = objPCAN.Initialize(PCAN_USBBUS2, PCAN_BAUD_500K)
if result != PCAN_ERROR_OK:

# An error occurred, get a text describing the error and show it

result = objPCAN.GetErrorText(result)
print (result[1])
else:
print ("PCAN-USB (Ch-1) was initialized")


# All initialized channels are released
#objPCAN.Uninitialize(PCAN_NONEBUS)
PLEASE NOTE: You are just copy/pasting code without understanding what you are doing. Please use the help file as i told you before. Last warning :!:
Taking you by the hand or programming for you is not included in the support service we give.