6

I am connecting to device using code blow on MacOS and out of 100 times this code would make connection only 1 or two times and dones't respond(since there is no timeout) rest of the times.

ser = serial.Serial(port="/dev/xyz",timeout = None, baudrate=115200, parity = serial.PARITY_NONE, bytesize = serial.EIGHTBITS, stopbits = serial.STOPBITS_ONE)

def exitSer(ser):
    print("Closing")
    ser.close()

atexit.register(exitSer, ser)

if ser.is_open:
    time.sleep(2)
    while(1):
        print(ser.readline().decode("utf-8"))

Could you please tell me how to use programs like fcntl etc to find if this port is completely free and available for use and how to set tty port's flags to free after making port free forcibly.

Once this works, I have to run this multithreaded where each thread is running different devices expecting output in lines. Any suggestions for that just in case this works.

bruceparker
  • 1,235
  • 1
  • 17
  • 33
  • The other application you refer to has a different baudrate and uses 1 stop bit. Could you post the code that you use to read data from the serial port? – J. P. Petersen Sep 28 '16 at 08:03
  • Just updated the question. Thank you for response. – bruceparker Sep 28 '16 at 23:25
  • 1
    I try to avoid using readline() on serial devices, as it first writes something when you receive a new line character. So if anything else is received, you will not see it. Instead you could use something like: sys.stdout.write(ser.read(1)) sys.stdout.flush() – J. P. Petersen Sep 29 '16 at 13:43
  • Do you know if the other tool you have tried sends something to the ios device? – J. P. Petersen Sep 29 '16 at 13:44
  • yes it is capable to write, read etc. Its exactly like how serial output and input would work. I also know that when i have my program running that tool wont work. – bruceparker Sep 30 '16 at 18:30
  • Does it make a difference if you use `\r\n` for the line endings of the data you write to the device? – PM 2Ring Oct 03 '16 at 07:32
  • No change as far as output is concerned. Thanks for looking at it. – bruceparker Oct 03 '16 at 17:56
  • Well, the baudrate is different for starters... The other tools mentions `115200` ... – Martin Tournoij Oct 03 '16 at 18:06
  • @Carpetsmoker if you read the question carefully, you will see that i have tried that too. – bruceparker Oct 03 '16 at 18:12
  • Well, what you have now is obviously wrong, so it has no business being there in the first place. – Martin Tournoij Oct 03 '16 at 18:25
  • @Carpetsmoker Again if you see it, i have mentioned above, "This code works and then after a while stops working". It rarely works but does work. It stops working only after i stop and restart the connection. This question aims at why does it work in first place with different baudrate and how can i stabilise it. 115200 works even less reliable. – bruceparker Oct 03 '16 at 23:22
  • @Carpetsmoker I have updated the question with more knowledge i have gained in past week. Any suggestions are very much appreciated. Thank you for your time. – bruceparker Oct 06 '16 at 18:28

1 Answers1

0
def startSerial(tty_id):
    ser = serial.Serial(port = tty_id, timeout = None)
    ser.close()
    ser.open()
    if ser.isOpen():
        print(ser.portstr, ":connection successful.")
        return ser
    else:
        return False

Calling ser.close() before .open() fixed it. I tested it about 200 times and i haven't been dissappointed so far. I am testing it now in multithreaded and hopefully that works too.

Thank you everybody.

bruceparker
  • 1,235
  • 1
  • 17
  • 33