help with serial port I/O

Ken Koller ken.koller at kla-tencor.com
Thu Sep 9 19:12:51 EDT 1999


This may not be much help, but is there a "flush" command? I haven't used
this module, but usually the write command writes to the Unix kernels buffer
and the kernel decides when to flush it to the hardware device. I believe
"flush" will force the kernel to pass the bytes to the device.

I'm guessing that the reason you have to run twice is the buffer is flushed
when the file handle is closed. So you may be getting the response from the
first write.

There are also some "blocking" modes that can be set up. At least from the C
I/O library on the SGI platform (it's been a few years). But I remember
mucking with them to get the reads to sync correctly. In that case there
were several buffers to keep track of. The small, probably single byte
hardware buffer of the serial port, the kernel buffer, and the C library
buffer.

udl.flush() ???

Chip Piller wrote:

> Hi,
> I am running RedHat Linux 6.0.
> I have an instrumentation amplifier that can be controlled by the serial
> port.
> I can successfully setup the serial line and read and write to the
> device using the minicom serial communications program.
>
> I want to write a simple program that will write a command to the
> device, wait for one second, and then read the response from the device.
>
> I tried the following code:
> #!/usr/bin/python
> udl = open('/dev/ttyS1','r+')
> udl.write('sen 22\r')              #change instrument sensitivity
> setting
> udl.write('mag\r')                  #query instrument for signal
> magnitude
> val = udl.readline()
> print 'magnitude readback = '
> print val
> udl.close()
>
> Here is the output:
> [root at piller1 python]# ./test3
> magnitude readback =
> sen 22
>
> [root at piller1 python]#
>
> The code does write the sen 22 command to the instrument and changes the
> value, but not on the first try, I usually have to run the program
> twice.
> I have not had success with getting any readback from the device using
> Python.
> Help or suggestions please!
> Chip





More information about the Python-list mailing list