Announcement

Collapse
No announcement yet.

PySerial issue

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PySerial issue

    Hi,

    I have a premierwave xn communication with a omron PLC through serial port with hostlink protocol.
    My script is working fine with sending command to PLC and receive the response from the PLC.

    But when I disconnect the serial cable, the script will hang.
    Here is my code:

    import os
    import serial
    import datetime

    ser = serial.Serial(port = '/dev/ttyS2', bytesize = 7, stopbits = 1, parity = serial.PARITY_EVEN)

    def recv(serial):
    while True:
    buffer = ''
    buffer = serial.read(size = 15) #step A
    if buffer:
    return buffer

    def send(data):
    ser.write(data)

    command ='@01RR0000000140*\r'

    while True:
    send (command)
    response = recv(ser)

    When I disconnect the cable, the script is not dead with exception, it hangs at step A. Even I set a read timeout, it won't return me empty string. It hangs too.
    I want to know, how can I make my script work even disconnecting happened.

    Thanks and best regards,
    Chris

  • #2
    I have solved this issue. I think the problem is my in my recv function, I put thie serial.read in a loop. And after that I put it with the write function in a loop too. That's why when I disconnect, it stuck at reading.
    After not put the read in a loop, it works fine now.

    Comment

    Working...
    X