From 98367311cff71c19ef5a13b510b4808c375ebd2a Mon Sep 17 00:00:00 2001 From: FengChendian Date: Thu, 14 Nov 2024 11:04:26 +0800 Subject: [PATCH] fix: lack of open settings --- CHANGELOG.md | 4 +++ example/lib/main.dart | 3 ++- example/pubspec.lock | 2 +- lib/src/serial_port.dart | 56 ++++++++++++++++++++++++++++------------ pubspec.yaml | 2 +- 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2e1039..ffc7c6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.1.3 + +- fix some bugs + ## 2.1.2 - update changelogs diff --git a/example/lib/main.dart b/example/lib/main.dart index b269372..5f0ff0e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -45,7 +45,8 @@ class _MyHomePageState extends State { print(portInfoLists); print(ports); if (ports.isNotEmpty) { - port = SerialPort("COM5", openNow: false); + port = SerialPort("COM8", openNow: false); + // port port.open(); } setState(() {}); diff --git a/example/pubspec.lock b/example/pubspec.lock index a691f02..e52b968 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -265,7 +265,7 @@ packages: path: ".." relative: true source: path - version: "2.1.1" + version: "2.1.2" shelf: dependency: transitive description: diff --git a/lib/src/serial_port.dart b/lib/src/serial_port.dart index 79aea71..f69ba61 100644 --- a/lib/src/serial_port.dart +++ b/lib/src/serial_port.dart @@ -40,6 +40,18 @@ class SerialPort { /// status final _status = calloc(); + // ignore: non_constant_identifier_names + int _BaudRate = CBR_115200; + + // ignore: non_constant_identifier_names + int _Parity = DCB_PARITY.NOPARITY; + + // ignore: non_constant_identifier_names + int _StopBits = DCB_STOP_BITS.ONESTOPBIT; + + // ignore: non_constant_identifier_names + int _ByteSize = 8; + /// [_keyPath] is registry path which will be opened static final _keyPath = TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM"); @@ -138,15 +150,16 @@ class SerialPort { required int ReadTotalTimeoutMultiplier, required bool openNow, }) { - dcb - ..ref.BaudRate = BaudRate - ..ref.Parity = Parity - ..ref.StopBits = StopBits - ..ref.ByteSize = ByteSize; + this._BaudRate = BaudRate; + this._Parity = Parity; + this._StopBits = StopBits; + this._ByteSize = ByteSize; + commTimeouts - ..ref.ReadIntervalTimeout = 10 - ..ref.ReadTotalTimeoutMultiplier = 10 - ..ref.ReadTotalTimeoutConstant = 1; + ..ref.ReadIntervalTimeout = ReadIntervalTimeout + ..ref.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant + ..ref.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier; + if (openNow) { open(); } @@ -180,6 +193,12 @@ class SerialPort { throw Exception('GetCommState failed'); } + dcb + ..ref.BaudRate = this._BaudRate + ..ref.Parity = this._Parity + ..ref.StopBits = this._StopBits + ..ref.ByteSize = this._ByteSize; + _setCommState(); _setCommTimeouts(); @@ -210,15 +229,16 @@ class SerialPort { // ignore: non_constant_identifier_names int ReadTotalTimeoutMultiplier = 0, }) { - dcb - ..ref.BaudRate = BaudRate - ..ref.Parity = Parity - ..ref.StopBits = StopBits - ..ref.ByteSize = ByteSize; + this._BaudRate = BaudRate; + this._Parity = Parity; + this._StopBits = StopBits; + this._ByteSize = ByteSize; + commTimeouts - ..ref.ReadIntervalTimeout = 10 - ..ref.ReadTotalTimeoutConstant = 1 - ..ref.ReadTotalTimeoutMultiplier = 0; + ..ref.ReadIntervalTimeout = ReadIntervalTimeout + ..ref.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant + ..ref.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier; + open(); } @@ -254,6 +274,7 @@ class SerialPort { // ignore: non_constant_identifier_names set BaudRate(int rate) { dcb.ref.BaudRate = rate; + this._BaudRate = rate; _setCommState(); } @@ -261,6 +282,7 @@ class SerialPort { // ignore: non_constant_identifier_names set ByteSize(int size) { dcb.ref.ByteSize = size; + this._ByteSize = size; _setCommState(); } @@ -269,6 +291,7 @@ class SerialPort { // ignore: non_constant_identifier_names set StopBits(int stopBits) { dcb.ref.StopBits = stopBits; + this._StopBits = stopBits; _setCommState(); } @@ -276,6 +299,7 @@ class SerialPort { // ignore: non_constant_identifier_names set Parity(int parity) { dcb.ref.Parity = parity; + this._Parity = parity; _setCommState(); } diff --git a/pubspec.yaml b/pubspec.yaml index e8a3ea6..21371a0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: serial_port_win32 description: A SerialPort library using win32 API, for connecting real device by serial port. Only supports Windows. -version: 2.1.2 +version: 2.1.3 homepage: https://github.com/FengChendian/serial_port_win32 repository: https://github.com/FengChendian/serial_port_win32 issue_tracker: https://github.com/FengChendian/serial_port_win32/issues