serial_port_win32

Creator: coderz1093

Last updated:

Add to Cart

Description:

serial port win32

serial_port_win32 #
A SerialPort library using win32 API.

Getting Started #
Get Ports #
final ports = SerialPort.getAvailablePorts();
print(ports);
/// result like [COM3, COM4]
copied to clipboard
Get Ports with more messages (Experiment) #
final List<PortInfo> ports = SerialPort.getPortsWithFullMessages();
print(ports);
/// print [Port Name: COM3, FriendlyName: 蓝牙链接上的标准串行 (COM3), hardwareID: BTHENUM\{00001101-0000-1000-8000-00803f9b55fb}_LOCALMFG&0000, manufactureName: Microsoft]

PortInfo({
required this.portName,
required this.friendlyName,
required this.hardwareID,
required this.manufactureName,
});
copied to clipboard
Create Serial Port #
The port instance is Singleton Pattern. Don't re-create port for same Com name.
final port = SerialPort("COM5", openNow: false, ByteSize: 8, ReadIntervalTimeout: 1, ReadTotalTimeoutConstant: 2);
// port.open()
port.openWithSettings(BaudRate: CBR_115200);
// final port = SerialPort("COM5"); /// auto open with default settings
copied to clipboard
Set parameters #
port.BaudRate = CBR_115200;
port.ByteSize = 8;
port.StopBits = ONESTOPBIT;
port.Parity = NOPARITY;
port.ReadIntervalTimeout = 10;
/// and so on, parameters like win32.
copied to clipboard
Read #
port.readBytesSize = 8;
port.readOnListenFunction = (value) {
print(value);
};
// or
// can only choose one function
print(await port.readBytesUntil(Uint8List.fromList("\n".codeUnits)));
copied to clipboard
Write #
Write String
String buffer = "hello";
port.writeBytesFromString(buffer);
copied to clipboard
Write Uint8List
final uint8_data = Uint8List.fromList([1, 2, 3, 4, 5, 6]);
print(port.writeBytesFromUint8List(uint8_data));
copied to clipboard
Get Port Connection Status #
port.isOpened == false;
copied to clipboard
Flow Control #
port.setFlowControlSignal(SerialPort.SETDTR);
port.setFlowControlSignal(SerialPort.CLRDTR);
copied to clipboard
Close Serial Port #
Close Without Listen
port.close();
copied to clipboard
Close On Listen
port.closeOnListen(
onListen: () => print(port.isOpened),
)
..onError((err) {
print(err);
})
..onDone(() {
print("is closed");
print(port.isOpened);
});
copied to clipboard
Attention #
If you want to read or write strings using serial, be careful to handle the terminator at the end.
Although in most cases, like "Hello\0" (68 65 6C 6C 6F 00) and "Hello"(68 65 6C 6C 6F) both can be identified by computer.
Small Example #
import 'package:serial_port_win32/src/serial_port.dart';

void main() {
var ports = SerialPort.getAvailablePorts();
print(ports);
if(ports.isNotEmpty){
var port = SerialPort(ports[0]);
port.BaudRate = CBR_115200;
port.StopBits = ONESTOPBIT;
port.close();
}
}
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.