0
votes

I have the need for an applet that allows run time defined baud rate, stop bit, parity etc to be used with my Raspberry Pi 3 application.

I have coded the applet using fixed parameters, and got the applet working just as I need, I then coded to gather the runtime values as required successfully but cannot find how to invoke the serial port using variables rather than fixed values.

This is the bit of my code for opening the serial port that I need help with

ser = serial.Serial(
  port='/dev/ttyUSB0',
  baudrate=38400,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS
  )

What I am looking for in pseudo code is

mybaud = "38400"
myparity = "serial.PARITY_NONE"
mystop = "serial.STOPBITS_ONE"
mybyte = "serial.EIGHTBITS" 

ser = serial.Serial(
  port='/dev/ttyUSB0',
  baudrate=mybaud,
  parity=myparity,
  stopbits=mystop,
  bytesize=mybyte
  )

Any advice would be appreciated.

1
Why would you put quotes around values that aren't strings? - Ignacio Vazquez-Abrams
serial.tools.list_ports.ListPortInfo object in pyserial has the information of variables of the serial device. - Nithin Varghese
I understand the serial device variables, but want to pass values to configure the serial port with variables from my program. - Rob_Haigh

1 Answers

0
votes

ok, so my previous programming experience and newby python skills got in the way.

If I simply assign the value to the variable and use the variable name it works as per the following example.

mybaud = 38400
myparity = serial.PARITY_NONE
mystop = serial.STOPBITS_ONE
mybyte = serial.EIGHTBITS"

ser = serial.Serial(
  port='/dev/ttyUSB0',
  baudrate=mybaud,
  parity=myparity,
  stopbits=mystop,
  bytesize=mybyte
  )