0

I am trying to use serial port but I have no sucess with the simple program below. Somebody can help me? I run the script the result is:

TypeError: 'module' object is not callable

I change de serial to Serial and I put the b => ser.write(b,'A)

I dont know a answer to solve it

import time
import serial

ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate = 9600,
    parity=0,
    stopbits=1,
    bytesize=8,
    timeout=1
)

while 1:
    ser.write(b'A')
    x=ser.readline()
    print (x)
    time.sleep(1)
CDJB
  • 14,043
  • 5
  • 29
  • 55

1 Answers1

1

Firstly, make sure you've installed pyserial and not serial through pip.

Then, from the docs here, the command is actually serial.Serial, so the following should work:

ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate = 9600,
    parity=0,
    stopbits=1,
    bytesize=8,
    timeout=1
)

Also, note that ser.write() takes a bytes object, so you should change this to

ser.write(b'A')

Edit: given the stack trace in the comments, the issue was that the asker's python file was called serial.py, causing it to try to import itself rather than the serial module. Renaming their py file would solve this.

CDJB
  • 14,043
  • 5
  • 29
  • 55
  • Yes, I installed pyserial and I changed the write command, but no sucess – Wagner Ideali Nov 27 '19 at 16:26
  • Which line throws the error? Are you using `serial.Serial` with a capital `S`? – CDJB Nov 27 '19 at 16:27
  • That sounds like you have the wrong module installed. What is the output if you do `serial.__version__`? – CDJB Nov 27 '19 at 16:31
  • Traceback (most recent call last): File "/home/pi/Desktop/serial.py", line 1, in import serial File "/usr/lib/python3/dist-packages/thonny/backend.py", line 276, in _custom_import module = self._original_import(*args, **kw) File "/home/pi/Desktop/serial.py", line 3, in ser = serial.Serial('/dev/ttyUSB0') # open serial port AttributeError: module 'serial' has no attribute 'Serial' – Wagner Ideali Nov 27 '19 at 17:38