def get_frequencies(self): """Get the frequencies of all memory locations""" for n in range(0, 15): f = self.get_frequency(n) debug(f"{n}={f}") print(f"{n}={f}")
def __init__(self): """Intialize class IfrInterface""" self.comport = 'COM1:' self.baudrate = 9600 self.serialport = None debug('Intializing serial port') try: self.serialport = serial.Serial(self.comport, baudrate=self.baudrate, timeout=0.2) debug('serial port is opened') except Exception as e: debug(f"EXCEPTION: {e}") self.serialport = None return self.send_lf() ret = self.ask_uok() if ret == b'%': debug(f"IFR1200Io is initialized") else: print('Error in initializing class IFR1200Io')
def send_command(self, cmd) -> bytes: """Send the given command to the IFR 1200 and wait for the response :param cmd: The string with the command to send :returns: The response (if any) """ if not self.serialport: raise serial.SerialException bytes_waiting = self.serialport.inWaiting() if bytes_waiting: debug( f"UNEXPECTED: Still {bytes_waiting} bytes waiting in serial port before sending command {cmd}" ) s = self.serialport.read(bytes_waiting) debug(f"RESPONSE1 = {s}") if isinstance(cmd, bytes): pass elif isinstance(cmd, str): cmd = cmd.encode('utf-8') debug(f"Command: {cmd}") self.serialport.write(cmd) self.serialport.flush() time.sleep(0.2) # Only get response in the following cases: if b"?" in cmd or b'DUMP' in cmd or b'MTR1' in cmd or b'MTR2' in cmd or b'DTME' in cmd: ret = self.get_response(cmd) return ret bytes_waiting = self.serialport.inWaiting() if bytes_waiting: debug( f"UNEXPECTED: Still {bytes_waiting} bytes waiting in serial port after sending cmd {cmd}" ) s = self.serialport.read(bytes_waiting) debug(f"RESPONSE2 = {s}") return s
def get_response(self, cmd='') -> bytes: """ Wait for a response from the IFR. :param cmd: The command to send :returns: The stripped response """ if not self.serialport: raise serial.SerialException # Determine the number of waiting, incoming bytes # debug(f"waiting for response for command [{cmd}]") time.sleep(0.2) # Wait somewhat more time. bytes_waiting = self.serialport.inWaiting() # debug(f'bytes waiting = {bytes_waiting}') # Read the bytes, and append them to the return string s = self.serialport.read(bytes_waiting) debug(f"Response: {s}") if s.startswith(b"**"): print(f"ERROR: INVALID COMMAND [{cmd.decode().strip()}]") return s.strip()
:return: True if valid, False if not """ if n < 0 or n > 15: print(f'Error, invalid memory location {n}') return False return True # ------------------------------------------------------------------- if __name__ == "__main__": """The main of this module will perform some tests""" # src.lib.helper.clear_debug_window() debug(f"Testing {__file__}") # import doctest # doctest.testmod() mem = IFR1200Memory() mem.set(0, 96.80) disp.set("3FM") mem.set(1, 90.40) # Arrow Jazz FM mem.set(2, 99.90) # BNR Nieuws Radio mem.set(4, 1.800) # Groot Nieuws Radio mem.set(5, 100.70) # Q Music mem.set(6, 98.90) # Radio 1 mem.set(7, 92.60) # Radio 2 mem.set(8, 102.30) # Radio 538