Flutter: How to implement serial communication debugging in Android

        This article describes how to implement serial port communication debugging in Flutter through the flutter_libserialport plug-in.

1. Introduce dependencies

        Introduce the flutter_libserialport dependency in the pubspec.yaml file of the flutter project:

dependencies:
  flutter_libserialport: ^0.3.0

2. Import import dependency package

        Import and import flutter_libserialport.dart in the dart code and it can be used.

import 'package:flutter_libserialport/flutter_libserialport.dart';

3. Read all serial devices

        All current serial ports can be obtained through the SerialPort.availablePorts method. The following code is used to print all serial port names.

// 串口测试
void serialTest() {
  print('Available ports:');
  var i = 0;
  List<String> list = SerialPort.availablePorts;
  print('PortSize: ${list.length}');
  for (final name in SerialPort.availablePorts) {
    print('${++i}) $name');
  }
}

 

 

You can also read and print out the detailed information of the serial port of the device through the following code.

// 串口测试
void serialTest() {
  print('Available ports:');
  var i = 0;
  List<String> list = SerialPort.availablePorts;
  print('PortSize: ${list.length}');
  for (final name in SerialPort.availablePorts) {
    final sp = SerialPort(name);
    print('${++i}) $name');
    print('\tDescription: ${sp.description}');
    print('\tManufacturer: ${sp.manufacturer}');
    print('\tSerial Number: ${sp.serialNumber}');
    print('\tProduct ID: 0x${sp.productId!.toRadixString(16)}');
    print('\tVendor ID: 0x${sp.vendorId!.toRadixString(16)}');
    sp.dispose();
  }
}

 4. Monitor and receive serial port data

        Through SerialPortReader, an input stream can be created to receive data sent to the serial port.

// 串口测试
void serialTest() {
  String name = '/dev/ttyS4';
  final port = SerialPort(name); 
  // port.config.baudRate = 115200;
  if (!port.openReadWrite()) {
    print(SerialPort.lastError);
    return;
  }

  // 读数据
  final reader = SerialPortReader(port);
  reader.stream.listen((data) {
    print('received: $data');
    print('receivedString: ${utf8.decode(data)}'); // 转换为字符串
    String hexString = data.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join();
    print('receivedHex: ${hexString.toUpperCase()}'); // 转换为16进制
  });
}

1. The data received by default is Uint8List type, that is, binary format.

2. We can convert the received Uint8List data into a string through the utf8.decode(data) method.

3. You can also use the data.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join() method to convert the received Uint8List data into hexadecimal.

 5. Send data through the serial port

        By calling the write method, you can directly send data to the serial port. It should be noted that the data type is Uint8List.

// 串口测试
void serialTest() {
  // 写数据
  var bytes = Uint8List.fromList([0xAB, 0xCD, 0xEF]);  
  port.write(bytes);
}

6. Code example

     The complete code example of serial port communication debugging in flutter is as follows:

// 串口测试
void serialTest() {
  print('Available ports:');
  var i = 0;
  List<String> list = SerialPort.availablePorts;
  print('PortSize: ${list.length}');
  for (final name in SerialPort.availablePorts) {
    // final sp = SerialPort(name);
    print('${++i}) $name');
    // print('\tDescription: ${sp.description}');
    // print('\tManufacturer: ${sp.manufacturer}');
    // print('\tSerial Number: ${sp.serialNumber}');
    // print('\tProduct ID: 0x${sp.productId!.toRadixString(16)}');
    // print('\tVendor ID: 0x${sp.vendorId!.toRadixString(16)}');
    // sp.dispose();
  }

  String name = '/dev/ttyS4';
  final port = SerialPort(name); 
  // port.config.baudRate = 115200;
  if (!port.openReadWrite()) {
    print(SerialPort.lastError);
    return;
  }

  // 读数据
  final reader = SerialPortReader(port);
  reader.stream.listen((data) {
    print('received: $data');
    print('receivedString: ${utf8.decode(data)}'); // 转换为字符串
    String hexString = data.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join();
    print('receivedHex: ${hexString.toUpperCase()}'); // 转换为16进制
  });

  // 写数据
  var bytes = Uint8List.fromList([0xAB, 0xCD, 0xEF]);  
  port.write(bytes);
}

New Era Migrant Workers

Guess you like

Origin blog.csdn.net/sg_knight/article/details/130781257