Skip to content

Commit

Permalink
fix: close #38
Browse files Browse the repository at this point in the history
  • Loading branch information
FengChendian committed Nov 13, 2024
1 parent a738319 commit 3c6b686
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 2.1.0

- Check if COM is disconnected when use `isOpened`.
## 2.0.2

- fix setCommState Error

## 2.0.1

Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,18 @@ void _send() async {
Uint8List readUint8List = await read;
print('await result: $readUint8List');
print('⬇---------------------------- time out read,try to read **18 bytes** data in queue (read <= 18 bytes)');
print(
'⬇---------------------------- time out read,try to read **18 bytes** data in queue (read <= 18 bytes)');
await port.writeBytesFromString("😄AT",
includeZeroTerminator: false,
stringConverter: StringConverter.nativeUtf8);
includeZeroTerminator: false,
stringConverter: StringConverter.nativeUtf8);
var timeOutRead = port.readBytes(18, timeout: Duration(milliseconds: 10))
..then((onValue) => print(onValue));
/// timeout
await Future.delayed(Duration(milliseconds: 15));
await port.writeBytesFromString("😄我AT",
includeZeroTerminator: false,
stringConverter: StringConverter.nativeUtf8);
includeZeroTerminator: false,
stringConverter: StringConverter.nativeUtf8);
await timeOutRead;
Expand Down
5 changes: 4 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class _MyHomePageState extends State<MyHomePage> {
print(portInfoLists);
print(ports);
if (ports.isNotEmpty) {
port = SerialPort("COM8", openNow: false);
port = SerialPort("COM5", openNow: false);
port.open();
}
setState(() {});
Expand Down Expand Up @@ -76,6 +76,9 @@ class _MyHomePageState extends State<MyHomePage> {
stringConverter: StringConverter.nativeUtf8);
var timeOutRead = port.readBytes(18, timeout: Duration(milliseconds: 10))
..then((onValue) => print(onValue));

/// timeout
await Future.delayed(Duration(milliseconds: 15));
await port.writeBytesFromString("😄我AT",
includeZeroTerminator: false,
stringConverter: StringConverter.nativeUtf8);
Expand Down
4 changes: 2 additions & 2 deletions 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.0"
version: "2.0.2"
shelf:
dependency: transitive
description:
Expand Down Expand Up @@ -448,7 +448,7 @@ packages:
source: hosted
version: "1.2.1"
win32:
dependency: transitive
dependency: "direct dev"
description:
name: win32
sha256: "10169d3934549017f0ae278ccb07f828f9d6ea21573bab0fb77b0e1ef0fce454"
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies:
dev_dependencies:
test: ^1.25.8
lints: ^5.0.0

win32: ^5.7.2
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

Expand Down
4 changes: 0 additions & 4 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter/material.dart';

import 'package:example/main.dart';

void main() {}
30 changes: 26 additions & 4 deletions lib/src/serial_port.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ enum StringConverter {
ANSI,
}

typedef ThreadProcNative = Int Function();

base class DEV_BROADCAST_PORT_ extends Struct {
@Uint32()
external int dbcp_size;

@Uint32()
external int dbcp_devicetype;

@Uint32()
external int dbcp_reserved;

@Array(1)
external Array<Uint16> _dbcc_name;
}

class SerialPort {
/// [portName] like COM3
final String portName;
Expand All @@ -18,7 +34,7 @@ class SerialPort {
final LPWSTR _portNameUtf16;

/// [dcb] is win32 [DCB] struct
final dcb = calloc<DCB>();
final dcb = calloc<DCB>()..ref.DCBlength = sizeOf<DCB>();

/// win32 [COMMTIMEOUTS] struct
final commTimeouts = calloc<COMMTIMEOUTS>();
Expand Down Expand Up @@ -153,7 +169,7 @@ class SerialPort {
}

/// [open] can be called when handler is null or handler is closed
void open() {
void open() async {
/// Do not open a port which has been opened
if (isOpened == false) {
handler = CreateFile(
Expand All @@ -175,6 +191,11 @@ class SerialPort {
}
}

if (GetCommState(handler, dcb) == FALSE) {
CloseHandle(handler);
throw Exception('GetCommState failed');
}

_setCommState();

_setCommTimeouts();
Expand Down Expand Up @@ -220,7 +241,8 @@ class SerialPort {
/// When [dcb] struct is changed, you must call [_setCommState] to update settings.
void _setCommState() {
if (SetCommState(handler, dcb) == FALSE) {
throw Exception('SetCommState error');
throw Exception(
'SetCommState error, win32 error code is ${GetLastError()}');
} else {
PurgeComm(handler,
PURGE_COMM_FLAGS.PURGE_RXCLEAR | PURGE_COMM_FLAGS.PURGE_TXCLEAR);
Expand Down Expand Up @@ -669,7 +691,7 @@ class SerialPort {
try {
hKey = _getRegistryKeyValue();
} on Exception {
return List.empty();
return portsList;
}

/// The index of the value to be retrieved.
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: serial_port_win32
description: A SerialPort library using win32 API, for connecting real device by serial port. Only supports Windows.
version: 2.1.0
version: 2.0.2
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

environment:
sdk: "^3.0.0"
sdk: "^3.1.0"

# Declare that this package only works on Windows.
platforms:
Expand Down

0 comments on commit 3c6b686

Please sign in to comment.