Hello,
In the PCAN-Linux driver package you will find the python example
in the path:
Code: Select all
peak-linux-driver-8.11.0\libpcanbasic\examples\python
These are GUI-Examples, if you work in terminal or prefer to work in console
you could also use the following code:
Code: Select all
from PCANBasic import *
channel = PCAN_USBBUS1
baud = PCAN_BAUD_500K
## The Plug & Play Channel (PCAN-USB) is initialized
##
#PCANBasicAPI =# The Plug & Play Channel (PCAN-USB) is initialized
#
PCANBasicAPI = PCANBasic()
result = PCANBasicAPI.Initialize(channel, baud)
if result != PCAN_ERROR_OK:
# An error occured, get a text describing the error and show it
#
result = PCANBasicAPI.GetErrorText(result)
print(result[1])
channel = PCAN_USBBUS1
PCANBasicAPI = PCANBasic()
result = PCANBasicAPI.Initialize(channel, baud)
if result != PCAN_ERROR_OK:
# An error occured, get a text describing the error and show it
#
result = PCANBasicAPI.GetErrorText(result)
print(result[1])
else:
print("PCAN-USB was initialized")
else:
print("PCAN-USB was initialized")
# Check the status of the USB Channel
#
result = PCANBasicAPI.GetStatus(channel)
if result == PCAN_ERROR_BUSLIGHT:
print("PCAN-USB (Ch-x): Handling a BUS-LIGHT status...")
elif result == PCAN_ERROR_BUSHEAVY:
print("PCAN-USB (Ch-x): Handling a BUS-HEAVY status...")
elif result == PCAN_ERROR_BUSOFF:
print("PCAN-USB (Ch-x): Handling a BUS-OFF status...")
elif result == PCAN_ERROR_OK:
print("PCAN_USB (Ch-x): Status is OK")
else:
# An error occured, get a text describing the error and show it
#
result = PCANBasicAPI.GetErrorText(result)
print(result[1])
msg = TPCANMsg()
msg.ID = 0x123
msg.MSGTYPE = PCAN_MESSAGE_STANDARD
msg.LEN = 8
msg.DATA[0] = 0x01
msg.DATA[1] = 0x02
msg.DATA[2] = 0x03
msg.DATA[3] = 0x04
msg.DATA[4] = 0x05
msg.DATA[5] = 0x06
msg.DATA[6] = 0x07
msg.DATA[7] = 0x08
#
result = PCANBasicAPI.Write(channel, msg)
if result != PCAN_ERROR_OK:
# An error occured, get a text describing the error and show it
#
result = PCANBasicAPI.GetErrorText(result)
print(result)
else:
print("Message sent successfully")
# All initialized channgels are released
#
result = PCANBasicAPI.Uninitialize(PCAN_NONEBUS)
if result != PCAN_ERROR_OK:
# An error occured, get a text describing the error and show it
#
result = PCANBasicAPI.GetErrorText(result)
print(result)
else:
print("PCAN-USB was UN-initialized")
Just create a python file in
Code: Select all
peak-linux-driver-8.11.0\libpcanbasic\examples\python
and paste the code, save it and run the .py file.
If you receive the following output everything works as expected:
Code: Select all
PCAN-USB was initialized
PCAN_USB (Ch-x): Status is OK
Message sent successfully
PCAN-USB was UN-initialized
Please report back to us with your findings.
Best Regards
Marvin