Vema Reddy
Vema Reddy

Reputation: 45

Python Pyserial Serial Buffer

I am trying to write and read as quickly as Python will let me through the serial port. The problem is that I am writing to fast and it is not working. I believe the problem has to do serial buffer and that I might be over writing my input buffer. Can I increase the input and output buffer? Is their any method or function or flag that tells when the buffer is full, empty, or busy? I am trying send these two command over and over and over as quickly as possible

ser = serial.Serial(port=2,baudrate=28800, timeout = 1)
#print ser

ser.flushInput()  #flush input buffer, discarding all its contents
ser.flushOutput() #flush output buffer, aborting current output
                  #and discard all that is in buffer    


ON = ":00000008f8"
PGMMEM0 = ":01f0000011FE" #program one memory location

start = timeit.default_timer()

for i in range(10):

   ser.write(ON)

   end = timeit.default_timer() - start
   print end

   ser.write(PGMMEM0)

   end = timeit.default_timer() - start
   print end

Upvotes: 2

Views: 32113

Answers (3)

Valentyn
Valentyn

Reputation: 719

If you run your code on Windows platform, you simply need to add a line in your code

ser.set_buffer_size(rx_size = 12800, tx_size = 12800)

Where 12800 is an arbitraery number I chose. You can make receiving(rx) and transmitting(tx) buffer as big as 2147483647

See also:

https://docs.python.org/3/library/ctypes.html

https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readbuffersize(v=vs.110).aspx

You might be able to setup the serial port from the DLL // Setup serial

 mySerialPort.BaudRate = 9600;
 mySerialPort.PortName = comPort;
 mySerialPort.Parity = Parity.None;
 mySerialPort.StopBits = StopBits.One;
 mySerialPort.DataBits = 8;
 mySerialPort.Handshake = Handshake.None;
 mySerialPort.RtsEnable = true;
 mySerialPort.ReadBufferSize = 32768;

Property Value Type: System.Int32 The buffer size, in bytes. The default value is 4096; the maximum value is that of a positive int, or 2147483647

And then open and use it in Python

Upvotes: 0

Konstantin
Konstantin

Reputation: 3159

The problem may be with the baud rate. You can set up baud rate in the constructor, make sure that both serial link partners are configured:

ser = serial.Serial(baudrate=9600)

If the delay after write operation works, you may create a wrapper class for serial port interaction, for example:

class RS232(object):

  def __init__ (self):
      self.ser = serial.Serial(port=2,baudrate=28800, timeout = 1)

  def write(self,s):
      self.ser.write(s)
      time.sleep(0.5)

myRS232 = RS232()
myRS232.write('whatever')

Upvotes: 0

unwind
unwind

Reputation: 399753

Yes, you can use the Serial.outWaiting() method to check how much data is still waiting to be sent.

I'm not sure exactly how the Serial.write() method behaves, if it just adds the data to the internal buffer before returning. It would have to be that way in order for the above to be needed, but on the other hand there's nonblocking() to make it non-blocking. It's a bit confusing.

Upvotes: 1

Related Questions