Переглянути джерело

串口通讯修改

undefined
pry 2 роки тому
джерело
коміт
e18737ebbc
2 змінених файлів з 84 додано та 23 видалено
  1. +69
    -15
      HKCardOUT/ViewModels/RootViewModel.cs
  2. +15
    -8
      UHFHelper/UHF_RS485_Helper.cs

+ 69
- 15
HKCardOUT/ViewModels/RootViewModel.cs Переглянути файл

@@ -8,6 +8,7 @@ using Stylet;
using StyletIoC;
using System;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
@@ -54,23 +55,76 @@ namespace HKCardOUT.ViewModels
{
ReadFunc -= ReadCard;
ReadFunc += ReadCard;
// 打开串口
UHF_RS485_Helper.GetInstance().Open(new SerialParam
{
PortName = DataBus.COM,
BaudRate = 57600,
DataBits = 8
});
if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
{
HandyControl.Controls.Growl.InfoGlobal("串口打开失败");

}
thread1 = new(new ThreadStart(GetData))
ThreadManage.GetInstance().Start(new Action(() =>
{
IsBackground = true
};
thread1.Start();
while (!SerialPort.GetPortNames().Contains(DataBus.COM))
{
Thread.Sleep(1000);
}

UHF_RS485_Helper.GetInstance().DisConnect = new Action(() =>
{
ThreadManage.GetInstance().StopTask("串口监听");
});

UHF_RS485_Helper.GetInstance().OpenOk = new Action(() =>
{
ThreadManage.GetInstance().StartLong(new Action(() =>
{
if (UHF_RS485_Helper.GetInstance().GetSerialPortState())
{
DataBus.StoreInfo.Devices.ForEach(item =>
{
var res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt());

if (res != null)
{
if (!res.ResData.IsMatch(new Regex("0{20}")))
ReadFunc?.Invoke(res);
}
});
}

//if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
//{
// HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
// //timer1.Stop();
// return;
//}
//else
//{

//}
Thread.Sleep(10);
}), "串口监听");
});

// 打开串口
UHF_RS485_Helper.GetInstance().Open(new SerialParam
{
PortName = DataBus.COM,
BaudRate = 57600,
DataBits = 8
});
if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
{
HandyControl.Controls.Growl.InfoGlobal("串口打开失败");

}


}), "打开串口");





//thread1 = new(new ThreadStart(GetData))
//{
// IsBackground = true
//};
//thread1.Start();
}
/// <summary>
/// 读取信息


+ 15
- 8
UHFHelper/UHF_RS485_Helper.cs Переглянути файл

@@ -7,6 +7,9 @@ namespace UHFHelper
{
private SerialPort? _serialPort;
private SerialParam? _serialParam;
public Action OpenOk { get; set; }

public Action DisConnect { get; set; }

/// <summary>
/// 单例模式
@@ -33,11 +36,13 @@ namespace UHFHelper
try
{
_serialPort.Open();
if (_serialPort.IsOpen) OpenOk?.Invoke();

}
catch (Exception)
{
}
}
}
/// <summary>
@@ -46,12 +51,13 @@ namespace UHFHelper
/// <returns></returns>
public bool GetSerialPortState()
{
if (_serialPort!=null)
if (_serialPort != null)
{
return _serialPort.IsOpen;
}
else
{
DisConnect?.Invoke();
return false;
}
}
@@ -73,7 +79,7 @@ namespace UHFHelper
/// </summary>
/// <param name="adr">设备读取地址</param>
/// <returns></returns>
public DKoutput? ReadCard(int adr)
public DKoutput? ReadCard(int adr)
{
if (_serialPort == null) return null;
DKoutput dKoutput = new();
@@ -168,7 +174,8 @@ namespace UHFHelper
}
}
return buffer;
}catch(Exception ex)
}
catch (Exception ex)
{
return null;
}
@@ -186,7 +193,7 @@ namespace UHFHelper
/// 读取数据
/// </summary>
/// <returns></returns>
public byte[]? GetMessage(int count,long timeout)
public byte[]? GetMessage(int count, long timeout)
{
if (_serialPort == null) return null;
var stopwatch = Stopwatch.StartNew();
@@ -249,10 +256,10 @@ namespace UHFHelper
/// <summary>
/// 发出蜂鸣声音3次,代表扣款成功!
/// </summary>
public void OpenBeep(int addr)
public void OpenBeep(int addr)
{
byte[] sendData = new byte[]{ 0x07, (byte)addr, 0x33, 0x08, 0x02, 0x03, 0x33,0xB1 };
byte[] sendData = new byte[] { 0x07, (byte)addr, 0x33, 0x08, 0x02, 0x03, 0x33, 0xB1 };
}
}



Завантаження…
Відмінити
Зберегти