通过USB端口的串行交互,以开发Windows应用程序

发布于 2025-01-19 01:42:08 字数 2416 浏览 1 评论 0原文

我刚刚到达颤音/飞镖,并以这些语言为新手,我对协议或图书馆并不那么了解。我确实有C ++和Fortran编码经验。我现在打算做的是通过USB端口与嵌入式设备之间建立串行通信,同时设计一个带有UI的应用程序,以与设备与“ to to to fro”通信进行交互。这不过是 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2025-01-26 01:42:08

在学习三天后,我们得出一个结论,即所使用的库仍处于其萌芽阶段,并且没有我们想要的API。返回硬件模式。和平。

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.

枫林﹌晚霞¤ 2025-01-26 01:42:08

您对“输出”变量的“ 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协议启动一个工业监控项目,因此我将使用此库。我希望这个图书馆能解决我的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文