Home python COM port in Python: how to wait for the data without loading...

COM port in Python: how to wait for the data without loading CPU

Author

Date

Category

There is a program that accepts data from the COM port. There was a problem how to make the program wait for the data and not follow?

When using an infinite WHILE cycle, the program loads the CPU up to 50%.

forchar = 0
IDCOM = []
ser = serial.serial (
  port = 'COM2', \
  BAUDRATE = 2400, \
  parity = serial.parity_none, \
  stopbits = serial.stopbits_one, \
  Bytesize = serial.eightbits, \
  Timeout = 0)
Print ("Connected to:" + Ser.PortStr)
Count = 1.
While True:
  For Line in Ser.ReadLine ():
    Forchar = Forchar + 1
    IDCOM.APPEND (Line)
  If forchar & gt; = 13:
    Break

Answer 1, Authority 100%

You have a port configured for non-blocking reading, as you set timeout = 0 , or remove this option, or write timeout = none , then you will wait for the port , Until they appear.

If you still need a non-blocking connection, then you need to add a small time.sleep cycle of Time.Sleep , then the processor load will leave.


Answer 2

still have such a design:

while true:
    While Ser.inwaiting () == 0:
      Pass
    # Necessary work with data ...

There is a test for the presence of data in the port queue. This way of waiting, the CPU does not load at all.


Answer 3

so clearer

while ser.inwaiting ():
       Data = Ser.read ()

While the port no data reading is not performed


Answer 4

Suppose

def reccode (DATA): # and went to check by conditions - for example
  IF Data == (b '\ x05srr \ r \ n')
    Print (U'a received a message ')
While True:
  While Ser.Inwaiting () & GT; 0: # Even if we deliver without condition
  Data = Ser.ReadLine ()
  Reccode (DATA)

With this construction of the While True algorithm, it will be done constantly and RecCode (DATA) will be called when receiving each character (byte) to the port, and if 1 bytes in the packet It is not known (work on the start and end symbol) or 2 you need to still serve the graphical interface and in additions 3 send data (messages) to the port, then some characters (received) may be lost, and the program (regardless of how many cores from the processor) will Is employed only by this, and if you need 2 ports to serve?

In the assembler (AVR controllers), in VB6 (x86 and 64), it is possible to use hardware interrupts, the event (reception of the port symbol) may (with proper configuration of the controller or machine hardware node), stop execution (or redirect the stream commands) of the main body of the program, perform a subprogram (interrupt) of the reception of one character with the ability to form a string on starting and finite symbols (\ x05 and \ r \ n ) and after receiving the final Symbol directly in the subroutine (if there is little conditions) process – changing variables or, set the readiness flag of the received message for the main program, or changing the global variable for other processes.

then While True can engage (let’s say) checking the readiness flags of the message of one, second, n-port, updating the processes of the graphic shell type Root = TK () .. (graphic elements), and in Place root.vfinloop () Use root.update (), TK as graphic control interfaces Quite slow processes

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions