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.

197 lines
9.9 KiB

using ScottPlot.Plottables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.SerialPorts;
using TouchSocket.Sockets;
using static SkiaSharp.HarfBuzz.SKShaper;
namespace SunlightCentralizedControlManagement_SCCM_.UserClass
{
public class AsyncSerialPortClient
{
public static string PortCOM1 { get; set; }
public static string PortCOM2 { get; set; }
public static string PortCOM3 { get; set; }
public static string PortCOM4 { get; set; }
public static void SCAPI(string dat)
{
}
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 = (client, e) =>
{
_responseEvent.Set();
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
SCAPI(DAT);
return EasyTask.CompletedTask; ;
};
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();
}
public static async Task PortClient2(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 = (client, e) =>
{
_responseEvent2.Set();
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
//string DAT1 = CRCcheck16.ToCRC16(e.ByteBlock.Span.ToString(Encoding.UTF8));
SCAPI(DAT);
return EasyTask.CompletedTask; ;
};
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();
}
public static async Task PortClient3(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 = (client, e) =>
{
_responseEvent3.Set();
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
//string DAT1 = CRCcheck16.ToCRC16(e.ByteBlock.Span.ToString(Encoding.UTF8));
SCAPI(DAT);
return EasyTask.CompletedTask; ;
};
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();
}
public static async Task PortClient4(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 = (client, e) =>
{
_responseEvent4.Set();
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8);
//string DAT1 = CRCcheck16.ToCRC16(e.ByteBlock.Span.ToString(Encoding.UTF8));
SCAPI(DAT);
return EasyTask.CompletedTask; ;
};
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();
}
private static readonly ManualResetEventSlim _responseEvent = new ManualResetEventSlim(false);
private static readonly ManualResetEventSlim _responseEvent2 = new ManualResetEventSlim(false);
private static readonly ManualResetEventSlim _responseEvent3 = new ManualResetEventSlim(false);
private static readonly ManualResetEventSlim _responseEvent4 = new ManualResetEventSlim(false);
/// <summary>
/// 发送指令并等待响应,超时时间为500ms
/// </summary>
/// <param name="command">要发送的指令字节数组</param>
/// <returns>从机的响应数据,超时则为null</returns>
public static void SendCommandAndWait(SerialPortClient serialPortClients, string command)
{
_responseEvent.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
}
public static void SendCommandAndWait2(SerialPortClient serialPortClients, string command)
{
_responseEvent2.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent2.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
}
public static void SendCommandAndWait3(SerialPortClient serialPortClients, string command)
{
_responseEvent3.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent3.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
}
public static void SendCommandAndWait4(SerialPortClient serialPortClients, string command)
{
_responseEvent4.Reset();
serialPortClients.Send(command); // 发送指令
// 等待500毫秒或直到收到响应
_responseEvent4.Wait(TimeSpan.FromMilliseconds(1000));
// 重置事件状态
}
}
}