libserialport.dart icon indicating copy to clipboard operation
libserialport.dart copied to clipboard

Unable to open `/dev/pts` devices (used by socat and jpnevulator)

Open pc-magas opened this issue 1 year ago • 5 comments

I made a minimalistic example using your library:

import 'package:libserialport/libserialport.dart';
import 'dart:typed_data';

void main(List<String> arguments) {
  final port = SerialPort("/dev/pts/3");
  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 I used a serial port monitor to intercept any incomming and outcomming messages from and to a serial port:

$ jpnevulator --ascii --tty /dev/ttyACM0 --pty --pass --read --ascii --timing-print
jpnevulator: Unable to open interface /dev/ttyACM0: Device or resource busy
jpnevulator: slave pts device is /dev/pts/3.

But once I run dart run I get the error:

Building package executable... 
Built serial:serial.
Unhandled exception:
SerialPortError: No such file or directory, errno = 2
#0      Util.call (package:libserialport/src/util.dart:39:7)
#1      _SerialPortImpl._init (package:libserialport/src/port.dart:221:10)
#2      new _SerialPortImpl (package:libserialport/src/port.dart:211:42)
#3      new SerialPort (package:libserialport/src/port.dart:72:38)
#4      main (file:///mnt/data/Kwdikas/dart/serial/bin/serial.dart:5:16)
#5      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:32)
#6      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

Meaning that dart is unable to read and write towards /dev/pts/3 device. Thoudh I am able to access the /dev/ttyACM0 directly if I replace the /dev/pts/3 with /dev/ttyACM0:

import 'package:libserialport/libserialport.dart';
import 'dart:typed_data';

void main(List<String> arguments) {
  final port = SerialPort("/dev/pts/3");
  if (!port.openReadWrite()) {
    print(SerialPort.lastError);
  }

  port.write(Uint8List.fromList("Lorem Ipsum".codeUnits));

  final reader = SerialPortReader(port);
  reader.stream.listen((data) {
    print('received: $data');
  });
}

Works fine as you can see:

received: []
received: []
received: [78, 71, 58, 80]
received: []
received: [10]
received: []
received: []
received: [58, 76, 111, 114]
received: []
received: [13, 10]
received: []
received: []
received: [79, 78, 71, 101]
received: []
received: [115, 117, 109, 13]
received: [10]
received: []
received: [58, 80, 79, 78]
received: []
received: []
received: [71, 58, 80, 79]
received: []
received: [58, 80, 79, 78]
received: []
received: [79, 78, 71, 58]
received: [80, 79, 78, 71]
received: []
received: []
received: []

The problem is that I am unable to read any data that dart code sends towards the device, but I can only read whatever device sends towards the application. That it hinders me on debugging my dart code. Is there a way for the library to open /dev/pts devices as well?

pc-magas avatar Aug 20 '22 16:08 pc-magas

Hello,

I think I have the same problem (or atleast similar).

I am using a SBC(Single board computer) to manage 4 devices (card reader, LED controller, touch screen etc...). However the SBC doesn't have enough ports, so I am using a board to combine all of the serial ports into one usb, then that usb is connected to the SBC and then an app 'splits' that usb connection into 4 virtual serial ports.

lrwxrwxrwx  1 root root          10 Sep  7 13:22 CardReader -> /dev/pts/4
lrwxrwxrwx  1 root root          10 Sep  7 13:22 Debug -> /dev/pts/3
lrwxrwxrwx  1 root root          10 Sep  7 13:22 LED -> /dev/pts/2
lrwxrwxrwx  1 root root          10 Sep  7 13:22 TouchScreen -> /dev/pts/1

If I open some of the ports with minicom, everything is working fine minicom -D /dev/CardReader -b 9600

But if I try to open it with libserialport I receive the following error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: SerialPortError: No such file or directory, errno = 2
#0      Util.call (package:libserialport/src/util.dart:39)
#1      _SerialPortImpl._init (package:libserialport/src/port.dart:221)
#2      new _SerialPortImpl (package:libserialport/src/port.dart:211)
#3      new SerialPort (package:libserialport/src/port.dart:72)
#4      NfcReaderManager.connect (package:kiosk/managers/nfc_reader_manager.dart:53)
#5      _AppState.initState (package:kiosk/main.dart:89)
#6      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4942)
#7      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4781)
#8      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3817)
#9      Element.updateChild (package:flutter/src/widgets/framework.dart:3551)
#10     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1202)
#11     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1171)
#12     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1119)
#13     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2597)
#14     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1118)
#15     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:953)
#16     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:933)
#17     _rootRun (dart:async/zone.dart:1418)
#18     _CustomZone.run (dart:async/zone.dart:1328)
#19     _CustomZone.runGuarded (dart:async/zone.dart:1236)
#20     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276)
#21     _rootRun (dart:async/zone.dart:1426)
#22     _CustomZone.run (dart:async/zone.dart:1328)
#23     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260)
#24     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18)
#25     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398)
#26     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429)
#27     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192)

bojidartonchev avatar Sep 07 '22 13:09 bojidartonchev

The question may be can serialport that is based upon support virtual serial connections though?

pc-magas avatar Feb 01 '23 09:02 pc-magas

It's indeed a problem with the Sigrok serialport library and not the dart wrapper, @jpnurmi created a PR a few years ago that is still open: https://github.com/sigrokproject/libserialport/pull/4 You will need to patch the library yourself. If you use flutter I made a fork with the patch: https://github.com/AurelienBallier/flutter_libserialport

AurelienBallier avatar Mar 24 '23 14:03 AurelienBallier

@AurelienBallier How I can use a patched libserial port in my case?

pc-magas avatar May 04 '23 09:05 pc-magas

You can:

git clone https://github.com/jpnurmi/libserialport
cd libserialport
git checkout f8e42f46d063f5002f959e502269b9c6698fd423

And then build and install the lib on your system. Ensure to remove any other libserialport installed and to add the proper paths for your system to find the library.

AurelienBallier avatar May 10 '23 08:05 AurelienBallier