Home > OS >  Python serial in a class readline not working
Python serial in a class readline not working

Time:08-03

I want to read values by serial from an application with serial interface.

Folling code gives the expected result:

# Example 1

import serial
from time import sleep

ser = serial.Serial(port='COM' '1', baudrate='9600', bytesize=8, stopbits=1, parity='N', timeout=1)
 
ser.write(b"@INIT0174\r") #init communication
    
for i in range (3):
    ser.flushInput()
    ser.write(b"@FBKVR01BB\r") # Read values
    sleep(0.01)
    print(ser.readline().decode()[5:-6])

'''
Result:  
-0.258785367012024\
-0.262508511543274\
-0.266262173652649
'''

Following code is not working as desired.

self.ser().readline().decode()  #creates not expected output
import serial
from time import sleep

class serTest():
    def __init__(self, port):
        self.port = port
        
    def ser(self):
        """
        Initalize serial object
        """
        s = serial.Serial(port='COM'  self.port, baudrate='9600', 
            bytesize=8, stopbits=1, parity='N', timeout = 1)
        return (s)
    
    def init(self):
        """
        open serial connection and write something to it
        """
        
        print('init')
        self.ser().write(b"@INIT0174\r")
        
        print('flush')
        self.ser().flushInput()
        
        sleep(0.5)
        #read a line
        output = (self.ser().readline().decode())
        print(output)
    
    def term(self):
        """
        close serial connection
        """
        self.ser().write(b"@TERM0178\r")

    def get_value(self, cycles=3):
        """
        Get value from application
        """
        
        for i in range (cycles):
            # ask for the value
            self.ser().write(b"@FBKVR01BB\r\n")
            sleep(0.5)
            print(self.ser().readline()[5:-6].decode())

test = serTest('1')
test.init()
test.get_value()
test.term()

'''
Result: 
init
flush
four empty lines
'''

Monitored outgoing answers from application in both cases with same content.

Howto achieve same result from function and class? Classes are pretty new for me. I appreciate any comment to that.

CodePudding user response:

When you call self.ser() inside this init(self) function, it creates multiple class instance objects each time, hence you create serial.Serial() each time.

Hope this could solve your issue.

def init(self):
    """
    open serial connection and write something to it
    """
    
    print('init')
    serial_obj = self.ser()
    serial_obj.write(b"@INIT0174\r")
    
    print('flush')
    serial_obj.flushInput()
    
    sleep(0.5)
    #read a line
    output = (serial_obj.readline().decode())
    print(output)

CodePudding user response:

I want to use the ser.object in other methods of this class. Is this a good way?

import serial
from time import sleep

class instance():
    def __init__(self, port):
        self.port = port
        self.ser_obj  = serial.Serial(port='COM'  self.port, baudrate='9600', 
            bytesize=8, stopbits=1, parity='N', timeout = 1)
        
    def init(self):
        """
        open serial connection
        """
        self.ser_obj.flushInput()
        self.ser_obj.write(b"@INIT0174\r")
        # other stuff
    
    def term(self):
        """
        close serial connection
        """
        self.ser_obj.write(b"@TERM0178\r")

    def get_value(self, cycles=3):
        """
        get value from application
        """
        for i in range (cycles):
            # ask for the value
            self.ser_obj.write(b"@FBKVR01BB\r\n")
            sleep(0.5)
            print(self.ser_obj.readline()[5:-6].decode())
  • Related