3

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.
PS_O5
  • 41
  • 1
  • 6
  • "but no luck" is not an error description we can work with. And StackOverflow isn't a site for guidance. So show the code you have tried, and describe what happens and what you expect to happen instead. Then you are more likely to get an answer. – Codo Apr 03 '22 at 15:07
  • @Codo I shall update my question and its description. I am new to this site so I don't know the SOP here. Thank you for letting me know this. – PS_O5 Apr 03 '22 at 19:10
  • It looks as if the code does not match the error messages. Can you provide a matching set? And the use of `output` in `Text(output)` is rather strange. What should it refer to? – Codo Apr 06 '22 at 11:00

2 Answers2

1

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.

PS_O5
  • 41
  • 1
  • 6
0

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.