Closed larbi-asmaoui closed 2 months ago
What is the delimiter or indication of new line? I would recommend capturing the values and using regex patterns to do this.
What is the delimiter or indication of new line? I would recommend capturing the values and using regex patterns to do this.
I think the error goes here: Stream upcomingData = reader.stream.map( (data) => String.fromCharCodes(data).replaceAll('\r\n', ''), ); upcomingData.listen((data) { _res += data.replaceAll('\r\n', ''); print(_res); });
Hi,
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter_libserialport/flutter_libserialport.dart';
class SerialPorts extends ChangeNotifier { List _ports = SerialPort.availablePorts;
String _res = "";
SerialPort? printerPort;
SerialPort? scalePort;
List get availablePorts => _ports;
double get getData => double.parse(_res); set setData(String data) { _res = data; notifyListeners(); }
void setPortName(String name) {}
void listAllPorts() { _ports = SerialPort.availablePorts; notifyListeners(); }
Future connect(String portName) async {
try {
printerPort = SerialPort(portName);
printerPort!.open(mode: 1);
printerPort!.config = SerialPortConfig()
..baudRate = 9600
..bits = 8
..stopBits = 1
..parity = SerialPortParity.none
..rts = SerialPortRts.flowControl
..cts = SerialPortCts.flowControl
..dsr = SerialPortDsr.flowControl
..dtr = SerialPortDtr.flowControl
..setFlowControl(SerialPortFlowControl.rtsCts);
if (printerPort!.isOpen) {
read();
notifyListeners();
}
} catch (e) {
print(e.toString());
printerPort!.close();
notifyListeners();
}
}
Future read() async {
try {
SerialPortReader reader = SerialPortReader(printerPort!, timeout: 3000);
Stream upcomingData = reader.stream.map(
(data) => String.fromCharCodes(data).replaceAll('\r\n', ''),
);
upcomingData.listen((data) {
_res += data.replaceAll('\r\n', '');
print(res);
});
notifyListeners();
} on SerialPortError catch (err, ) {
if (printerPort!.isOpen) {
printerPort!.close();
print(SerialPort.lastError);
notifyListeners();
}
}
}
void disconnect() { printerPort!.dispose(); scalePort!.dispose(); printerPort!.close(); scalePort!.close(); print("disconnected !!"); notifyListeners(); } }
I run this code and I got data like this : 28.0028.0028.0028.0028.0028.00
I want to print each value per each line.please help.