FlybeatController 1.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package FlybeatController --version 1.0.2
NuGet\Install-Package FlybeatController -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FlybeatController" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FlybeatController" Version="1.0.2" />
<PackageReference Include="FlybeatController" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FlybeatController --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: FlybeatController, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FlybeatController@1.0.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FlybeatController&version=1.0.2
#tool nuget:?package=FlybeatController&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
FlybeatController
FlybeatController 是一个用于控制相机和光源的通信控制库,提供了串口和TCP通信支持,采用链式API设计,使用简单直观。
特性
- 支持串口和TCP通信
- 自动重连机制
- 完整的事件通知系统
- 同步和异步操作支持
- 链式命令构建API
- 资源自动管理
- 丰富的错误处理机制
- 内置测试命令支持
安装
<PackageReference Include="System.IO.Ports" Version="9.0.2" />
快速开始
串口通信示例
using (var sender = new SerialCommandSender("COM1", 9600))
{
// 订阅事件
sender.DataReceived += (s, data) => Console.WriteLine($"收到数据: {data}");
sender.ConnectionStateChanged += (s, connected) =>
Console.WriteLine($"连接状态: {(connected ? "已连接" : "已断开")}");
// 连接并发送命令(同步方式)
sender.Connect();
var command = CameraCommandBuilder.CAM()
.SetStation("Station1")
.SetEnabled(true)
.Build();
sender.SendCommand(command);
}
TCP通信示例
using (var sender = new TcpCommandSender("localhost", 8080))
{
// 订阅事件
sender.DataReceived += (s, data) => Console.WriteLine($"收到数据: {data}");
sender.ConnectionStateChanged += (s, connected) =>
Console.WriteLine($"连接状态: {(connected ? "已连接" : "已断开")}");
// 连接并发送命令(同步方式)
sender.Connect();
var command = LightCommandBuilder.LIGHT()
.SetLightId("Light1")
.SetEnabled(true)
.Build();
sender.SendCommand(command);
}
命令系统
1. 相机命令
var command = CameraCommandBuilder.CAM()
.SetStation("Station1") // 设置工位
.SetEnabled(true) // 设置启用状态
.SetTriggerPosition("Pos1") // 设置触发位置
.Build();
2. 光源命令
var command = LightCommandBuilder.LIGHT()
.SetLightId("Light1") // 设置光源ID
.SetLightTriggerSource("Source1") // 设置触发源
.SetWorkMode(LightWorkMode.LightMode) // 设置工作模式
.SetBrightness("200") // 设置亮度
.SetLightingTime("1000") // 设置亮起时间
.SetEnabled(true) // 设置启用状态
.Build();
3. 测试命令
// 启动测试
var runCommand = TestCommandBuilder.TEST()
.SetCommandType(TestCommandType.Run)
.Build();
// 停止测试
var stopCommand = TestCommandBuilder.TEST()
.SetCommandType(TestCommandType.Stop)
.Build();
// 单次触发
var onceCommand = TestCommandBuilder.TEST()
.SetCommandType(TestCommandType.Once)
.Build();
// 固定频率测试
var freqCommand = TestCommandBuilder.TEST()
.SetCommandType(TestCommandType.Frequency)
.SetFrequency(20) // 设置频率为20Hz
.Build();
测试控件面板
// 创建测试控制面板
var testPanel = new TestControlPanel(sender);
// 订阅命令错误事件
testPanel.CommandError += (s, ex) =>
{
Console.WriteLine($"命令执行错误: {ex.Message}");
};
// 添加到窗体
form.Controls.Add(testPanel);
通信组件
SerialCommandSender
串口通信发送器,提供串口通信功能。
主要方法
// 同步连接
public void Connect();
// 同步断开连接
public void Disconnect();
// 同步发送命令
public bool SendCommand(ICommand command);
// 异步发送命令(保留用于特殊场景)
public Task SendCommandAsync(ICommand command);
TcpCommandSender
TCP通信发送器,提供TCP网络通信功能。
主要方法
// 同步连接
public void Connect();
// 同步断开连接
public void Disconnect();
// 同步发送命令
public bool SendCommand(ICommand command);
// 异步发送命令(保留用于特殊场景)
public Task SendCommandAsync(ICommand command);
事件系统
所有通信组件都支持以下事件:
// 数据接收事件
public event EventHandler<string> DataReceived;
// 错误事件
public event EventHandler<Exception> ErrorOccurred;
// 连接状态事件
public event EventHandler<bool> ConnectionStateChanged;
// 重连状态事件
public event EventHandler<string> ReconnectStatus;
错误处理示例
using (var sender = new SerialCommandSender("COM1", 9600))
{
try
{
sender.ErrorOccurred += (s, ex) =>
{
Console.WriteLine($"发生错误: {ex.Message}");
};
sender.ConnectionStateChanged += (s, connected) =>
{
if (!connected)
{
Console.WriteLine("连接已断开,等待自动重连...");
}
};
sender.Connect(); // 使用同步连接
var command = TestCommandBuilder.TEST()
.SetCommandType(TestCommandType.Run)
.Build();
if (!sender.SendCommand(command)) // 使用同步发送
{
Console.WriteLine("命令发送失败");
}
}
catch (Exception ex)
{
Console.WriteLine($"操作错误: {ex.Message}");
}
}
最佳实践
同步与异步
- 优先使用同步方法(Connect、SendCommand)
- 异步方法保留用于特殊场景
- 在UI线程中使用时注意避免阻塞
资源管理
- 使用 using 语句确保资源正确释放
- 及时取消不需要的事件订阅
错误处理
- 订阅 ErrorOccurred 事件处理错误
- 检查同步方法的返回值
- 实现适当的重试策略
测试命令使用
- 使用 TestControlPanel 快速实现测试界面
- 根据需要自定义测试频率
- 正确处理测试命令的错误情况
注意事项
通信操作
- 同步方法可能会阻塞,注意在UI线程中的使用
- 确保在断开连接前停止所有测试
- 正确处理连接断开的情况
测试命令
- 频率设置范围建议在1-1000Hz之间
- 启动测试前确保设备处于就绪状态
- 正确处理测试过程中的异常情况
资源管理
- 及时释放不需要的资源
- 正确处理事件订阅的生命周期
错误处理
try
{
using (var sender = new SerialCommandSender("COM1", 9600))
{
await sender.ConnectAsync();
await sender.SendCommandAsync(command);
}
}
catch (InvalidOperationException ex)
{
// 处理串口未打开等错误
Console.WriteLine($"操作错误: {ex.Message}");
}
catch (Exception ex)
{
// 处理其他错误
Console.WriteLine($"发生错误: {ex.Message}");
}
扩展性
该库设计时考虑了扩展性,您可以:
- 实现自己的
ICommandSender来支持其他通信协议 - 创建新的命令类型,继承
BaseCommand - 添加新的命令构建器,继承
BaseCommandBuilder
贡献
欢迎提交问题和改进建议!
许可证
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET Framework | net48 is compatible. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.8
- System.IO.Ports (>= 9.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
为飞拍控制器提供的SDK驱动
1.适配.net 4.8框架应用