PS_O5
PS_O5

Reputation: 41

Serial interaction through USB port for developing windows application on flutter

I have just arrived on flutter/dart and being novice in these languages, I am not so aware about the protocols or the libraries. I do have C++ and Fortran coding experience. What I intend to do now is to establish a serial communication between my pc and my embedded device through USB port and at the same time design an app with its UI to interact with the device for "to-fro" communication. This is nothing but this. I have used usb_serial , flutter_libserialport and libserialport plugins/libraries but no luck. Can someone guide me?

This is my code:

import 'package:serial_port_win32/serial_port_win32.dart';
import 'package:flutter/material.dart';


void main() {
  usbData();
  runApp(const MyApp());
}

void usbData(){

  final ports = SerialPort.getAvailablePorts();
  String output = "";
  final port = SerialPort("COM9", openNow: true, ByteSize: 8);
  port.BaudRate = 115200;
  port.readOnListenFunction = (value) {
  var temp = value;
  output = temp.toString();
  print(output);
  };
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Welcome to Flutter'),
        ),
        body: const Center(
          child: Text(output),
        ),
      ),
    );
  }
}

What I intend to do here is to read some bytes from USB and then show them on flutter app. The app successfully reads bytes from USB and print them on console when its executed without widget code. When I integrate the widget code it shows following error:

lib/main.dart(3,8): error G67247B7E: Expected ';' after this. [C:\Users\mill\build\windows\flutter\flutter_assemble.vcxproj]
lib/main.dart(3,8): error GFAA2A68C: Error when reading 'lib/lib': The system cannot find the file specified. [C:\Users\mill\build\windows\flutter\flutter_assemble.vcxproj]
lib/main.dart(67,56): error G4127D1E8: The getter 'readOnListenFunction' isn't defined for the class 'SerialPort'. [C:\Users\mill\build\windows\flutter\flutter_assemble.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(245,5): error MSB8066: Custom build for 'C:\Users\mill\build\windows\CMakeFiles\82d3aa4e044df461f20bf9b726d9f159\flutter_windows.dll.rule;C:\Users\mill\build\windows\CMakeFiles\033e83e7c3b917ce9552b6364ce66756\flutter_assemble.rule' exited with code 1. [C:\Users\mill\build\windows\flutter\flutter_assemble.vcxproj]
Exception: Build process failed.

Upvotes: 3

Views: 5380

Answers (2)

Ali Parsa
Ali Parsa

Reputation: 1

You have coded incorrectly, the term "const" for the "output" variable is wrong, also the output is stored inside the void usbData(), so it must be taken out from inside the method:

import 'package:serial_port_win32/serial_port_win32.dart';
import 'package:flutter/material.dart';

String output = "";
 
void usbData(){
  final ports = SerialPort.getAvailablePorts();
  final port = SerialPort("COM9", openNow: true, ByteSize: 8);
  port.BaudRate = 115200;
  port.readOnListenFunction = (value) {
  var temp = value;
  output = temp.toString();
  print(output);
  };
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Welcome to Flutter'),
        ),
        body:   Center(
          child: Text(output),
        ),
      ),
    );
  }
}

////////////////// I have decided to start an industrial monitoring project under RS_485 Modbus protocol, so I will use this library. I hope this library will solve my problem.

Upvotes: 0

PS_O5
PS_O5

Reputation: 41

After studying for three straight days, we came to a conclusion that the libraries used are still in their budding phase and don't have the APIs the way we want. Back to hardware mode. PEACE.

Upvotes: 1

Related Questions