Problem with Write function

High-speed USB 2.0 to CAN/LIN Interface
Post Reply
MOUHOUBI
Posts: 11
Joined: Wed 15. Jul 2020, 16:58

Problem with Write function

Post by MOUHOUBI » Thu 16. Jul 2020, 11:36

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
Attachments
Capture1.PNG
Capture1.PNG (9.17 KiB) Viewed 1777 times
Capture.PNG
Capture.PNG (31.6 KiB) Viewed 1777 times
Last edited by K.Wagner on Thu 16. Jul 2020, 11:40, edited 1 time in total.
Reason: Code formatting for better reading

K.Wagner
Software Development
Software Development
Posts: 876
Joined: Wed 22. Sep 2010, 13:36

Re: Problem with Write function

Post by K.Wagner » Thu 16. Jul 2020, 11:49

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.
Best regards,
Keneth

Post Reply