|
|
|
@ -15,6 +15,7 @@ namespace IoTNode.DeviceServices.SerialPortManager
|
|
|
|
|
public class SerialPortService : BaseDeviceService
|
|
|
|
|
{
|
|
|
|
|
private const string _productNumber = "serialport";
|
|
|
|
|
private static object lockObject = new object();
|
|
|
|
|
|
|
|
|
|
public SerialPortService(IServiceProvider applicationServices, IConfiguration configuration)
|
|
|
|
|
: base(applicationServices, configuration)
|
|
|
|
@ -26,11 +27,10 @@ namespace IoTNode.DeviceServices.SerialPortManager
|
|
|
|
|
this.Notify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Exec(string id, string name)
|
|
|
|
|
public void Exec(string deviceNumber, string name)
|
|
|
|
|
{
|
|
|
|
|
using var scope = _applicationServices.CreateScope();
|
|
|
|
|
var repo = scope.ServiceProvider.GetService<IRepository<Device>>();
|
|
|
|
|
var deviceNumber = $"serialportonHelper.Instance.GetCPUNumber()";
|
|
|
|
|
var device = repo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.Number == deviceNumber);
|
|
|
|
|
var buttons = device.Data.FirstOrDefault(o => o.Key == "Buttons")?.Value.FromJson<List<SPCommandModel>>(); ;
|
|
|
|
|
var button = buttons.FirstOrDefault(o => o.Name == name);
|
|
|
|
@ -61,13 +61,17 @@ namespace IoTNode.DeviceServices.SerialPortManager
|
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<挂起>")]
|
|
|
|
|
public static void ExecInternal(string port, int baud, int dataBits, int partity, int stopBits, string message)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var sp = new SerialPort(port, baud, (Parity)partity, dataBits, (StopBits)stopBits);
|
|
|
|
|
sp.Open();
|
|
|
|
|
var bytes = message.HexToBytes();
|
|
|
|
|
sp.Write(bytes, 0, bytes.Length);
|
|
|
|
|
sp.Close();
|
|
|
|
|
lock (lockObject)
|
|
|
|
|
{
|
|
|
|
|
using var sp = new SerialPort(port, baud, (Parity)partity, dataBits, (StopBits)stopBits);
|
|
|
|
|
sp.Open();
|
|
|
|
|
var bytes = message.HexToBytes();
|
|
|
|
|
sp.Write(bytes, 0, bytes.Length);
|
|
|
|
|
sp.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
@ -102,7 +106,7 @@ namespace IoTNode.DeviceServices.SerialPortManager
|
|
|
|
|
};
|
|
|
|
|
repo.Add(device);
|
|
|
|
|
this.UpdateDevice(repo, device);
|
|
|
|
|
var buttons = new List<SPCommandModel>() { new SPCommandModel { Name = "测试", PortName = "/dev/ttyS0", BaudRate = 9600, Parity = 0, DataBits = 8, StopBits = 1, Message = "" } }.ToJson();
|
|
|
|
|
var buttons = new List<SPCommandModel>() { new SPCommandModel { Name = "测试",PortName = "COM1", BaudRate = 9600, Parity = 0, DataBits = 8, StopBits = 1, Message = "0123456789ABCDEF" } }.ToJson();
|
|
|
|
|
this.UpdateData(repo, device, device.CreateData("Buttons", buttons, DeviceDataType.String, "指令", timestamp: DateTimeOffset.Now.ToUnixTimeMilliseconds()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|