4

I'm trying to write a script for a LCD device called the uLCD32-pt and the issue is that you are required to connect to it with a baudrate of 9600 and in order to get a higher baud rate you have to connect to it, send a change baudrate command, then send the new commands at the newly set baudrate. My lcd display is going super slow when drawing pixels and I know its because of the baudrate so is their any way to change the baudrate after connecting to a device? Here is my code so far?

import serial
import time

#Connect to uLCD32-pt with autobaud
ser = serial.Serial(
    port='/dev/ttyUSB0', 
    baudrate=9600, 
    timeout=1,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)
ser.write("U")


while(True):
    #15 x white pixels
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0001)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#1
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0002)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#2
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0003)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#3
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0004)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#4
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0005)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#5
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0006)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#6
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0007)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#7
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0008)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#8
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0009)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#9
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x000A)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#10
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x000B)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#11
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x000C)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#12
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x000D)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#13
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x000E)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#14
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x000F)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#15
    ser.write(chr(0x0050)+chr(0x0000)+chr(0x0010)+chr(0x0000)+chr(0x000A)+chr(0x00FF)+chr(0x00FF))#16
Artem Zankovich
  • 2,319
  • 20
  • 36
Xenland
  • 510
  • 1
  • 6
  • 19

2 Answers2

10

In the documentation, the description of the baud rate property says:

Read or write current baud rate setting.

So I expect this line to work:

ser.baudrate = 115200
David Grayson
  • 84,103
  • 24
  • 152
  • 189
6

You can call setBaudrate that does a few checks and reconfigures the port if it was already open.

ser.setBaudrate(115200)
hithwen
  • 2,154
  • 28
  • 46
  • 2
    I get an error: AttributeError: 'Serial' object has no attribute 'setBaudrate' using python 3.6.4. Perhaps this code is only Python 2 compatible? – Nuclear_Man_D Jun 14 '18 at 04:34
  • 2
    this method is not longer valid, as it was moved to a property seen in the other [answer](https://stackoverflow.com/a/9271871/9530917). – Jakar510 Dec 10 '19 at 21:47
  • I get the same error as Nuclear_Man_D in Python 2.7.16 – Lorraine Aug 11 '21 at 15:41