Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not correctly setup baudRate #24

Open
maxnemoy opened this issue Feb 18, 2021 · 8 comments
Open

Not correctly setup baudRate #24

maxnemoy opened this issue Feb 18, 2021 · 8 comments

Comments

@maxnemoy
Copy link

maxnemoy commented Feb 18, 2021

Im using the flutter_serial_port package (windows 10)

trying to set the port speed to 115200

var port = SerialPort(element);
port.config.baudRate = 115200;

after that I establish a connection

port .openReadWrite();
_reader = SerialPortReader(port);
_reader.stream.listen((event) {
      print(event);
});

the problem is that apparently the speed is not set before opening the port, and I receive incorrect data

console:

flutter: [254]
flutter: [254]
flutter: [255]
flutter: [254]
flutter: [254]
flutter: [254]

empirically, I found out, if you push the controller for the port speed of 4800, then the reading becomes correct.

port.config.baudRate = 115200 it does not do what I expect

but the strange behavior is that I have another computer, to work on it the controller must be flashed for a speed of 115200, even if I do not use it port.config.baudRate = 115200
(operating system, flutter version, library version is the same for both pc)

Where am I going wrong? Thx!

@nkpro2000sr
Copy link

After skimming libserialport.dart/port.dart, libserialport.dart/port.dart & libserialport/examples/port_config.c for a while,

I found that

  1. SerialPort.config getter calls sp_get_config, which provides a copy of a sp_port_config object.
  2. SerialPort.config setter calls sp_set_config, which set any sp_port_config object to port.
  3. SerialPortConfig.baudRate setter calls sp_set_config_baudrate, which set any integer to sp_port_config.
  4. sp_set_baudrate which set baudrate directly to port, is not implemented here. (Which you required)

So,
port.config.baudRate = 115200 actually sets baud rate to copy of a sp_port_config object (which got from port.config getter).

Work around

var config = port.config;
config.baudRate = 115200;
port.config = config;

which get a copy of a sp_port_config object, set baud rate and again set the copy to the port.

If my assumptions are wrong, i am so sorry and feel free to correct.

nkpro2000sr added a commit to nkpro2000sr/serial-flutter-demo that referenced this issue Apr 3, 2021
jpnurmi/libserialport.dart#24 (comment)

After skimming [libserialport.dart/port.dart](https://github.com/jpnurmi/libserialport.dart/blob/ccd8a195ad2552b021e4401545ffa15fe686a2c5/lib/src/port.dart), [libserialport.dart/port.dart](https://github.com/jpnurmi/libserialport.dart/blob/ccd8a195ad2552b021e4401545ffa15fe686a2c5/lib/src/config.dart) & [libserialport/examples/port_config.c](https://github.com/sigrokproject/libserialport/blob/1b011060df5579c15bd4e103c2d8afc4d442fd72/examples/port_config.c) for a while,

I found that
1. `SerialPort.config` getter calls `sp_get_config`, which provides a copy of a `sp_port_config` object.
2. `SerialPort.config` setter calls `sp_set_config`, which set any `sp_port_config` object to port.
3. `SerialPortConfig.baudRate` setter calls `sp_set_config_baudrate`, which set any integer to `sp_port_config`.
4. `sp_set_baudrate` which set baudrate directly to port, is not implemented here. (Which you required)

So,
`port.config.baudRate = 115200` actually sets baud rate to copy of a `sp_port_config` object (which got from port.config getter).

#### Work around
```dart
var config = port.config;
config.baudRate = 115200;
port.config = config;
```
which get a copy of a `sp_port_config` object, set baud rate and again set the copy to the port.

*If my assumptions are wrong, i am so sorry and feel free to correct.*
@AlexanderMAKS
Copy link

This work around does not work all the time for me for some reason. If I use the workaround and try to send out a 0x0d (which is a return) then it will "send out" an 0x8d. My setup was a basic flutter app sending data to an FTDI USB-> Serial cable and monitoring the lines on a scope. But, for some reason the below code did work for me :

  SerialPort port;
  print("hello");
  List<String> devices = SerialPort.availablePorts;
  print('Devices : $devices');
  port = SerialPort(devices.first);
  port.openWrite();
  
  var config = port.config;
  config.baudRate = 9600;
  port.config = config;
  
  port.config.bits = 8;
  port.config.stopBits = 1;
  port.config.parity = 0;
  
  port.config = config;  //Had to add this 
  
  port.write(Uint8List.fromList([0x0D]));
}```

@maxwang967
Copy link

This work around does not work all the time for me for some reason. If I use the workaround and try to send out a 0x0d (which is a return) then it will "send out" an 0x8d. My setup was a basic flutter app sending data to an FTDI USB-> Serial cable and monitoring the lines on a scope. But, for some reason the below code did work for me :

  SerialPort port;
  print("hello");
  List<String> devices = SerialPort.availablePorts;
  print('Devices : $devices');
  port = SerialPort(devices.first);
  port.openWrite();
  
  var config = port.config;
  config.baudRate = 9600;
  port.config = config;
  
  port.config.bits = 8;
  port.config.stopBits = 1;
  port.config.parity = 0;
  
  port.config = config;  //Had to add this 
  
  port.write(Uint8List.fromList([0x0D]));
}```

Thank you so much. You really save my day!

@phhoef
Copy link

phhoef commented Jan 26, 2023

Hi, I know this issue is older, but I have a similar problem.
I am trying to open a serial port with baud rate 1800 in flutter.
This is a magic baud rate I've introduce on the microcontroller to trigger a reset. When using CoolTerm the reset is working, but with libserialport it is not.

This is the code

    final port = SerialPort(com);
    try {
      port.openWrite();
      var config = port.config;
      config.baudRate = 1800;
      port.config = config;

      port.config.bits = 8;
      port.config.stopBits = 1;
      port.config.parity = 0;

      port.config = config; //Had to add this
      port.write(Uint8List.fromList([0]));
    } catch (error) {
      print(error);
      // ignoring error
    } finally {
      port.dispose();
    }

I followed your examples, but it seems, that the baud rate is never used ...
That said, opening a port with default settings does work as I can send some commands ...

@mingpepe
Copy link

@nkpro2000sr, I encountered similar issue and trace the code found that a small mistake in your statement.

SerialPort.config getter calls sp_get_config, which provides a copy of a sp_port_config object.
=>It create a config object in the first time and later always return the same reference to that object,so not return a copyed

Consider the following code

var port = SerialPort(element);
if (port.openWrite()) {
    port.config.baudRate = 115200;
    port.config.stopBit = 1;
    // Other config
    port.config = port.config; // workaround
}

All these configuration call sp_set_config_xxx,e.g. sp_set_config_baudratethey do not write to OS.
The workaround port.config = port.conig works since it call to sp_set_config.

As this repo does not provide example for reading and writing, I take this as an example. In this example, it calls to sp_set_xxx instead of sp_set_config_xxx, which is different from this wrapper. You can find definition of sp_set_xxx and sp_set_config_xxx here.

Take #47 as an example, his configuration is not written to OS and his workaround is using other library to open then close the port. It works since other libaray set configuration for him.

@jpnurmi, I suggest calling to sp_set_xxx in config setting, like here. It may be more clear to use this package. If you think it's ok, I can make a PR.

@mihnen
Copy link

mihnen commented Apr 19, 2024

I'm confused, does this library actually work on windows? Doesn't seem to be taking the correct baudrate, have tried everything from all the comments above and nothing is working. Has this really been an open issue for 2+ years?

_port = SerialPort(_selectedPort);

var config = _port.config;
config.baudRate = 230400;
config.bits = 8;
config.stopBits = 1;
config.cts = SerialPortCts.ignore;
config.rts = SerialPortRts.off;
config.dsr = SerialPortDsr.ignore;
config.dtr = SerialPortDtr.off;
config.parity = SerialPortParity.none;
config.xonXoff = SerialPortXonXoff.disabled;

_port.config = config;

@mihnen
Copy link

mihnen commented Apr 19, 2024

Okay solved my problem, in case this helps someone else you have to set the config AFTER you have opened the port. This is not mentioned anywhere in the documentation that I could find and seems counter intuitive but it works for me on Windows 11 that way.

@micheljung
Copy link

Also note, from the documentation:

For each setting in a port configuration, a special value of -1 can be used, which will cause that setting to be left alone when the configuration is applied by SerialPort.config setter.

When a new configuration is created, all of its settings are initially set to the special -1 value.

You should always configure all settings before using a port. There are no default settings applied by the library. When you open a port, it may have default settings from the OS or driver, or the settings left over by the last program to use it.

Configurations must be disposed using dispose().

Example:

    var port = SerialPort(address);
    check(port.openReadWrite(), "Could not open for read/write: $port");
    var config = SerialPortConfig();
    config.baudRate = 115200;
    config.bits = 8;
    config.parity = SerialPortParity.none;
    config.stopBits = 1;
    config.xonXoff = 0;
    config.rts = 1;
    config.cts = 0;
    config.dsr = 0;
    config.dtr = 1;
    port.config = config;
    config.dispose();

And it worked! When rts and dtr weren't set correctly, I had the behavior that no bytes were received.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants