Reputation: 10114
I want to fake a device via a script and have the dart program to interface with it. Therefore, in my linux machine, I've run the following command to emulate a serial port:
socat PTY,link=./arduino-sim,raw,echo=0 PTY,link=./virtual-tty,raw,echo=0
And I've made a session via screen that will emulate the virtual device:
screen ./arduino-sim
Also, the dart programm itself is:
import 'package:libserialport/libserialport.dart';
import 'dart:typed_data';
void main(List<String> arguments) {
final port = SerialPort("./virtual-tty");
if (!port.openReadWrite()) {
print(SerialPort.lastError);
}
port.write(Uint8List.fromList("Lorem Ipsum".codeUnits));
final reader = SerialPortReader(port);
reader.stream.listen((data) {
print('received: $data');
});
}
and both arduino-sim
and virtual-tty
have the common path with my dart project:
-rw-rw-r-- 1 pcmagas pcmagas 1038 Αυγ 17 13:36 analysis_options.yaml
lrwxrwxrwx 1 pcmagas pcmagas 10 Αυγ 17 14:59 arduino-sim -> /dev/pts/1
drwxrwxr-x 2 pcmagas pcmagas 4096 Αυγ 17 13:36 bin
-rw-rw-r-- 1 pcmagas pcmagas 29 Αυγ 17 13:36 CHANGELOG.md
drwxrwxr-x 2 pcmagas pcmagas 4096 Αυγ 17 13:36 lib
-rw-rw-r-- 1 pcmagas pcmagas 8063 Αυγ 17 14:13 pubspec.lock
-rw-rw-r-- 1 pcmagas pcmagas 276 Αυγ 17 13:37 pubspec.yaml
-rw-rw-r-- 1 pcmagas pcmagas 122 Αυγ 17 13:36 README.md
drwxrwxr-x 2 pcmagas pcmagas 4096 Αυγ 17 13:36 test
lrwxrwxrwx 1 pcmagas pcmagas 10 Αυγ 17 14:59 virtual-tty -> /dev/pts/3
But once I've run my project via dart run
I get the following error:
art run
Building package executable...
Built serial:serial.
SerialPortError: Success, errno = 0
Unhandled exception:
SerialPortError: Success, errno = 0
But if I plug an actual serial device (in my case an arduino Uno) and replace the line
final port = SerialPort("./virtual-tty");
with:
final port = SerialPort("/dev/ttyACM0");
Works fine:
Building package executable...
Built serial:serial.
received: [82, 73, 78, 71, 13, 10, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10, 82, 73, 78, 71, 13, 10]
received: [82, 73, 78, 71, 13]
So why libserialport is unable tyo use virtual serial port;
Upvotes: 1
Views: 225