Can you give us a few more details about the nature of how it fails? Do you receive an exception, does it fail to transmit data? Does it transmit something, but not what you expect?
My first suggestion is that the termios.TCSADRAIN value probably doesn’t exist. These are not Linux or Windows systems, so start assuming some common PC-class things have changed.
Actually, after I said that … I see the example in the Wiki uses the TCSADRAIN, but I don’t see it in the manual. I tend to use pyserial, so am not so faimilar with termios.
I think you need to explain what doesn’t work - what error symptons show up.
First to confirm - are you using a Digi product or a PC to run this script? For a Digi, you do it the way you were trying.
Second, if a Digi product, then make sure you set the serial port profile correctly - if there is a “login” or GPS or other function running on the serial port, then Python will FAIL to open the port because it is busy/in-use. How to Enable Python access to serial port
Since some Digi product default to NOT enable Python access, I’m going to guess that is your problem here.
Also be aware that if your Python code crashes, you might need to reboot to recover the port. This is because the lower-level OS still thinks Python is using it, even though your Python object was lost during the crash. So it helps to keep the serial code and the rest of your application as seperate as possible.