0

I have been trying to make serial work with Python and Pi3, however so far I have been unsuccessful.

When running the Python script, I am getting an error "serial.serialutil.SerialException: Port is already open."

I have already tried all tips in another post that should solve issues with Pi3 and the Bluetooth port. (How do I make serial work on the Raspberry Pi3)

My python script is the following:

import serial
ser = serial.Serial('/dev/serial0', 9600, timeout=1)
ser.open()

ser.write("testing")
try:
    while 1:
        response = ser.readline()
        print response
except KeyboardInterrupt:
    ser.close()

Any suggestions?

5
  • Er, yes. The error will show the line in error. Remove it.
    – joan
    Jan 2 '17 at 17:17
  • @joan, the line which throws the error is line 3: ser.open()
    – Martin
    Jan 2 '17 at 17:21
  • 1
    That's the line you need to remove.
    – joan
    Jan 2 '17 at 17:27
  • What happens if you run lsof?
    – Bex
    Jan 2 '17 at 18:42
  • 2
    @Bex serial.Serial() opens the device if the device is specified. The ser.open() call is trying to re-open an already opened device.
    – joan
    Jan 2 '17 at 18:50
1

The line ser = serial.Serial('/dev/serial0', 9600, timeout=1) opens the device automatically. When you try to open the device that is already open, python raises an error. Just take out ser.open() and it will work.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.