You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.9 KiB
46 lines
1.9 KiB
1 month ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
using TouchSocket.Core;
|
||
|
using TouchSocket.SerialPorts;
|
||
|
using TouchSocket.Sockets;
|
||
|
|
||
|
namespace SunlightCentralizedControlManagement_SCCM_.UserClass
|
||
|
{
|
||
|
public class AsyncSerialPortClient
|
||
|
{
|
||
|
public static async Task PortClient(SerialPortClient portclient, string com, int BAUD)
|
||
|
{
|
||
|
//var client = new SerialPortClient();
|
||
|
portclient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到端口
|
||
|
portclient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到端口
|
||
|
portclient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从端口断开连接。此处仅主动断开才有效。
|
||
|
portclient.Closed = (client, e) => { return EasyTask.CompletedTask; };//从端口断开连接,当连接不成功时不会触发。
|
||
|
portclient.Received = async (c, e) =>
|
||
|
{
|
||
|
|
||
|
await Console.Out.WriteLineAsync(e.ByteBlock.Span.ToString(Encoding.UTF8));
|
||
|
};
|
||
|
|
||
|
await portclient.SetupAsync(new TouchSocketConfig()
|
||
|
.SetSerialPortOption(new SerialPortOption()
|
||
|
{
|
||
|
BaudRate = BAUD,//波特率
|
||
|
DataBits = 8,//数据位
|
||
|
Parity = System.IO.Ports.Parity.None,//校验位
|
||
|
PortName = com,//COM
|
||
|
StopBits = System.IO.Ports.StopBits.One,//停止位
|
||
|
})
|
||
|
.SetSerialDataHandlingAdapter(() => new PeriodPackageAdapter() { CacheTimeout = TimeSpan.FromMilliseconds(100) })
|
||
|
.ConfigurePlugins(a =>
|
||
|
{
|
||
|
//a.Add<MyPlugin>();
|
||
|
}));
|
||
|
|
||
|
await portclient.ConnectAsync();
|
||
|
}
|
||
|
}
|
||
|
}
|