Serial port writing style

拈花ヽ惹草 提交于 2019-12-25 04:47:09

问题


I am using two libraries to connect with a port, and two of them uses different styles in writing these commands. I want to understand the difference because I want to use the second one, but it results in port becoming unresponsive after some time, I wonder if it causes a kind of overloading. Here are the methods.

Method 1:

 if self.port:
     self.port.flushOutput()
     self.port.flushInput()
     for c in cmd:
         self.port.write(c)
     self.port.write("\r\n")

Method 2:

if self.port:
    cmd += b"\r\n"
    self.port.flushInput() 
    self.port.write(cmd)
    self.port.flush()

The major difference I first encounter is that the first one splitting the command in to letters then send it. I wonder if this makes any difference. And as I said the second code fails after some time( it is unclear, if these methods are the problem). I don't understand what flushes do there. I want to understand the difference between these and know if the second one prone to errors.

Note: Please note that self.port is serial.Serial object.

Any advice appreciated.


回答1:


Well, from the pySerial documentation the function flushInput has been renamed to reset_input_buffer and flushOutput to reset_output_buffer:

reset_input_buffer()

Flush input buffer, discarding all it’s contents.

Changed in version 3.0: renamed from flushInput()

reset_output_buffer() Clear output buffer, aborting the current output and discarding all that is in the buffer.

Changed in version 3.0: renamed from flushOutput()

The first method is less likely to fail because the output buffer is reset before attempting a write. This implies that the buffer is always empty before writing, hence the odds the write will fail are lower.

The problem is that both the methods are error prone:

There is no guarantee that all the data you are attempting to write will be written by the write() function, either with or without the for loop. This can happen if the output buffer is already full. But the write() functions returns the number of bytes successfully written to the buffer. Hence you should loop untill the number of written bytes is equal to the number of bytes you wanted to write:

toWrite = "the command\r\n"
written = 0

while written < len(toWrite) :
   written += self.port.write(toWrite[written:])
   if written == 0 :
       # the buffer is full
       # wait untill some bytes are actually transmitted 
       time.slepp(100)

Note that "writing to the buffer" doesn't mean that the data is instantly trasmitted on the serial port, the buffer will be flushed on the serial port when the operative system thinks is time to do so, or when you force it by calling the flush() function which will wait for all the data to be written on the port.

Note also that this approach will block the thread execution untill the write is successfully completed, this can take a while if the serial port is slow or you want to write a big amount of data.

If your program is ok with that you are fine, otherwise you can dedicate a different thread to serial port communication or adopt a non-blocking approach. In the former you will have to handle multithread communication, in the latter you will have to manage internally your buffer and delete only the successfully written bytes.

Finally if your program is really simple an approach like this should do the trick:

if self.port:
   cmd+=b"\r\n"
   for c in cmd:
     self.port.write(c)
     self.port.flush()

But it will be extremely unefficient.



来源:https://stackoverflow.com/questions/40437167/serial-port-writing-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!