I need to send through the Raspberry Pi device /dev/ttyAMA0 , for example, the character с . For this, as in the pySerial documentation, I wrote a mini code:

 import serial def send_tty(): ser = serial.Serial('/dev/ttyAMA0', 115800) print(ser.name) ser.write(b'c') return 

When performing ser.write(b'c') I get 1 and no effect on the screen. Are there any other modules in Python that allow you to send commands to TTY/UART normally or how to do it with this?

  • And you were not mistaken with speed, 115800 strange speed, usually they use 115200 . - Avernial
  • @Avernial even speed did not help achieve the right effect. - Insider

1 answer 1

Basically, everything is fine with you. This should work.

The only thing that maybe the port settings are not suitable (speed, parity, etc.)

Here is a more detailed example, where you can see how to set the port parameters, how to work with it, how to read data in response, and so on:

 #!/usr/bin/python import serial, time #initialization and open the port #possible timeout values: # 1. None: wait forever, block call # 2. 0: non-blocking mode, return immediately # 3. x, x is bigger than 0, float allowed, timeout block call ser = serial.Serial() ser.port = "/dev/ttyUSB7" ser.baudrate = 9600 ser.bytesize = serial.EIGHTBITS #number of bits per bytes ser.parity = serial.PARITY_NONE #set parity check: no parity ser.stopbits = serial.STOPBITS_ONE #number of stop bits #ser.timeout = None #block read ser.timeout = 1 #non-block read #ser.timeout = 2 #timeout block read ser.xonxoff = False #disable software flow control ser.rtscts = False #disable hardware (RTS/CTS) flow control ser.dsrdtr = False #disable hardware (DSR/DTR) flow control ser.writeTimeout = 2 #timeout for write try: ser.open() except Exception, e: print "error open serial port: " + str(e) exit() if ser.isOpen(): try: ser.flushInput() #flush input buffer, discarding all its contents ser.flushOutput()#flush output buffer, aborting current output #and discard all that is in buffer #write data ser.write("AT+CSQ") print("write data: AT+CSQ") time.sleep(0.5) #give the serial port sometime to receive the data numOfLines = 0 while True: response = ser.readline() print("read data: " + response) numOfLines = numOfLines + 1 if (numOfLines >= 5): break ser.close() except Exception, e1: print "error communicating...: " + str(e1) else: print "cannot open serial port " 

There are other examples of using pyserial here :

  • Unfortunately, this and the other did not help. You see, when I connect via ttyAMA0 with the help of minicom , after removing the systemctl stop serial-getty@ttyAMA0.service authorization, systemctl stop serial-getty@ttyAMA0.service and send it to c , my screen resolution changes. When using this code, this does not happen, I just get 1 as an answer, or for example 5 , if it is (b'hello') - it depends on the number of characters. - Insider
  • @Insider: getty on this terminal, of course, should not be. Take it away completely. See what port settings in minicom (speed, parity, etc.). If it works through minicom, it will work through python, it won't go anywhere - Igor Chubin
  • This is what the minicom settings look like - http://prntscr.com/c7nxtq , but in python http://prntscr.com/c7ny2k . Pre-deactivate getty - Insider
  • @Insider: You are aware that the 1 that write returns is not a device response! This is just the result of the function execution. The answer you need to read is ser.read() or ser.readline() - Igor Chubin
  • I know the answer I would see on the screen, the screen resolution would change. - Insider