dart 利用USB口串行交互开发Flutter的Windows应用程序

bzzcjhmw  于 2023-09-28  发布在  Flutter
关注(0)|答案(2)|浏览(252)

我刚到flutter/dart,是这些语言的新手,我对协议或库不是很了解。我有C++和Fortran编程经验。我现在打算做的是通过USB端口在我的PC和我的嵌入式设备之间建立串行通信,同时设计一个具有UI的应用程序与设备进行“来回”通信。这只是this。我使用过usb_serialflutter_libserialportlibserialport插件/库,但没有运气。有人能给我指路吗?
这是我的代码:

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),
        ),
      ),
    );
  }
}

我打算在这里做的是从USB读取一些字节,然后在Flutter应用程序上显示它们。该应用程序成功地从USB读取字节,并在没有小部件代码的情况下执行时在控制台上打印它们。当我集成小部件代码时,它显示以下错误:

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.
fcy6dtqo

fcy6dtqo1#

经过连续三天的研究,我们得出的结论是,所使用的库仍处于萌芽阶段,没有我们想要的API。回到硬件模式。和平

8oomwypt

8oomwypt2#

你的编码不正确,“output”变量的术语“const”是错误的,而且输出存储在void usbData()中,所以它必须从方法内部取出:

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),
        ),
      ),
    );
  }
}

/我决定开始一个RS_485 Modbus协议下的工业监控项目,所以我将使用这个库。我希望这个图书馆能解决我的问题。

相关问题