Basic Python App

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
blipton
Posts: 5
Joined: Sat 11. Jul 2020, 02:22

Basic Python App

Post by blipton » Sun 23. Aug 2020, 18:53

I downloaded the pcan-basic.zip, but having trouble running the PCANBasic.py example. I've tried python 2.3 and 3.7, but the application just runs and exits without any console or message. I've tried with and without the Peak USB plugged in.

I can't find any documentation on it, how is it meant to be run?

Thanks!

BTW, I'm looking for a very simple python script that loads the windows driver, and sends a pre-canned message!

User avatar
U.Wilhelm
Sales & Support
Sales & Support
Posts: 1190
Joined: Fri 10. Sep 2010, 19:34

Re: Basic Python App

Post by U.Wilhelm » Mon 24. Aug 2020, 09:13

Please read the Onlne Help which is part of the API (CHM File) there you find all you need !

Here the minimal code for Pyhtoin 2.7 and CAN 2.0a/b with 500K
from PCANBasic import *
import time
import sys
import random

# My code
# build a dictionnary for error codes

print("Initializaion of CAN driver")
# Create CAN Connection Object
connect1 = PCANBasic()


# Initialize connection
res = connect1.Initialize(
PCAN_USBBUS1, # Channel USB C1
PCAN_BAUD_500K, # 500K - change for your need
TPCANType(0),0,0)

# Btr0Btr1 : The speed for the communication (BTR0BTR1 code)
# HwType : NON PLUG&PLAY: The type of hardware and operation mode
# Channel = ctypes.c_ubyte(1)

if res == PCAN_ERROR_OK:
print "Successfull initialization"
else:
print "ERROR initialization"
print 'returned value= ', res
print 'returned value= ', hex(res)
print connect1.GetErrorText(res,0x00)


# A CAN message is configured
#
msg = TPCANMsg()
msg.ID = 0x12345678
msg.MSGTYPE = PCAN_MESSAGE_EXTENDED
msg.LEN = 8
msg.DATA[0] = 1
msg.DATA[1] = 2
msg.DATA[2] = 3
msg.DATA[3] = 4
msg.DATA[4] = 5
msg.DATA[5] = 6
msg.DATA[6] = 7
msg.DATA[7] = 8

# The message is sent using the PCAN-USB Channel 1
#
for i in range(0,50,1):
result = connect1.Write(PCAN_USBBUS1,msg)
if result != PCAN_ERROR_OK:
# An error occurred, get a text describing the error and show it
#
result = connect1.GetErrorText(result,0x00)
print result
else:
print "Message", i, "sent successfully"
time.sleep(0.01)
for j in range (0,7,1):
msg.DATA[j] = random.randint(0,255) #i+j


time.sleep(0.2) # release after 200ms
# un-initialize before quiting
res = connect1.Uninitialize(PCAN_USBBUS1)
print hex(res)
--------------------------------
PEAK-System Technik
Technical Support Team
support@peak-system.com
-------------------------------

blipton
Posts: 5
Joined: Sat 11. Jul 2020, 02:22

Re: Basic Python App

Post by blipton » Thu 3. Sep 2020, 05:11

Thanks that worked great!

Post Reply