Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. """
  2. Fnord
  3. """
  4. import binascii
  5. import time
  6. import datetime
  7. import canopen
  8.  
  9. def data_to_int(datain):
  10. """
  11. SDO Data to Integer
  12. """
  13. return int(binascii.hexlify(datain), 16)
  14.  
  15. ### SETTINGS NEDAN ###########################################################
  16.  
  17. # COPID of node
  18. POLLING_NODEID = 0x0f
  19.  
  20. # (Index, SubIndex, Size)
  21. POLLING_SDO = (0x4000, 0x03, 4)
  22.  
  23. # Time between poll, in seconds
  24. POLLING_FREQ = 1.0
  25.  
  26. # Index of byte
  27. POLLING_BYTE_INDEX = 3
  28.  
  29. # Threshold value
  30. POLLING_THRESHOLD = 0x55
  31.  
  32. ##############################################################################
  33.  
  34. # Setup CANOpen Network
  35. NETWORK = canopen.Network()
  36. NETWORK.connect(channel='can0', bustype='socketcan')
  37.  
  38. #
  39. # Create "Object Dictionary" to reduce error messages
  40. #
  41. OD_SDO = canopen.objectdictionary.Variable("fnord", POLLING_SDO[0],
  42. POLLING_SDO[1])
  43. OD_SDO.access_type = "ro"
  44. OD_SDO.add_bit_definition("fnord", range(1, POLLING_SDO[2]*8))
  45. OD = canopen.ObjectDictionary()
  46. OD.add_object(OD_SDO)
  47.  
  48. TESTOBJ = NETWORK.add_node(POLLING_NODEID, OD)
  49.  
  50. print("STARTING POLLING!")
  51. while 1:
  52. time.sleep(POLLING_FREQ)
  53. data = TESTOBJ.sdo.upload(POLLING_SDO[0], POLLING_SDO[1])
  54.  
  55. value = data_to_int(data[POLLING_BYTE_INDEX])
  56. if value < POLLING_THRESHOLD:
  57. print("%s: %02X\n", str(datetime.datetime.now()), value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement