Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10114

libserial is not detected in my dart programm

I made a minimalistic dart software that interfaces a serial port:

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

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

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

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

But once I run:

dart run

I get the following error:

Unhandled exception:
Invalid argument(s): Failed to load dynamic library 'libserialport.so': libserialport.so: cannot open shared object file: No such file or directory
#0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:12:43)
#1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:23:12)
#2      dylib
package:libserialport/src/dylib.dart:32
#3      _SerialPortImpl._init.<anonymous closure>
package:libserialport/src/port.dart:221
#4      Util.call
package:libserialport/src/util.dart:37
#5      _SerialPortImpl._init
package:libserialport/src/port.dart:221
#6      new _SerialPortImpl
package:libserialport/src/port.dart:211
#7      new SerialPort
package:libserialport/src/port.dart:72
#8      main
bin/serial.dart:5
#9      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:32)
#10     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

Exited (255)

I run it in a linux machine.

Upvotes: 0

Views: 1284

Answers (2)

Raj
Raj

Reputation: 1284

The issue is the dynamic library is unable able to load from the system. The solution is

Install libserialport-dev package

for Debian based system use

sudo apt install libserialport-dev

Note: The dart package libserialport uses FFI to access the native API. Under the hood it uses libserialport a minimal, cross-platform shared library written in C. The package actually contain the c code. when we build or run the application it's supposed to be compiled it to libserial.so, but due to some reason this doesn't happen when we use this package directly.

If you use flutter framework use flutter_libserialport package, this take care of creating the .so file without any issue

Upvotes: 0

Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10114

The problem as you see in the error message is that expects somewhere to be found the libserial.so library.

First and formemost we need to locate the library and see if it is installed:

ldconfig -p | grep libserial

At no output you can install it as you can see here. For linux mint and debian based distros run:

sudo apt-get install libserial0

Then re-run the command:

ldconfig -p | grep libserial

If the command after the installation has an output then you must check if the path is /lib/libserialport.so. In my case it was not:

    libserialport.so.0 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libserialport.so.0

Sometimes after so in librarie's names a number may be appended. In that case we can symlink the libary to the /usr/lib path like that:

sudo ln -s /usr/lib/x86_64-linux-gnu/libserialport.so.0 /usr/lib/libserialport.so

Also, another case is if the library is not located to /usr/lib/libserial.so but in a subfolder inside the /usr/lib path.

Upvotes: 0

Related Questions