-
Notifications
You must be signed in to change notification settings - Fork 30
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
Comments
After skimming libserialport.dart/port.dart, libserialport.dart/port.dart & libserialport/examples/port_config.c for a while, I found that
So, Work aroundvar config = port.config;
config.baudRate = 115200;
port.config = config; which get a copy of a If my assumptions are wrong, i am so sorry and feel free to correct. |
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.*
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 :
|
Thank you so much. You really save my day! |
Hi, I know this issue is older, but I have a similar problem. 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 ... |
@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. 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 As this repo does not provide example for reading and writing, I take this as an example. In this example, it calls to 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 |
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?
|
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. |
Also note, from the documentation:
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 |
Im using the
flutter_serial_port
package (windows 10)trying to set the port speed to 115200
after that I establish a connection
the problem is that apparently the speed is not set before opening the port, and I receive incorrect data
console:
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 expectbut 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!
The text was updated successfully, but these errors were encountered: