does serialport not return port open errors?

110 Views Asked by At

I intend to test all communication speeds, and stop at the highest. But I can't get information from the serialport to STOP/finish the cycle.

clc; clear USB
BaudRate=[115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 600, 300]
i=0;
do
 `i++
  disp("--------")
    Baud=BaudRate(i)
    switch Baud
      case (115200)
        USB=serialport("/dev/ttyUSB0",115200)
      case (57600)
        USB=serialport("/dev/ttyUSB0",57600)
      case (38400)
        USB=serialport("/dev/ttyUSB0",38400)
      case (19200)
        USB=serialport("/dev/ttyUSB0",19200)
      case (9600)
        USB=serialport("/dev/ttyUSB0",9600)
      case (4800)
        USB=serialport("/dev/ttyUSB0",4800)
      case (2400)
        USB=serialport("/dev/ttyUSB0",2400)
      case (1200)
        USB=serialport("/dev/ttyUSB0",1200)
      case (600)
        USB=serialport("/dev/ttyUSB0",600)
      case (300)
        USB=serialport("/dev/ttyUSB0",300)
    endswitch
until (STOP)

set(USB,"DataBits",7)
set(USB,"parity","E")
set(USB,"stopbits",1)
set(USB,"timeout",300)
USB

I tried to cause an error in opening the port and based on that error, finish.

I tried to open the door again, expecting an error that the door was already open;

I tried with set(USB,"DataBits",7) and waited for it to give me a nonexistent port error;

And handle it through lasterror.message or lasterrror.identifier and thus get a condition to stop the cycle do/until

1

There are 1 best solutions below

1
maio290 On

I guess everything here works as intended: the serialport() call returns an octave_serialport instance (see docs).

As Tim Roberts already stated out, you are actually supposed to know the baudrate of the device you're talking to, it doesn't look like your goal would be an algorithm to determine the maximum supported baud rate of your device, therefore I ditch that part completely. For the loop, you'd use for here.

Just assign the proper port (see serialportlist()) and baudrate, then check for the property Status on your octave_serialport instance - this should give you an indication whether your connection is good or not.

Also, you used the set command wrong, the supported keys are (probably case-sensitive): baudrate, bytesize, name, parity, stopbits, timeout, requesttosend, dataterminalready.

  • DataBits has to be bytesize.
  • timeout cannot exceed 255.