Skip to content

Commit

Permalink
fix: lack of open settings
Browse files Browse the repository at this point in the history
  • Loading branch information
FengChendian committed Nov 14, 2024
1 parent 77a0f0a commit 9836731
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.3

- fix some bugs

## 2.1.2

- update changelogs
Expand Down
3 changes: 2 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class _MyHomePageState extends State<MyHomePage> {
print(portInfoLists);
print(ports);
if (ports.isNotEmpty) {
port = SerialPort("COM5", openNow: false);
port = SerialPort("COM8", openNow: false);
// port
port.open();
}
setState(() {});
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.1.1"
version: "2.1.2"
shelf:
dependency: transitive
description:
Expand Down
56 changes: 40 additions & 16 deletions lib/src/serial_port.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ class SerialPort {
/// status
final _status = calloc<COMSTAT>();

// 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");

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -254,13 +274,15 @@ class SerialPort {
// ignore: non_constant_identifier_names
set BaudRate(int rate) {
dcb.ref.BaudRate = rate;
this._BaudRate = rate;
_setCommState();
}

/// data byteSize
// ignore: non_constant_identifier_names
set ByteSize(int size) {
dcb.ref.ByteSize = size;
this._ByteSize = size;
_setCommState();
}

Expand All @@ -269,13 +291,15 @@ class SerialPort {
// ignore: non_constant_identifier_names
set StopBits(int stopBits) {
dcb.ref.StopBits = stopBits;
this._StopBits = stopBits;
_setCommState();
}

/// You can use [DCB_PARITY.NOPARITY], [DCB_PARITY.ODDPARITY] and so on like win32
// ignore: non_constant_identifier_names
set Parity(int parity) {
dcb.ref.Parity = parity;
this._Parity = parity;
_setCommState();
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 9836731

Please sign in to comment.