2

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
asked Nov 25, 2014 at 15:19
1
  • Can you post your code here? It would be much easier to help you! Commented Nov 25, 2014 at 15:34

3 Answers 3

1

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.

answered Nov 25, 2014 at 15:40
Sign up to request clarification or add additional context in comments.

2 Comments

can you explain how the inWait and outWait works? I dont understand how each of these methods work
@VemaReddy It checks how many bytes are in input and output buffers. Input buffer contains information that was transmitted from the device and output has information that was written by your code and hasn't been read by the device yet.
0

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')
answered Nov 25, 2014 at 15:45

6 Comments

sorry for got to add beginning stuff, I would like baudrate at 28800 because trying to send faster. IF I add 0.5 delay after my writes it works, but I am trying to avoid doing that
Either select lower speed or specify delay (so the partner device may process it). You may wrap 'read' operation with adding delay and invoke the wrapper once you want to write
why do I need a delay, because with a virtual port I don't have delays in my code and it works? Can I create a bigger input or output buffer. is their any flags that I can check for when the buffer is busy processing because I don't like putting 0.5s delay because it might be longer depending on the length of instruction?
What is your partner device?
I create a usb device with uart-bridge and 8051 microcontroller. I feel confident that this is not a hardware issue but serial buffer issue
|
0

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

answered Apr 24, 2018 at 21:38

3 Comments

is there a way we can do this for Linux platform ?
@Solen'ya It is not about whether you can or cannot do it on Linux platform. It is about whether your Linux driver supports this or not.
what I meant was the command ser.set_buffer_size(rx_size = 12800, tx_size = 12800) is not available on the Linux platform . Is there any alternative using which we can do it. If it is driver dependent, How can I do the same thing on Raspberry Pi ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.