0

I did debug the code below, line-by-line using some print statements.

class Timeout(Exception):
    pass

def getSource(comm):
    source = comm.split('@')
    params = source[1].split(':')
    debug = '--debug' in sys.argv
    if source[0] == 'serial':
        try:
            return Serial(params[0], int(params[1]), flush=True, debug=debug)
        except:
            print ("ERROR: Unable to initialize a serial connection to", comm)
            raise Exception

Everything looks OK until the line:

return Serial(params[0], int(params[1]), flush=True, debug=debug)

this line is supposed to be compiled since all the objects in the Serial like params[0], etc are obtained. But it returns an error jumping to the except and printing the statement "ERROR: Unable to initialize a serial connection to ..."

I am using Python 3.6.8 on a Docker container.

Any kind of help would be appreciated. I'm ready for any further info, if needed.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
BlueCurve
  • 33
  • 1
  • 7
  • What's the argument you are passing to `getSource()`? Also, can you open the corresponding serial port within that docker container using some other tool like Putty? It could very well be a problem with the docker container itself. Take a look here https://stackoverflow.com/a/24231872/12661819 – Dominik Berse Jun 30 '21 at 20:30
  • 1
    What is `Serial`? And exactly what are the values of the parameters you are passing? – quamrana Jun 30 '21 at 20:42
  • @quamrana `Serial` is a class that is defined in the same code. All the parameters are obtained by parsing the `serial@/dev/ttyUSB0:115200`. The `/dev/ttyUSB0` is the port associated with a telosb mote, and `115200` is the baudrate. – BlueCurve Jun 30 '21 at 20:54
  • @DominikBerse the argument is `comm` which is equivalent to `serial@/dev/ttyUSB0:115200`, here. As I am passing this command in the docker terminal: `python filename.py serial@/dev/ttyUSB0:115200` – BlueCurve Jun 30 '21 at 20:58
  • A bare `except:` is never a good idea. Make that `except Exception as e:`, and include `e` in your output - it will probably tell you exactly what went wrong. – jasonharper Jun 30 '21 at 21:01
  • What do you mean by "include `e` in your output"? @jasonharper – BlueCurve Jun 30 '21 at 21:11
  • I mean, put it in the `print()` so that you can actually see the error message. Currently, your code is throwing away every bit of data that might help solve the problem. – jasonharper Jun 30 '21 at 21:20
  • It’s just that I didn’t know that ‘debug’ and ‘flush’ were actual parameters. – quamrana Jun 30 '21 at 21:29
  • If you mean this: `print (e)`, I got this error: `unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(, file=)"?` and `invalid literal for int() with base 10: 'telosb'` @jasonharper – BlueCurve Jun 30 '21 at 22:48
  • Did you have a look at the link I posted? You need to forward the device manually using `--device=/dev/ttyUSB0` as `docker` parameter. Also, if you want to do `print(e)` in your `except` Statement, it has to be `except Exception as e:` instead of `except:`. – Dominik Berse Jun 30 '21 at 23:17
  • I am already using the latter command which is `docker run -t -i --privileged -v /dev/bus/usb:/dev/bus/usb bash`. @DominikBerse – BlueCurve Jun 30 '21 at 23:30
  • If the device is indeed available over this path, please work on providing the actual exception. Replace `except:` with `except Exception as e:` and do a `print(e)` within that scope. – Dominik Berse Jun 30 '21 at 23:55
  • I did, and got the same error I mentioned above in reply to @jasonharper – BlueCurve Jun 30 '21 at 23:59
  • The device is available and detectable by the system; checked the output of `motelist`. I was already running this same code and device on Python 2.7.4 and by these same commands without any error, but now with Python 3.6 I got the error I mentioned. @DominikBerse – BlueCurve Jul 01 '21 at 00:06
  • Using the `python:3` docker image with the command line you provided above works for me. However I am using `pyserial` directly, as I don't know what `Serial` does. Also the exception part works for me. I can not provide any more help without details on the exception. – Dominik Berse Jul 01 '21 at 00:12

1 Answers1

0

Here, I post the Serial. I hope this helps you guys to better understand my issue.

class Serial:

def __init__(self, port, baudrate, flush=False, debug=False, readTimeout=None, ackTimeout=0.02):
    self.debug = debug
    self.readTimeout = readTimeout
    self.ackTimeout = ackTimeout
    self._ts = None

    if port.startswith('COM') or port.startswith('com'):
        port = int(port[3:]) - 1
    elif port.isdigit():
        port = int(port) - 1

    self._s = serial.Serial(port, int(baudrate), rtscts=0, timeout=0.5)
    self._s.flushInput()
    if flush:
        print >>sys.stdout, "Flushing the serial port",
        endtime = time.time() + 1
        while time.time() < endtime:
            self._s.read()
            sys.stdout.write(".")
        if not self.debug:
            sys.stdout.write("\n")
    self._s.close()
    self._s = serial.Serial(port, baudrate, rtscts=0, timeout=readTimeout)

def getByte(self):
    c = self._s.read()
    if c == '':
        raise Timeout
    #print 'Serial:getByte: 0x%02x' % ord(c)
    return ord(c)

def putBytes(self, data):
    #print "DEBUG: putBytes:", data
    for b in data:
        self._s.write(struct.pack('B', b))
        time.sleep(0.000001)

def getTimeout(self):
    return self._s.timeout

def setTimeout(self, timeout):
    self._s.timeout = timeout
BlueCurve
  • 33
  • 1
  • 7