Browse Source

数据读取

Lishi
pry 2 years ago
parent
commit
223568cc5f
2 changed files with 51 additions and 40 deletions
  1. +26
    -11
      HKCardOUT/ViewModels/RootViewModel.cs
  2. +25
    -29
      UHFHelper/UHF_RS485_Helper.cs

+ 26
- 11
HKCardOUT/ViewModels/RootViewModel.cs View File

@@ -106,25 +106,40 @@ namespace HKCardOUT.ViewModels
// Thread.Sleep(50);
//}), "串口监听");

Object async = new object();
DataBus.StoreInfo.Devices.ForEach(item =>
{
ThreadManage.GetInstance().StartLong(new Action(() =>
ThreadManage.GetInstance().Start(new Action(() =>
{
if (UHF_RS485_Helper.GetInstance().GetSerialPortState())
try
{
if (DataBus.StoreInfo != null)
while (true)
{
var res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt());
if (res != null)
if (UHF_RS485_Helper.GetInstance().GetSerialPortState())
{
HKLog.HKLogImport.WriteInfo($"卡号地址:{res.Address}----------卡号数据:{res.ResData}");
if (!res.ResData.IsMatch(new Regex("0{20}")))
ReadFunc?.Invoke(res);
if (DataBus.StoreInfo != null)
{
DKoutput res = null;
Monitor.TryEnter(async, 2000);
res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt());
Monitor.Exit(async);
if (res != null)
{
HKLog.HKLogImport.WriteInfo($"{DateTime.Now.ToString("HH:mm:ss")} 卡号地址:{res.Address}----------卡号数据:{res.ResData}");
if (!res.ResData.IsMatch(new Regex("0{20}")))
ReadFunc?.Invoke(res);
}
}
}
Thread.Sleep(2000);
}
}
Thread.Sleep(2000);
}), $"监听{item.Address}");
catch (Exception ex)
{
HKLog.HKLogImport.WriteInfo($"{DateTime.Now.ToString("HH:mm:ss")} 异常信息:{ex.Message}");
Monitor.Exit(async);
}
}), $"监听{item.Address}", true);
});
});

@@ -299,7 +314,7 @@ namespace HKCardOUT.ViewModels
SaleLogDto Info = null;
if (Stacks.Count > 0)
{
Stacks.TryDequeue(out SaleLogDto dto);
Stacks.TryDequeue(out SaleLogDto dto);
dto = Info;
}
if (Info != null)


+ 25
- 29
UHFHelper/UHF_RS485_Helper.cs View File

@@ -73,7 +73,6 @@ namespace UHFHelper
}


private object asy = new object();
/// <summary>
/// 读卡方法,返回读卡器数据
/// </summary>
@@ -81,39 +80,36 @@ namespace UHFHelper
/// <returns></returns>
public DKoutput? ReadCard(int adr)
{
lock (asy)
if (_serialPort == null) return null;
DKoutput dKoutput = new();
var readByte = ReadByte(adr);
_serialPort.Write(readByte, 0, readByte.Length);
var result = SendMessage(readByte, 150, 18);
if (result == null)
{
return null;
}
else
{
if (_serialPort == null) return null;
DKoutput dKoutput = new();
var readByte = ReadByte(adr);
_serialPort.Write(readByte, 0, readByte.Length);
var result = SendMessage(readByte, 150, 18);
if (result == null)
//获取校验码
var crc = result.Skip(16).Take(2).ToArray();
//获取卡号
var cardNo = result.Skip(6).Take(10).ToArray();
//获取读卡器数据
var readData = result.Skip(0).Take(16).ToArray();
//获取读卡器地址
var address = result.Skip(1).Take(1).ToArray();
//判断数据是否合法
var temcrc = CRC16.ToCRC16(readData);
if (crc.ByteArrayToHexString() == temcrc.ByteArrayToHexString())
{
return null;
dKoutput.Address = address.ByteArrayToHexString();
dKoutput.ResData = cardNo.ByteArrayToHexString();
return dKoutput;
}
else
{
//获取校验码
var crc = result.Skip(16).Take(2).ToArray();
//获取卡号
var cardNo = result.Skip(6).Take(10).ToArray();
//获取读卡器数据
var readData = result.Skip(0).Take(16).ToArray();
//获取读卡器地址
var address = result.Skip(1).Take(1).ToArray();
//判断数据是否合法
var temcrc = CRC16.ToCRC16(readData);
if (crc.ByteArrayToHexString() == temcrc.ByteArrayToHexString())
{
dKoutput.Address = address.ByteArrayToHexString();
dKoutput.ResData = cardNo.ByteArrayToHexString();
return dKoutput;
}
else
{
return null;
}
return null;
}
}
}


Loading…
Cancel
Save