cabbi / modbus_client_serial

BSD 3-Clause "New" or "Revised" License
9 stars 6 forks source link

libserialport #1

Closed magicmatt007 closed 1 year ago

magicmatt007 commented 1 year ago

I'd like to use this package on Windows 10 with an USB Modbus RTU stick.

The README says that I need to take care of building libserialport myself. I think I go this job done, and have a "libserialport.dll" now. I'm not sure, where to put it though. I tried ...\windows\system32 but that doesn't work.

This code creates an error:

  // Create the modbus client.
  var modbusClient = ModbusClientSerialRtu(
      portName: "COM9",
      unitId: 1,
      baudRate: SerialBaudRate.b19200,
      dataBits: SerialDataBits.bits8,
      parity: SerialParity.even,
      stopBits: SerialStopBits.one,
      flowControl: SerialFlowControl.rtsCts,
      responseTimeout: const Duration(seconds: 1));

  await modbusClient.connect();

Error:

Invalid argument(s): Failed to load dynamic library 'serialport.dll': error code 126
#0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:43)

Any proposals?

Another question: As I want to use the library with Flutter - Windows to start with but maybe also Android - I wonder if there is an easier way to get hold of libserialport? https://pub.dev/packages/libserialport suggests, that flutter_liberserialport could be used for this. But again, I don't know how to do this with this package here.

Many thanks!

cabbi commented 1 year ago

As the error reported it is looking for 'serialport.dll' (Failed to load dynamic library 'serialport.dll'. Please rename libserialport.dll to serialport.dll

I will adjust the ReadMe in next releases.

Please let me know if this solves the issue so I can close it, thanks

cabbi commented 1 year ago

>Another question: I am not using modbus_client in any Flutter project for now. That said I am not sure how to proceed. You might try to add flutter_liberserialport package in your project and see this will take the libserialport library

magicmatt007 commented 1 year ago

I changed your modbus_client_serial.dart:

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

Now it works without problems.

magicmatt007 commented 1 year ago

As the error reported it is looking for 'serialport.dll' (Failed to load dynamic library 'serialport.dll'. Please rename libserialport.dll to serialport.dll

I will adjust the ReadMe in next releases.

Please let me know if this solves the issue so I can close it, thanks

Just changing the name of the DLL doesn't help.

Apparently, the location of the DLL can be passed by setting an environment variable LIBSERIALPORT_PATH with the path of the DLL. The error message then at least picks up that path, but still complains that it doesn't find the file. I think the tricky part here is configuring the libserialport package correctly.

I want to use your package only with Flutter. This can be done easily by replacing libserialport.dart with flutter_libserialport.dart. See my other post above. So I give up trying to get the pure dart version doing the job.

cabbi commented 1 year ago

I changed your modbus_client_serial.dart:

// import 'package:libserialport/libserialport.dart'; import 'package:flutter_libserialport/flutter_libserialport.dart'; Now it works without problems.

You also added flutter_liberserialport as a dependency in your pubspec.yaml, isn't it?

I am not familiar with plugins and I don't know if a Dart Package can add plugins as a flutter one does. I will investigate further and share my findings.

magicmatt007 commented 1 year ago

I changed your modbus_client_serial.dart: // import 'package:libserialport/libserialport.dart'; import 'package:flutter_libserialport/flutter_libserialport.dart'; Now it works without problems.

You also added flutter_liberserialport as a dependency in your pubspec.yaml, isn't it?

I am not familiar with plugins and I don't know if a Dart Package can add plugins as a flutter one does. I will investigate further and share my findings.

Yes, initially I added flutter_libersialport to my own pubspec.yaml. But after your comment, I now removed it from mine and added it to pubspec.yaml of your package as shown below. It still works (after a "flutter pub get" command to update the depenencies).

name: modbus_client_serial
description: This is the Modbus Client (ASCII and RTU) package sending requests to a remote device via Serial Port
version: 1.0.0+1
repository: https://github.com/cabbi/modbus_client_serial

environment:
  sdk: '>=2.17.0 <4.0.0'

dependencies:
  # libserialport: ^0.3.0+1
  flutter_libserialport: ^0.3.0
  logging: ^1.2.0
  synchronized: ^3.1.0
  modbus_client: ^1.0.0
cabbi commented 1 year ago

I did a fresh new flutter app, then I added modbus_client_serial package and got the expected error: missing "serialport.dll" Then I simple added _flutter pub add flutterlibserialport to my new app and now app is running.

That said I will update readme file in case you use modbus_client_serial in a flutter app. Simply add flutter_libserialport package to you project and run it without any external library build. :)