Serial communication handling by python script

Hi everyone,
i would like to send a serial command through the RS232 port using a python script.

I tried this basic script:

fd = os.open(‘/com/1’ , os.O_RDWR)

portParam = termios.tcgetattr(fd)

portParam[4] = 115200

portParam[5] = 115200

termios.tcsetattr(fd, termios.TCSADRAIN, portParam)

os.write(fd, “DAFTP0010”)

os.close(fd)

But it doesn’t work. Where is the error?
Many thanks,
Michele

I moved this to the python forum, a much better place to get an answer than the cellular hardware forum (I hope).

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.

Have a look at these Wiki pages:

Thank for your reply.
In fact, i am not familiar with termios library.

I would like just to set the serial connection speed in the python script. there is another technique?

Many Thanks,
Michele

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.

Lynnl,
Thank you for your reply,

I am using a DIGI connectWAN 3G, with a serial RS232 device connected on the serial port.

I solved my problem removing the termios settings change.

It just works…

I will work a little bit the script, then i will post the script onto the forum.

Many thanks,
Michele