serial port getting engaged after reading single command in python

混江龙づ霸主 提交于 2020-01-16 08:41:11

问题


I am making a connection with python using pyserial with a UART port. As I send the command using serial.write my output is received but my serial port does not break connection. As I need to send a second command also to receive the output. Please guide me on this.

I have also used ser.close still I am not able to close the port.

import serial

ser = serial.Serial(
        port='COM5', \
        baudrate=9600, \
        parity=serial.PARITY_NONE, \
        stopbits=serial.STOPBITS_ONE, \
        bytesize=serial.EIGHTBITS, \
        timeout= 10)
print("connected to: " + ser.portstr)

ser.write(b'\reeprom\r')
seq = []
count = 1

while True:
        for c in ser.readline():
            seq.append(chr(c))  # convert from ANSII
            joined_seq = ''.join(str(v) for v in seq)  # Make a string from array

            if chr(c) == '\n':
                print("Line " + str(count) + ': ' + joined_seq)
                seq = []
                count += 1
                break

ser.close()

Can you please guide what is going wrong. I am expecting that I should get my output of the fired command to UART and then code should exit rather than continue running. Also I wanted to know if this works can I again make connection and fire the second command to UART. Please help me on this.

来源:https://stackoverflow.com/questions/59564715/serial-port-getting-engaged-after-reading-single-command-in-python

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