通过USB端口的串行交互,以开发Windows应用程序
我刚刚到达颤音/飞镖,并以这些语言为新手,我对协议或图书馆并不那么了解。我确实有C ++和Fortran编码经验。我现在打算做的是通过USB端口与嵌入式设备之间建立串行通信,同时设计一个带有UI的应用程序,以与设备与“ to to to fro”通信进行交互。这不过是 this 。 我已经使用USB_Serial
,Flutter_libSerialPort
和libserialport
插件>插件/库,但没有运气。 有人可以指导我吗?
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在学习三天后,我们得出一个结论,即所使用的库仍处于其萌芽阶段,并且没有我们想要的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.
您对“输出”变量的“ const”一词编码是错误的,也将输出存储在void usbdata()中,因此必须从方法内将其取出
。 //////////////////
我决定根据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:
//////////////////
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.