0

I am so sorry for asking similar questions. But I managed to get some stuff yet I cannot make it work properly.

import serial

ser = serial.Serial(port='COM3', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=1)
try:
    ser.isOpen()
    print("Serial port is open")
except:
    print("ERROR")
    exit()

if (ser.isOpen()):
    try:
        while (True):
            ser.write("S CR LF")
            print(ser.read())
    except:
        print("error")
else:
    print("Cannot open serial port")

I learned that I should send some commands in order to receive information. https://i.stack.imgur.com/mPhcQ.jpg https://i.stack.imgur.com/fvJPo.jpg https://i.stack.imgur.com/a8lE8.jpg These are the Commands that I should use and then parse them in order to get the result only. This is what I get when I run this code. (I know, I should change the "ser.Write(this part)" but I don't know how?)

Serial port is open
error

Process finished with exit code 0
  • You most likely want an actual carriage return and line feed: `ser.write("S\r\n")` – 001 Oct 17 '19 at 11:58
  • Also, [`isOpen`](https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.isOpen) is deprecated. Use [`is_open`](https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.is_open). And that does not throw an exception so the first `try` block seems pointless. – 001 Oct 17 '19 at 12:06
  • I hope you guys checked the 3 links for commands/ I will try your suggestions – Hilmi Araz Oct 17 '19 at 12:38
  • Well using `is_open` caused it to directly jump to except section and Printed ERROR and `ser.write("S\r\n")` doesn't do anything – Hilmi Araz Oct 17 '19 at 12:41
  • It sounds like you are not successfully opening the port. Change your code to catch `serial.SerialException` and print it out to see the message. – 001 Oct 17 '19 at 13:32
  • `ser1 = serial.SerialException()` i created something like this under `ser = serial.Serial` and printed right after that like `print(ser1)` and does nothing return empty – Hilmi Araz Oct 18 '19 at 08:32

1 Answers1

0

You can use the exceptions to know what is causing the error:

import serial
import sys

try:
    ser = serial.Serial(port='COM3', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=1)
    while (True):
        ser.write(str.encode("S\r\n"))
        print(ser.read())
except serial.SerialException as e:
    print("serial error:", e)
except:
    print("other error:", sys.exc_info()[0])

So if, for example, you are using the wrong port, it will print:

error: could not open port 'COM3': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)

Edit: so the issue was that write wants a byte array and not a string. Though the docs say a string is ok (version 2.5).

The important thing is to not hide the exceptions. See: About catching ANY exception

001
  • 13,291
  • 5
  • 35
  • 66
  • `Traceback (most recent call last): File "C:/Users/Administrator/Desktop/Proje/Serial2.py", line 6, in ser.write("S\r\n") File "C:\Users\Administrator\Desktop\Proje\venv\lib\site-packages\serial\serialwin32.py", line 308, in write data = to_bytes(data) File "C:\Users\Administrator\Desktop\Proje\venv\lib\site-packages\serial\serialutil.py", line 63, in to_bytes raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) TypeError: unicode strings are not supported, please encode to bytes: 'S\r\n' ` – Hilmi Araz Oct 18 '19 at 13:54
  • Ok. There's the error. Change the code to `ser.write(str.encode("S\r\n"))`. – 001 Oct 18 '19 at 13:56
  • @HilmiAraz BTW, that's actually a [`TypeError`](https://docs.python.org/3/library/exceptions.html#TypeError) exception. However, you did not see it previously since you were catching _all_ exceptions and just printing the (not very helpful) message `"error"`. See: [About catching ANY exception](//stackoverflow.com/a/4990739) – 001 Oct 18 '19 at 14:10
  • @HilmiAraz You have the timeout set to 1 second, so even if the device does not respond, `ser.read()` will return. Set the timeout to `None` so that `ser.read()` blocks until it actually has a response. – 001 Oct 23 '19 at 12:57