#!/usr/bin/env python3 # -*- coding: utf-8 -*- ########################################################################################################################################## # Python-File Information # ########################################################################################################################################## # # File: Xcom_Test_Program.py # Author: Tobias Müller # Date: 2017.08.09 # ########################################################################################################################################## # Requirements # ########################################################################################################################################## import sys import signal import socket import os import time import ptvsd as debug import serial as UART import pigpio as GPIO from Xcom_API import Xcom_API ########################################################################################################################################## # Class Definition & Description # ########################################################################################################################################## # This program demonstrate the functionality of the Xcom_API-class. It sends permanently a request for a known object_id and display the # answer from the Xtender-System. To use this program a Raspberry Pi with a Raspicomm extender module and a RS232-bridge called # Xcom-232i is required. ########################################################################################################################################## # Program Information # ########################################################################################################################################## PROG_NAME = 'Xcom_Test_Program' PROG_VERSION = 'v1.0' ########################################################################################################################################## # Debug server-Settings # ########################################################################################################################################## debug.enable_attach(secret='Debug') ########################################################################################################################################## # GPIO-Server-Setting # ########################################################################################################################################## # LED-Ports LED1 = 18 LED2 = 27 # Detect GPIO-HOST Hostname = socket.gethostname() # GPIO-State-Variable GPIO_ENABLE = False # GPIO-ERROR_Variable GPIO_ERR = False ########################################################################################################################################## # COM-Port-Setting # ########################################################################################################################################## # COM-Parameter-Variable COM_PARA = {'Port' : '/dev/ttyAMA0', 'Baudrate' : 115200, 'Bytesize' : UART.EIGHTBITS, 'Parity' : UART.PARITY_EVEN, 'Stopbits' : UART.STOPBITS_ONE, 'Timeout' : 2, 'XON_XOFF' : False, 'RTS_CTS' : False, 'DSR_DTR' : False} # COM-State-Variable COM_ENABLE = False # COM-ERROR-Variable COM_ERR = False COM_TIME_ERR = False ########################################################################################################################################## # Global Variable # ########################################################################################################################################## object_set = False port = '' baudrate = '' crc = False source = '' destination = '' ########################################################################################################################################## # Functions # ########################################################################################################################################## # exit Programm def exit(): sys.exit(0) #print header def header(): # Use Global-Variable global port global baudrate global object_set global PROG_NAME global PROG_VERSION global COM_ENABLE global GPIO_ENABLE global destination global crc global source # clear terminal os.system('cls' if os.name == 'nt' else 'clear') #print Header print('#' *120) print('#' *120) print('##',' ' *114,'##') print('## Welcome to {} version: {}. This program demonstrate the functionality of the {}-class. ##'.format(PROG_NAME, PROG_VERSION[1:],Xcom_API.get_prog_name())) print('## It sends permanently a request for a known object_id and display it.',' ' *42,'##') print('##',' ' *114,'##') print('## COM-Port: {}{}destination-addr.: {}{}Version Xcom_API: {} ##'.format(port,' '*(26-len(port)),destination,' '*(24-len(destination)),Xcom_API.get_prog_version()[1:])) print('## Baudrate: {}{}source-addr.: {}{}Object of Xcom_API set: {}{} ##'.format(baudrate,' '*(26-len(baudrate)),source,' '*(29-len(source)),object_set,' '*(object_set))) print('## COM enable: {}{}CRC-Check enable: {}{}GPIO enable: {}{} ##'.format(COM_ENABLE,' '*(19+COM_ENABLE),crc,' '*(20+crc),GPIO_ENABLE,' '*(GPIO_ENABLE))) print('##',' ' *114,'##') print('#' *120) print('#' *120,'\n') # setup def setup(): # Use Global-Variable global COM_PARA global port global baudrate global destination global crc global source # get COM-Port if len(sys.argv) >= 2: port = sys.argv[1] COM_PARA['Port'] = port header() else: while True: Buffer = input('Do you want to use COM-Port: /dev/ttyAMA0? (yes/no): ') if Buffer == 'yes' or Buffer == 'y': port = COM_PARA['Port'] header() break elif Buffer == 'no' or Buffer == 'n': port = input('Enter a COM-Port: ') COM_PARA['Port'] = port header() break else: print('What do you mean?\n') # get baudrate if len(sys.argv) >= 3 and (sys.argv[2] == '115200' or sys.argv[2] == '38400'): baudrate = sys.argv[2] COM_PARA['Baudrate'] = int(baudrate) header() else: while True: Buffer = input('Do you want to use a Baudrate of: 115200? (yes/no): ') if Buffer == 'yes' or Buffer == 'y': baudrate = str(COM_PARA['Baudrate']) header() break elif Buffer == 'no' or Buffer == 'n': while True: baudrate = input('Enter a Baudrate (115200 or 38400): ') if baudrate == '115200' or baudrate == '38400': COM_PARA['Baudrate'] = int(baudrate) header() break else: print('baudrate: {}, is not supported.'.format(baudrate)) break else: print('What do you mean?\n') # get destination-address if len(sys.argv) >= 4: destination = sys.argv[3] header() else: while True: Buffer = input('Do you want to use the destination-address: 101? (yes/no): ') if Buffer == 'yes' or Buffer == 'y': destination = str(101) header() break elif Buffer == 'no' or Buffer == 'n': while True: destination = input('Enter a destination-address: ') try: if isinstance(int(destination),int): header() break except ValueError: print('destination-address: {}, is not an integer.'.format(destination)) break else: print('What do you mean?\n') # get source-address if len(sys.argv) >= 5: source = sys.argv[4] header() else: while True: Buffer = input('Do you want to use the source-address: 1? (yes/no): ') if Buffer == 'yes' or Buffer == 'y': source = str(1) header() break elif Buffer == 'no' or Buffer == 'n': while True: source = input('Enter a source-address: ') try: if isinstance(int(source),int): header() break except ValueError: print('source-address: {}, is not an integer.'.format(source)) break else: print('What do you mean?\n') # get crc check state 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'): if sys.argv[5] == 'True' or sys.argv[5] == '1': crc = True else: crc = False header() else: while True: Buffer = input('Do you want to activate CRC-Check? (yes/no): ') if Buffer == 'yes' or Buffer == 'y': crc = True header() break elif Buffer == 'no' or Buffer == 'n': crc = False header() break else: print('What do you mean?\n') # open GPIOs def open_gpios(LED1,LED2,Hostname): # Use Global-Variable global GPIO_ERR global GPIO_ENABLE # Try Function try: # Enable GPIO-Server LED_Handle = GPIO.pi(Hostname) # GPIO-Mode LED_Handle.set_mode(LED1, GPIO.OUTPUT) LED_Handle.set_mode(LED2, GPIO.OUTPUT) # GPIO-Start-State LED_Handle.write(LED1, GPIO.LOW) LED_Handle.write(LED2, GPIO.LOW) GPIO_ENABLE = True # print completed task print('GPIOs on \"{}\" are configured.'.format(Hostname)) # return LED-Handle return LED_Handle # Handle Errors except AttributeError: 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)) GPIO_ERR = True exit() # close GPIOs def close_gpios(LED_Handle,Hostname,LED1,LED2): # Use Global-Variable global GPIO_ERR global GPIO_ENABLE # Try Function try: # Reset GPIOs LED_Handle.clear_bank_1(1<