CANLibrary/CANLibrary/example.py
2012-07-06 15:45:14 +02:00

65 lines
1.6 KiB
Python

# -*- coding: UTF-8 -*-
'''
Example for creating and sending CAN messages via PCAN using the CanMessage, CanSymbol and PCan classes.
@author: Christian Sültrop
'''
from CanMessage import CanMessage
from CanSymbol import CanSymbol
from PCan import PcanAdapter
print '\ncreate some messages'
mMotor_1 = CanMessage(0x280, 8, 10)
print 'mMotor_1.Data', mMotor_1.Data
mMotor_2 = CanMessage(0x288, 8, 20)
print 'mMotor_2.Data', mMotor_2.Data
print '\ncreate some symbols'
# for mMotor_1
MO1_Leergas = CanSymbol(0, 1, [0x01], 'MO1_Leergas')
MO1_Sta_Pedal = CanSymbol(1, 1, [0x01], 'MO1_Sta_Pedal')
MO1_Mo_m_ex = CanSymbol(8, 8, [0x00], 'MO1_Mo_m_ex')
value = int(3000*0.25)
MO1_Drehzahl = CanSymbol(16, 16, [value>>8, value&0x00ff], 'MO1_Drehzahl') # split into two bytes
mMotor_1.addSymbol(MO1_Leergas)
mMotor_1.addSymbol(MO1_Sta_Pedal)
mMotor_1.addSymbol(MO1_Mo_m_ex)
mMotor_1.addSymbol(MO1_Drehzahl)
print MO1_Drehzahl.Data
# for mMotor_2
MO2_Kuehlm_T = CanSymbol(8, 8, [int(60*0.75)-48], 'MO2_Kuehlm_T')
mMotor_2.addSymbol( MO2_Kuehlm_T )
mMotor_2.addSymbol( CanSymbol(24, 8, [0], 'MO2_GRA_Soll') )
mMotor_2.addSymbol( CanSymbol(0, 6, [44], 'MO2_CAN_Vers') )
print '\nManipulate Symbols'
# does not work yet! need a way to replace a symbol -> dictionary
MO1_Leergas.SetData( [0x00] )
mMotor_1.addSymbol( MO1_Leergas )
print '\nCreate a PCAN adapter'
pcan = PcanAdapter(PcanAdapter.Baudrate['500k'])
pcan.initialize()
print '\nSend the messages'
mMotor_1.composeData()
pcan.sendMessage(mMotor_1)
mMotor_2.composeData()
pcan.sendMessage(mMotor_2)
print 'end of program'