#!/usr/bin/python3 """ MIT License Permission is hereby granted, free of charge, to any person obtaining a copy in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. This example shows how to use the RS232 serial port of the BL i.MX8MM. """ import serial, time # Configure the serial driver ser = serial.Serial() # Communicate via RS232 ser.port = "/dev/ttymxc0" ser.baudrate = 115200 ser.bytesize = serial.EIGHTBITS ser.parity = serial.PARITY_NONE ser.stopbits = serial.STOPBITS_ONE # Try to open the port try: ser.open() except Exception as e: print ("Error while opening the serial port: " + str(e)) exit() # If the port is open, write Hello World and wait # for a reply which must be terminated by an LF. if ser.isOpen(): try: # Clear the buffers ser.flushInput() ser.flushOutput() while True: # Write to the serial port ser.write("Hello World!\n".encode()) # Write it immediately ser.flush() print("Write data!") try: # Now wait for the other side to send something myvar = ser.read_until() # Wait for LF except: # If there was an error during receiving, print an error myvar = b'Error receiving!' # Display the received data, which must be ASCII characters in this example print("Read data! " + myvar.decode('ascii')) time.sleep(0.05) ser.close() except Exception as e1: print ("Error while communicating...:" + str(e1)) else: print ("Cannot open serial port!")