Xcom_Test_Program.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. ##########################################################################################################################################
  4. # Python-File Information #
  5. ##########################################################################################################################################
  6. #
  7. # File: Xcom_Test_Program.py
  8. # Author: Tobias Müller
  9. # Date: 2017.08.09
  10. #
  11. ##########################################################################################################################################
  12. # Requirements #
  13. ##########################################################################################################################################
  14. import sys
  15. import signal
  16. import socket
  17. import os
  18. import time
  19. import ptvsd as debug
  20. import serial as UART
  21. import pigpio as GPIO
  22. from Xcom_API import Xcom_API
  23. ##########################################################################################################################################
  24. # Class Definition & Description #
  25. ##########################################################################################################################################
  26. # This program demonstrate the functionality of the Xcom_API-class. To use this program a Raspberry Pi with a Raspicomm extender module
  27. # and a RS232-bridge called Xcom-232i is required.
  28. ##########################################################################################################################################
  29. # Program Information #
  30. ##########################################################################################################################################
  31. PROG_NAME = 'Xcom_Test_Program'
  32. PROG_VERSION = 'v1.0'
  33. ##########################################################################################################################################
  34. # Debug server-Settings #
  35. ##########################################################################################################################################
  36. debug.enable_attach(secret='Debug')
  37. ##########################################################################################################################################
  38. # GPIO-Server-Setting #
  39. ##########################################################################################################################################
  40. # LED-Ports
  41. LED1 = 18
  42. LED2 = 27
  43. # Detect GPIO-HOST
  44. Hostname = socket.gethostname()
  45. # GPIO-State-Variable
  46. GPIO_ENABLE = False
  47. # GPIO-ERROR_Variable
  48. GPIO_ERR = False
  49. ##########################################################################################################################################
  50. # COM-Port-Setting #
  51. ##########################################################################################################################################
  52. # COM-Parameter-Variable
  53. COM_PARA = {'Port' : '/dev/ttyAMA0',
  54. 'Baudrate' : 115200,
  55. 'Bytesize' : UART.EIGHTBITS,
  56. 'Parity' : UART.PARITY_EVEN,
  57. 'Stopbits' : UART.STOPBITS_ONE,
  58. 'Timeout' : 2,
  59. 'XON_XOFF' : False,
  60. 'RTS_CTS' : False,
  61. 'DSR_DTR' : False}
  62. # COM-State-Variable
  63. COM_ENABLE = False
  64. # COM-ERROR-Variable
  65. COM_ERR = False
  66. COM_TIME_ERR = False
  67. ##########################################################################################################################################
  68. # Global Variable #
  69. ##########################################################################################################################################
  70. object_set = False
  71. port = ''
  72. baudrate = ''
  73. crc = False
  74. source = ''
  75. destination = ''
  76. ##########################################################################################################################################
  77. # Functions #
  78. ##########################################################################################################################################
  79. # exit Programm
  80. def exit():
  81. sys.exit(0)
  82. #print header
  83. def header():
  84. # Use Global-Variable
  85. global port
  86. global baudrate
  87. global object_set
  88. global PROG_NAME
  89. global PROG_VERSION
  90. global COM_ENABLE
  91. global GPIO_ENABLE
  92. global destination
  93. global crc
  94. global source
  95. # clear terminal
  96. os.system('cls' if os.name == 'nt' else 'clear')
  97. #print Header
  98. print('#' *120)
  99. print('#' *120)
  100. print('##',' ' *114,'##')
  101. print('## Welcome to {} version: {}. This program demonstrate the functionality of the {}-class. ##'.format(PROG_NAME, PROG_VERSION[1:],Xcom_API.get_prog_name()))
  102. print('##',' ' *114,'##')
  103. print('## COM-Port: {}{}destination-addr.: {}{}Version Xcom_API: {} ##'.format(port,' '*(26-len(port)),destination,' '*(24-len(destination)),Xcom_API.get_prog_version()[1:]))
  104. print('## Baudrate: {}{}source-addr.: {}{}Object of Xcom_API set: {}{} ##'.format(baudrate,' '*(26-len(baudrate)),source,' '*(29-len(source)),object_set,' '*(object_set)))
  105. print('## COM enable: {}{}CRC-Check enable: {}{}GPIO enable: {}{} ##'.format(COM_ENABLE,' '*(19+COM_ENABLE),crc,' '*(20+crc),GPIO_ENABLE,' '*(GPIO_ENABLE)))
  106. print('##',' ' *114,'##')
  107. print('#' *120)
  108. print('#' *120,'\n')
  109. # setup
  110. def setup():
  111. # Use Global-Variable
  112. global COM_PARA
  113. global port
  114. global baudrate
  115. global destination
  116. global crc
  117. global source
  118. # get COM-Port
  119. if len(sys.argv) >= 2:
  120. port = sys.argv[1]
  121. COM_PARA['Port'] = port
  122. header()
  123. else:
  124. while True:
  125. Buffer = input('Do you want to use COM-Port: /dev/ttyAMA0? (yes/no): ')
  126. if Buffer == 'yes' or Buffer == 'y':
  127. port = COM_PARA['Port']
  128. header()
  129. break
  130. elif Buffer == 'no' or Buffer == 'n':
  131. port = input('Enter a COM-Port: ')
  132. COM_PARA['Port'] = port
  133. header()
  134. break
  135. else:
  136. print('What do you mean?\n')
  137. # get baudrate
  138. if len(sys.argv) >= 3 and (sys.argv[2] == '115200' or sys.argv[2] == '38400'):
  139. baudrate = sys.argv[2]
  140. COM_PARA['Baudrate'] = int(baudrate)
  141. header()
  142. else:
  143. while True:
  144. Buffer = input('Do you want to use a Baudrate of: 115200? (yes/no): ')
  145. if Buffer == 'yes' or Buffer == 'y':
  146. baudrate = str(COM_PARA['Baudrate'])
  147. header()
  148. break
  149. elif Buffer == 'no' or Buffer == 'n':
  150. while True:
  151. baudrate = input('Enter a Baudrate (115200 or 38400): ')
  152. if baudrate == '115200' or baudrate == '38400':
  153. COM_PARA['Baudrate'] = int(baudrate)
  154. header()
  155. break
  156. else:
  157. print('baudrate: {}, is not supported.'.format(baudrate))
  158. break
  159. else:
  160. print('What do you mean?\n')
  161. # get destination-address
  162. if len(sys.argv) >= 4:
  163. destination = sys.argv[3]
  164. header()
  165. else:
  166. while True:
  167. Buffer = input('Do you want to use the destination-address: 101? (yes/no): ')
  168. if Buffer == 'yes' or Buffer == 'y':
  169. destination = str(101)
  170. header()
  171. break
  172. elif Buffer == 'no' or Buffer == 'n':
  173. while True:
  174. destination = input('Enter a destination-address: ')
  175. try:
  176. if isinstance(int(destination),int):
  177. header()
  178. break
  179. except ValueError:
  180. print('destination-address: {}, is not an integer.'.format(destination))
  181. break
  182. else:
  183. print('What do you mean?\n')
  184. # get source-address
  185. if len(sys.argv) >= 5:
  186. source = sys.argv[4]
  187. header()
  188. else:
  189. while True:
  190. Buffer = input('Do you want to use the source-address: 1? (yes/no): ')
  191. if Buffer == 'yes' or Buffer == 'y':
  192. source = str(1)
  193. header()
  194. break
  195. elif Buffer == 'no' or Buffer == 'n':
  196. while True:
  197. source = input('Enter a source-address: ')
  198. try:
  199. if isinstance(int(source),int):
  200. header()
  201. break
  202. except ValueError:
  203. print('source-address: {}, is not an integer.'.format(source))
  204. break
  205. else:
  206. print('What do you mean?\n')
  207. # get crc check state
  208. if len(sys.argv) == 6 and (sys.argv[5] == 'False' or sys.argv[5] == 'True' or sys.argv[5] == '1' or sys.argv[5] == '0'):
  209. if sys.argv[5] == 'True' or sys.argv[5] == '1':
  210. crc = True
  211. else:
  212. crc = False
  213. header()
  214. else:
  215. while True:
  216. Buffer = input('Do you want to activate CRC-Check? (yes/no): ')
  217. if Buffer == 'yes' or Buffer == 'y':
  218. crc = True
  219. header()
  220. break
  221. elif Buffer == 'no' or Buffer == 'n':
  222. crc = False
  223. header()
  224. break
  225. else:
  226. print('What do you mean?\n')
  227. # open GPIOs
  228. def open_gpios(LED1,LED2,Hostname):
  229. # Use Global-Variable
  230. global GPIO_ERR
  231. global GPIO_ENABLE
  232. # Try Function
  233. try:
  234. # Enable GPIO-Server
  235. LED_Handle = GPIO.pi(Hostname)
  236. # GPIO-Mode
  237. LED_Handle.set_mode(LED1, GPIO.OUTPUT)
  238. LED_Handle.set_mode(LED2, GPIO.OUTPUT)
  239. # GPIO-Start-State
  240. LED_Handle.write(LED1, GPIO.LOW)
  241. LED_Handle.write(LED2, GPIO.LOW)
  242. GPIO_ENABLE = True
  243. # print completed task
  244. print('GPIOs on \"{}\" are configured.'.format(Hostname))
  245. # return LED-Handle
  246. return LED_Handle
  247. # Handle Errors
  248. except AttributeError:
  249. print('There is no connection to the \"{}\" possible.\nEnsure that the Deamon-Host-Process\"pigpiod\" has started with root privileges on the Raspberry Pi to connect to the GPIOs.'.format(Hostname))
  250. GPIO_ERR = True
  251. exit()
  252. # close GPIOs
  253. def close_gpios(LED_Handle,Hostname,LED1,LED2):
  254. # Use Global-Variable
  255. global GPIO_ERR
  256. global GPIO_ENABLE
  257. # Try Function
  258. try:
  259. # Reset GPIOs
  260. LED_Handle.clear_bank_1(1<<LED1)
  261. LED_Handle.clear_bank_1(1<<LED2)
  262. # close GPIO-Server
  263. LED_Handle.stop()
  264. GPIO_ENABLE = False
  265. # print completed task
  266. print('GPIOs closed.')
  267. except AttributeError:
  268. print('There is no connection to the \"{}\" possible.\n GPIOs can not be closed safely.'.format(Hostname))
  269. GPIO_ERR = True
  270. exit()
  271. # open COM-Port
  272. def open_com_port(LED_Handle,LED1,COM_PARA):
  273. # Use Global-Variable
  274. global COM_ENABLE
  275. global COM_ERR
  276. # Try Function
  277. try:
  278. # open COM-Port
  279. COM_Handle = UART.Serial(port = COM_PARA['Port'],
  280. baudrate = COM_PARA['Baudrate'],
  281. bytesize = COM_PARA['Bytesize'],
  282. parity = COM_PARA['Parity'],
  283. stopbits = COM_PARA['Stopbits'],
  284. timeout = COM_PARA['Timeout'],
  285. xonxoff = COM_PARA['XON_XOFF'],
  286. rtscts = COM_PARA['RTS_CTS'],
  287. dsrdtr = COM_PARA['DSR_DTR'])
  288. COM_ENABLE = True
  289. # LED1 on
  290. LED_Handle.write(LED1, GPIO.HIGH)
  291. # print completed task
  292. print('COM-Port: \"{}\", is opened.'.format(COM_Handle.port))
  293. # return COM_Handle
  294. return COM_Handle
  295. # Handle Errors
  296. except UART.SerialException:
  297. print('COM-Port: \"{}\", can not be opened. Make sure it is available and not used by other programs.'.format(COM_PARA['Port']))
  298. COM_ERR = True
  299. exit()
  300. except ValueError:
  301. print('COM-Port-Parameters are out of spezifications.')
  302. COM_ERR = True
  303. exit()
  304. except TypeError:
  305. print('Baudrate: {}, is not supported.'.format(COM_PARA['Baudrate']))
  306. COM_ERR = True
  307. exit()
  308. # close COM-Port
  309. def close_com_port(COM_Handle,LED_Handle,LED1):
  310. # Use Global-Variable
  311. global COM_ENABLE
  312. global COM_ERR
  313. # Try Function
  314. try:
  315. # save Port-Name
  316. port = COM_Handle.port
  317. # close COM-Port
  318. COM_Handle.close()
  319. COM_ENABLE = False
  320. # LED1 off
  321. LED_Handle.write(LED1, GPIO.LOW)
  322. # print completed task
  323. print('COM-Port: \"{}\", is closed.'.format(port))
  324. # Handle Errors
  325. except UART.SerialException:
  326. print('COM-Port: \"{}\", can not be closed.'.format(port))
  327. COM_ERR = True
  328. exit()
  329. # write to COM-Port
  330. def com_wite(LED_Handle,LED2,COM_Handle,BUFFER):
  331. # print Byte-Frame
  332. print('\nsend byte-frame:')
  333. print('\n'.join(hex(i) for i in BUFFER))
  334. # LED2 on
  335. LED_Handle.write(LED2, GPIO.HIGH)
  336. # sent Byte-Frame
  337. COM_Handle.write(BUFFER)
  338. # LED2 off
  339. LED_Handle.write(LED2, GPIO.LOW)
  340. # read from COM-Port
  341. def com_read(LED_Handle,LED2,COM_Handle):
  342. # LED2 on
  343. LED_Handle.write(LED2, GPIO.HIGH)
  344. # read Byteframe
  345. BUFFER = COM_Handle.readall()
  346. # LED2 off
  347. LED_Handle.write(LED2, GPIO.LOW)
  348. # print Byte-Frame
  349. print('\nread byte-frame:')
  350. print('\n'.join(hex(i) for i in BUFFER))
  351. # return Buffer
  352. return BUFFER
  353. # Create object of class Xcom_API
  354. def create_obj_of_class(destination,source,crc):
  355. # Use Global-Variable
  356. global object_set
  357. #Try Function
  358. try:
  359. # Create an object of class Xcom_API
  360. OBJ_Handle = Xcom_API(crc=crc,destination=int(destination),source=int(source))
  361. object_set = True
  362. return OBJ_Handle
  363. # Handle Errors
  364. except:
  365. print('Destination- and/or source-address are not supported.')
  366. exit()
  367. # xtender_comm
  368. def xtender_comm(OBJ_Handle, LED_Handle, COM_Handle, LED2):
  369. # Description
  370. while True:
  371. print('This function uses the methods:\n')
  372. print('\'get_write_frame(self, object_id, property_data)\'\n')
  373. print('and\n')
  374. print('\'get_read_frame(self, object_id)\'\n')
  375. print('from the Xcom_API-Class to generate a byte-frame for a read or write instruction. You can decide, whether you use the')
  376. print('generated byte-frame to send it over a serial-connection or not. If not, it will display the generated byte-frame and')
  377. print('close the function\n')
  378. print('If you will send the byteframe, it will decode the answer of the xtender-system with the:\n')
  379. print('\'get_data_from_frame(self, bytearray_of_frame)\'\n')
  380. print('method. If the answer from the xtender-system has an error, the error-message will be displayed with the:\n')
  381. print('\'get_text_from_error_id(self, error_id)\'\n')
  382. print('method, otherwise it display the answer of the received frame. With the:\n')
  383. print('\'get_bin_from_frame_flags(self, bytearray_of_frame)\'\n')
  384. print('and\n')
  385. print('\'get_text_from_frame_flags(self, bytearray_of_frame)\'\n')
  386. print('method the state of the frame-flags will be displayed.\n')
  387. print('You can only use these methods with known parameter-/info-numbers of the Xcom-API-Class, otherwise use the')
  388. print('\'xtender_comm_ext()\' function, which uses the \'extended\' version of the mentioned methods\n\n\n')
  389. Buffer = input('Do you want to continue with this function (yes/no): ')
  390. if Buffer == 'yes' or Buffer == 'y':
  391. header()
  392. break
  393. elif Buffer == 'no' or Buffer =='n':
  394. header()
  395. return
  396. else:
  397. header()
  398. print('What do you mean?\n')
  399. # Ask for task and generate Byte_Frame
  400. while True:
  401. Buffer = input('Type in which frame you want to generate (read/write): ')
  402. if Buffer == 'read' or Buffer == 'r':
  403. try:
  404. header()
  405. print('Known Parameter-Numbers (object_id):')
  406. print(' maximum current of ac source = 1107\n battery charge current = 1138\n smart boost allowed = 1126')
  407. print(' inverter allowed = 1124\n type of detection of grid loss = 1552\n charger allowed = 1125')
  408. print(' charger uses only power from ac = 1646\n ac output voltage = 1286\n inverter frequency = 1112')
  409. print(' transfer relay allowed = 1128\n limitation of the power boost = 1607\n remote entry active = 1545')
  410. print('\nKnown Information-Numbers (object_id):')
  411. print(' battery voltage = 3000\n battery charge current = 3005\n battery temperature = 3001')
  412. print(' battery voltage ripple = 3006\n state of charge = 3007\n number of battery elements = 3050')
  413. print(' input voltage = 3011\n input current = 3012\n input frequency = 3084')
  414. print(' input power = 3138\n output voltage = 3021\n output current = 3022')
  415. print(' output frequency = 3085\n output power = 3139\n operating state = 3028')
  416. print(' boost active = 3019\n state of inverter = 3049\n state of transfer relay = 3020')
  417. print(' state of output relay = 3030\n state of aux relay 1 = 3031\n state of aux relay 2 = 3032')
  418. print(' state of ground relay = 3074\n state of neutral transfer relay = 3075\n state of remote entry = 3086')
  419. Buffer = int(input('\nType in an object_id: '))
  420. Buffer2 = OBJ_Handle.get_read_frame(Buffer)
  421. header()
  422. break
  423. except:
  424. while True:
  425. Buffer = input('\nobject_id unknown, do you want to try it again (yes/no): ')
  426. if Buffer == 'yes' or Buffer == 'y':
  427. header()
  428. break
  429. elif Buffer == 'no' or Buffer =='n':
  430. header()
  431. return
  432. else:
  433. print('\nWhat do you mean?')
  434. elif Buffer == 'write' or Buffer =='w':
  435. try:
  436. header()
  437. print('Known Parameter-Numbers (object_id):')
  438. print(' maximum current of ac source = 1107\n battery charge current = 1138\n smart boost allowed = 1126')
  439. print(' inverter allowed = 1124\n type of detection of grid loss = 1552\n charger allowed = 1125')
  440. print(' charger uses only power from ac = 1646\n ac output voltage = 1286\n inverter frequency = 1112')
  441. print(' transfer relay allowed = 1128\n limitation of the power boost = 1607\n remote entry active = 1545')
  442. Buffer = input('\nType in an object_id: ')
  443. header()
  444. Buffer2 = input('Type in a property_data: ')
  445. try:
  446. Buffer2 = int(Buffer2)
  447. except:
  448. Buffer2 = float(Buffer2)
  449. Buffer = int(Buffer)
  450. Buffer2 = OBJ_Handle.get_write_frame(Buffer,Buffer2)
  451. header()
  452. break
  453. except:
  454. while True:
  455. Buffer = input('\nobject_id unknown or property_data not supported, do you want to try it again (yes/no): ')
  456. if Buffer == 'yes' or Buffer == 'y':
  457. header()
  458. break
  459. elif Buffer == 'no' or Buffer =='n':
  460. header()
  461. return
  462. else:
  463. print('\nWhat do you mean?')
  464. else:
  465. header()
  466. print('What do you mean?\n')
  467. # Ask for send byte-frame
  468. while True:
  469. Buffer = input('Do you want to send the Byte-frame over the serial connection (yes/no): ')
  470. if Buffer == 'yes' or Buffer == 'y':
  471. header()
  472. break
  473. elif Buffer == 'no' or Buffer =='n':
  474. print('\ngenerated byte-frame:')
  475. print('\n'.join(hex(i) for i in Buffer2))
  476. input('\nPress \'Enter\' to exit function.')
  477. header()
  478. return
  479. else:
  480. header()
  481. print('\nWhat do you mean?\n')
  482. # send Byteframe
  483. com_wite(LED_Handle,LED2,COM_Handle,Buffer2)
  484. # read from COM-Port
  485. Buffer2 = com_read(LED_Handle,LED2,COM_Handle)
  486. # continue with progress
  487. input('\nPress \'Enter\' to continue.')
  488. header()
  489. # get data from frame print value
  490. try:
  491. Buffer = OBJ_Handle.get_data_from_frame(Buffer2)
  492. if not Buffer[0]:
  493. print('Answer from xtender:',Buffer[1],'\n')
  494. else:
  495. print('An error with error-number:',hex(Buffer[1]),'occure.\n')
  496. print('Errortext:',OBJ_Handle.get_text_from_error_id(Buffer[1]))
  497. except:
  498. print('Failure with frame.')
  499. input('\nPress \'Enter\' to exit function.')
  500. return
  501. # print frame_flag informations
  502. Buffer = OBJ_Handle.get_bin_from_frame_flags(Buffer2)
  503. print('\nFrame-flags binary:',Buffer)
  504. print('Frame-flags binary expression as text is:')
  505. Buffer2 = OBJ_Handle.get_text_from_frame_flags(Buffer2)
  506. for i in range(0,len(Buffer2)):
  507. print(' {} -> {}'.format(Buffer[len(Buffer)-1-i],Buffer2[i]))
  508. print(' {} -> {}'.format('0','Bit reserved (no information)'))
  509. print(' {} -> {}'.format('0','Bit reserved (no information)'))
  510. input('\nPress \'Enter\' to exit function.')
  511. header()
  512. # xtender_comm_ext
  513. def xtender_comm_ext(OBJ_Handle, LED_Handle, COM_Handle, LED2):
  514. # Description
  515. while True:
  516. print('This function uses the methods:\n')
  517. print('\'get_write_frame_ext(self, object_type, object_id, property_id, property_data, data_format)\'\n')
  518. print('and\n')
  519. print('\'get_read_frame_ext(self, object_type, object_id, property_id)\'\n')
  520. print('from the Xcom_API-Class to generate a byte-frame for a read or write instruction. You can decide, whether you use the')
  521. print('generated byte-frame to send it over a serial-connection or not. If not, it will display the generated byte-frame and')
  522. print('close the function\n')
  523. print('If you will send the byte-frame, it will decode the answer of the xtender-system with the:\n')
  524. print('\'get_data_from_frame_ext(self, bytearray_of_frame, data_format)\'\n')
  525. print('method. If the answer from the xtender-system has an error, the error-message will be displayed with the:\n')
  526. print('\'get_text_from_error_id(self, error_id)\'\n')
  527. print('method, otherwise it display the answer of the received frame. With the:\n')
  528. print('\'get_bin_from_frame_flags(self, bytearray_of_frame)\'\n')
  529. print('and\n')
  530. print('\'get_text_from_frame_flags(self, bytearray_of_frame)\'\n')
  531. print('method the state of the frame-flags will be displayed.\n\n\n')
  532. Buffer = input('Do you want to continue with this function (yes/no): ')
  533. if Buffer == 'yes' or Buffer == 'y':
  534. header()
  535. break
  536. elif Buffer == 'no' or Buffer =='n':
  537. header()
  538. return
  539. else:
  540. header()
  541. print('What do you mean?\n')
  542. # Ask for task and generate Byte_Frame
  543. while True:
  544. Buffer = input('Type in which frame you want to generate (read/write): ')
  545. if Buffer == 'read' or Buffer == 'r':
  546. try:
  547. header()
  548. print('Known object_type:')
  549. print(' info\n parameter\n message\n datalog_field\n datalog_transfer\n')
  550. Buffer = input('Type in an object_type: ')
  551. if Buffer == 'info':
  552. object_type = Xcom_API._object_type_info
  553. elif Buffer == 'parameter':
  554. object_type = Xcom_API._object_type_parameter
  555. elif Buffer == 'message':
  556. object_type = Xcom_API._object_type_message
  557. elif Buffer == 'datalog_field':
  558. object_type = Xcom_API._object_type_datalog_field
  559. elif Buffer == 'datalog_transfer':
  560. object_type = Xcom_API._object_type_datalog_transfer
  561. else:
  562. raise ValueError
  563. header()
  564. object_id = input('Type in an object_id: ')
  565. header()
  566. print('Known property_id:')
  567. print(' value\n string\n value_qsp\n min_qsp\n max_qsp\n level_qsp\n')
  568. Buffer = input('Type in a property_id: ')
  569. if Buffer == 'value':
  570. property_id = Xcom_API._property_id_value
  571. elif Buffer == 'string':
  572. property_id = Xcom_API._property_id_string
  573. elif Buffer == 'value_qsp':
  574. property_id = Xcom_API._property_id_value_qsp
  575. elif Buffer == 'min_qsp':
  576. property_id = Xcom_API._property_id_min_qsp
  577. elif Buffer == 'max_qsp':
  578. property_id = Xcom_API._property_id_max_qsp
  579. elif Buffer == 'level_qsp':
  580. property_id = Xcom_API._property_id_level_qsp
  581. else:
  582. raise ValueError
  583. header()
  584. print('Known data_format:')
  585. print(' bool\n format\n short_int\n enum\n short_enum\n long_enum\n error\n int32\n float\n byte\n')
  586. Buffer = input('Type in a property_id: ')
  587. if Buffer == 'bool':
  588. data_format = Xcom_API._format_bool
  589. elif Buffer == 'format':
  590. data_format = Xcom_API._format_format
  591. elif Buffer == 'short_int':
  592. data_format = Xcom_API._format_short_int
  593. elif Buffer == 'enum':
  594. data_format = Xcom_API._format_enum
  595. elif Buffer == 'short_enum':
  596. data_format = Xcom_API._format_short_enum
  597. elif Buffer == 'long_enum':
  598. data_format = Xcom_API._format_long_enum
  599. elif Buffer == 'error':
  600. data_format = Xcom_API._format_error
  601. elif Buffer == 'int32':
  602. data_format = Xcom_API._format_int32
  603. elif Buffer == 'float':
  604. data_format = Xcom_API._format_float
  605. elif Buffer == 'byte':
  606. data_format = Xcom_API._format_byte
  607. else:
  608. raise ValueError
  609. object_id = int(object_id)
  610. Buffer2 = OBJ_Handle.get_read_frame_ext(object_type, object_id, property_id)
  611. header()
  612. break
  613. except:
  614. while True:
  615. Buffer = input('\nobject_type, object_id, property_id or data_format not supported.\nDo you want to try it again (yes/no): ')
  616. if Buffer == 'yes' or Buffer == 'y':
  617. header()
  618. break
  619. elif Buffer == 'no' or Buffer =='n':
  620. header()
  621. return
  622. else:
  623. print('\nWhat do you mean?')
  624. elif Buffer == 'write' or Buffer =='w':
  625. try:
  626. header()
  627. print('Known object_type:')
  628. print(' info\n parameter\n message\n datalog_field\n datalog_transfer\n')
  629. Buffer = input('Type in an object_type: ')
  630. if Buffer == 'info':
  631. object_type = Xcom_API._object_type_info
  632. elif Buffer == 'parameter':
  633. object_type = Xcom_API._object_type_parameter
  634. elif Buffer == 'message':
  635. object_type = Xcom_API._object_type_message
  636. elif Buffer == 'datalog_field':
  637. object_type = Xcom_API._object_type_datalog_field
  638. elif Buffer == 'datalog_transfer':
  639. object_type = Xcom_API._object_type_datalog_transfer
  640. else:
  641. raise ValueError
  642. header()
  643. object_id = input('Type in an object_id: ')
  644. header()
  645. print('Known property_id:')
  646. print(' value\n string\n value_qsp\n min_qsp\n max_qsp\n level_qsp\n')
  647. Buffer = input('Type in a property_id: ')
  648. if Buffer == 'value':
  649. property_id = Xcom_API._property_id_value
  650. elif Buffer == 'string':
  651. property_id = Xcom_API._property_id_string
  652. elif Buffer == 'value_qsp':
  653. property_id = Xcom_API._property_id_value_qsp
  654. elif Buffer == 'min_qsp':
  655. property_id = Xcom_API._property_id_min_qsp
  656. elif Buffer == 'max_qsp':
  657. property_id = Xcom_API._property_id_max_qsp
  658. elif Buffer == 'level_qsp':
  659. property_id = Xcom_API._property_id_level_qsp
  660. else:
  661. raise ValueError
  662. header()
  663. property_data = input('Type in a property_data: ')
  664. header()
  665. print('Known data_format:')
  666. print(' bool\n format\n short_int\n enum\n short_enum\n long_enum\n error\n int32\n float\n byte\n')
  667. Buffer = input('Type in a property_id: ')
  668. if Buffer == 'bool':
  669. data_format = Xcom_API._format_bool
  670. elif Buffer == 'format':
  671. data_format = Xcom_API._format_format
  672. elif Buffer == 'short_int':
  673. data_format = Xcom_API._format_short_int
  674. elif Buffer == 'enum':
  675. data_format = Xcom_API._format_enum
  676. elif Buffer == 'short_enum':
  677. data_format = Xcom_API._format_short_enum
  678. elif Buffer == 'long_enum':
  679. data_format = Xcom_API._format_long_enum
  680. elif Buffer == 'error':
  681. data_format = Xcom_API._format_error
  682. elif Buffer == 'int32':
  683. data_format = Xcom_API._format_int32
  684. elif Buffer == 'float':
  685. data_format = Xcom_API._format_float
  686. elif Buffer == 'long_enum':
  687. data_format = Xcom_API._format_byte
  688. else:
  689. raise ValueError
  690. try:
  691. property_data = int(property_data)
  692. except:
  693. property_data = float(property_data)
  694. object_id = int(object_id)
  695. Buffer2 = OBJ_Handle.get_write_frame_ext(object_type, object_id, property_id, property_data, data_format)
  696. header()
  697. break
  698. except:
  699. while True:
  700. Buffer = input('\nobject_type, object_id, property_id, property_data or data_format not supported.\nDo you want to try it again (yes/no): ')
  701. if Buffer == 'yes' or Buffer == 'y':
  702. header()
  703. break
  704. elif Buffer == 'no' or Buffer =='n':
  705. header()
  706. return
  707. else:
  708. print('\nWhat do you mean?')
  709. else:
  710. header()
  711. print('What do you mean?\n')
  712. # Ask for send byte-frame
  713. while True:
  714. Buffer = input('Do you want to send the Byte-frame over the serial connection (yes/no): ')
  715. if Buffer == 'yes' or Buffer == 'y':
  716. header()
  717. break
  718. elif Buffer == 'no' or Buffer =='n':
  719. print('\ngenerated byte-frame:')
  720. print('\n'.join(hex(i) for i in Buffer2))
  721. input('\nPress \'Enter\' to exit function.')
  722. header()
  723. return
  724. else:
  725. header()
  726. print('\nWhat do you mean?\n')
  727. # send Byteframe
  728. com_wite(LED_Handle,LED2,COM_Handle,Buffer2)
  729. # read from COM-Port
  730. Buffer2 = com_read(LED_Handle,LED2,COM_Handle)
  731. # continue with progress
  732. input('\nPress \'Enter\' to continue.')
  733. header()
  734. # get data from frame print value
  735. try:
  736. Buffer = OBJ_Handle.get_data_from_frame_ext(Buffer2,data_format)
  737. if not Buffer[0]:
  738. print('Answer from xtender:',Buffer[1],'\n')
  739. else:
  740. print('An error with error-number:',hex(Buffer[1]),'occure.\n')
  741. print('Errortext:',OBJ_Handle.get_text_from_error_id(Buffer[1]))
  742. except:
  743. print('Failure with frame.')
  744. input('\nPress \'Enter\' to exit function.')
  745. return
  746. # print frame_flag informations
  747. Buffer = OBJ_Handle.get_bin_from_frame_flags(Buffer2)
  748. print('\nFrame-flags binary:',Buffer)
  749. print('Frame-flags binary expression as text is:')
  750. Buffer2 = OBJ_Handle.get_text_from_frame_flags(Buffer2)
  751. for i in range(0,len(Buffer2)):
  752. print(' {} -> {}'.format(Buffer[len(Buffer)-1-i],Buffer2[i]))
  753. print(' {} -> {}'.format('0','Bit reserved (no information)'))
  754. print(' {} -> {}'.format('0','Bit reserved (no information)'))
  755. input('\nPress \'Enter\' to exit function.')
  756. header()
  757. ##########################################################################################################################################
  758. # Main-Program #
  759. ##########################################################################################################################################
  760. # Try Main-Function
  761. try:
  762. # print Header
  763. header()
  764. # run setup if to check arguments an create an object of the class Xcom_API
  765. setup()
  766. # Configure GPIOs and COM-Port and object of class
  767. LED_Handle = open_gpios(LED1,LED2,Hostname)
  768. header()
  769. COM_Handle = open_com_port(LED_Handle,LED1,COM_PARA)
  770. header()
  771. OBJ_Handle = create_obj_of_class(destination,source,crc)
  772. header()
  773. # main while loop:
  774. while True:
  775. # print Task
  776. print('Xcom_API-Information-Methods:')
  777. print(' get_source_address()\n get_destination_address()\n is_crc_check_active()\n get_object_counter()\n get_prog_name()\n get_prog_version()\n')
  778. print('Test-Functions:')
  779. print(' xtender_comm()\n xtender_comm_ext()\n')
  780. print('If you want to quit this program type: \'quit\'.\n\n')
  781. # get answer
  782. Buffer = input('Please type in a method you want to use: ')
  783. #Control Buffer and start Function
  784. if Buffer == 'quit' or Buffer == 'q':
  785. del OBJ_Handle
  786. break
  787. elif Buffer == 'get_source_address()':
  788. header()
  789. print('This method returns an integer-value of the source-address of an object.\n')
  790. print('Example-Python-Code:')
  791. print('>>> OBJ_Handle = Xcom_API(source = {})'.format(OBJ_Handle.get_source_address()))
  792. print('>>> Source-Address = OBJ_Handle.get_source_address()')
  793. print('>>> Source-Address')
  794. print(' {}\n'.format(OBJ_Handle.get_source_address()))
  795. input('Press \'Enter\' to continue.')
  796. header()
  797. elif Buffer == 'get_destination_address()':
  798. header()
  799. print('This method returns an integer-value of the destination-address of an object.\n')
  800. print('Example-Python-Code:')
  801. print('>>> OBJ_Handle = Xcom_API(destination = {})'.format(OBJ_Handle.get_destination_address()))
  802. print('>>> Destination-Address = OBJ_Handle.get_destination_address()')
  803. print('>>> Destination-Address')
  804. print(' {}\n'.format(OBJ_Handle.get_destination_address()))
  805. input('Press \'Enter\' to continue.')
  806. header()
  807. elif Buffer == 'is_crc_check_active()':
  808. header()
  809. print('This method returns a boolean-value of the active-state of the CRC-Check of an object.\n')
  810. print('Example-Python-Code:')
  811. print('>>> OBJ_Handle = Xcom_API(crc = {})'.format(int(OBJ_Handle.is_crc_check_active())))
  812. print('>>> CRC-Check = OBJ_Handle.is_crc_check_active()')
  813. print('>>> CRC-Check')
  814. print(' {}\n'.format(OBJ_Handle.is_crc_check_active()))
  815. input('Press \'Enter\' to continue.')
  816. header()
  817. elif Buffer == 'get_object_counter()':
  818. header()
  819. print('This method returns a integer-value of the counter of active-objects of this class.\n')
  820. print('Example-Python-Code:')
  821. print('>>> OBJ_Handle = Xcom_API()')
  822. print('>>> Object-Counter = OBJ_Handle.get_object_counter()')
  823. print('>>> Object-Counter')
  824. print(' {}\n'.format(OBJ_Handle.get_object_counter()))
  825. print('Or without to use an object of the Xcom_API-Class:')
  826. print('>>> Object-Counter = Xcom_API.get_object_counter()')
  827. print('>>> Object-Counter')
  828. print(' {}\n'.format(Xcom_API.get_object_counter()))
  829. input('Press \'Enter\' to continue.')
  830. header()
  831. elif Buffer == 'get_prog_name()':
  832. header()
  833. print('This method returns a string of the of the program-name of this class.\n')
  834. print('Example-Python-Code:')
  835. print('>>> OBJ_Handle = Xcom_API()')
  836. print('>>> Program-Name = OBJ_Handle.get_prog_name()')
  837. print('>>> Program-Name')
  838. print(' {}\n'.format(OBJ_Handle.get_prog_name()))
  839. print('Or without to use an object of the Xcom_API-Class:')
  840. print('>>> Program-Name = Xcom_API.get_prog_name()')
  841. print('>>> Program-Name')
  842. print(' {}\n'.format(Xcom_API.get_prog_name()))
  843. input('Press \'Enter\' to continue.')
  844. header()
  845. elif Buffer == 'get_prog_version()':
  846. header()
  847. print('This method returns a string of the of the program-version of this class.\n')
  848. print('Example-Python-Code:')
  849. print('>>> OBJ_Handle = Xcom_API()')
  850. print('>>> Program-Version = OBJ_Handle.get_prog_version()')
  851. print('>>> Program-Version')
  852. print(' {}\n'.format(OBJ_Handle.get_prog_version()))
  853. print('Or without to use an object of the Xcom_API-Class:')
  854. print('>>> Program-Version = Xcom_API.get_prog_version()')
  855. print('>>> Program-Version')
  856. print(' {}\n'.format(Xcom_API.get_prog_version()))
  857. input('Press \'Enter\' to continue.')
  858. header()
  859. elif Buffer == 'xtender_comm()':
  860. header()
  861. xtender_comm(OBJ_Handle, LED_Handle, COM_Handle, LED2)
  862. header()
  863. elif Buffer == 'xtender_comm_ext()':
  864. header()
  865. xtender_comm_ext(OBJ_Handle, LED_Handle, COM_Handle, LED2)
  866. header()
  867. else:
  868. header()
  869. print('What do you mean?\n')
  870. # exit program
  871. exit()
  872. # Handle Errors
  873. except KeyboardInterrupt:
  874. if signal.SIGINT:
  875. print('\n\nProgram closed with Strg+c.')
  876. # Exit program safely.
  877. finally:
  878. print('\nExit Program.')
  879. if (('COM_Handle' in locals() and COM_Handle and COM_ENABLE and not COM_ERR) or
  880. ('LED_Handle' in locals() and LED_Handle and GPIO_ENABLE and not GPIO_ERR)):
  881. print('\nCleanup work:')
  882. if 'COM_Handle' in locals() and COM_Handle and COM_ENABLE and not COM_ERR:
  883. close_com_port(COM_Handle,LED_Handle,LED1)
  884. if 'LED_Handle' in locals() and LED_Handle and GPIO_ENABLE and not GPIO_ERR:
  885. close_gpios(LED_Handle,Hostname,LED1,LED2)