Results 1 to 4 of 4

Thread: Problem to communicate with serial port

  1. #1
    Join Date
    Feb 2021
    Beans
    1

    Problem to communicate with serial port

    I am using ST32 connected to the computer with USB cable. this micro-controller is already programmed and should give me two values
    (it has worked on windows 10 I just replaced '/dev/ttyS1' with 'COM1').
    the problem is that when i compile my program it doesn't show any Errors. I don't even know if I am communicating with the right port or not.

    DO you have an Idea what can i do to solve this problem?



    import
    serial
    import re
    serport = '/dev/ttyS1'
    serbaudrate = 9600
    ser = serial.Serial(port = serport, baudrate = serbaudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=2)
    try:
    ser.isOpen()
    except:
    print("Error")
    exit()

    if ser.isOpen():
    try:
    while 1:
    if ser.inWaiting() > 0:
    response = ser.readline()
    needles = re.match("(\d+);(\d+)", response.decode("utf-8"))
    if needles:
    print("Got: {} {}".format(needles[1], needles[2]))
    #print("Got: {}".format(needles[2]))
    else:
    ser.write(b"1")
    except Exception as e:
    print(e)

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,577
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Problem to communicate with serial port

    Welcome to the forums.

    I haven't done serial comms in Python, but I suspect you don't have read/write permission to the device. Typically, you would need to be in the group dialout. You could test for that:
    Code:
    ls -l /dev/ttyS*
    id
    cat /dev/null > /dev/ttyS1
    cat /dev/ttyS1 > /dev/null
    If you get "permission denied", then that will need dealing with.

    You don't have the initializer inside a try/except scope.
    Code:
    try:
      ser.isOpen
    looks a bit weird. This call isn't likely to raise an exception.

    I read that isOpen is deprecated in favour of is_open.

  3. #3
    Join Date
    Jan 2017
    Beans
    210

    Re: Problem to communicate with serial port

    Are you sure the device is on ttyS1?
    Code:
    $ sudo dmesg | grep ttyS
    [    0.970737] 00:03: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A

  4. #4
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,577
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Problem to communicate with serial port

    [Posted in error.]

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •