@@ -80,13 +80,39 @@ namespace HKCardOUT.ViewModels | |||
UHF_RS485_Helper.GetInstance().OpenOk = new Action(() => | |||
{ | |||
ThreadManage.GetInstance().StartLong(new Action(() => | |||
//ThreadManage.GetInstance().StartLong(new Action(() => | |||
//{ | |||
// if (UHF_RS485_Helper.GetInstance().GetSerialPortState()) | |||
// { | |||
// if (DataBus.StoreInfo != null) | |||
// { | |||
// DataBus.StoreInfo.Devices.ForEach(item => | |||
// { | |||
// var res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt()); | |||
// if (res != null) | |||
// { | |||
// HKLog.HKLogImport.WriteInfo($"卡号地址:{res.Address}----------卡号数据:{res.ResData}"); | |||
// if (!res.ResData.IsMatch(new Regex("0{20}"))) | |||
// ReadFunc?.Invoke(res); | |||
// if (res.ResData.Contains("888") || res.ResData.Contains("999") || res.ResData.Contains("666")) | |||
// ReadFunc?.Invoke(res); | |||
// } | |||
// }); | |||
// } | |||
// else | |||
// Growl.InfoGlobal("系统已离线,请连接网络,并重启程序!"); | |||
// } | |||
// Thread.Sleep(50); | |||
//}), "串口监听"); | |||
DataBus.StoreInfo.Devices.ForEach(item => | |||
{ | |||
if (UHF_RS485_Helper.GetInstance().GetSerialPortState()) | |||
ThreadManage.GetInstance().StartLong(new Action(() => | |||
{ | |||
if (DataBus.StoreInfo != null) | |||
if (UHF_RS485_Helper.GetInstance().GetSerialPortState()) | |||
{ | |||
DataBus.StoreInfo.Devices.ForEach(item => | |||
if (DataBus.StoreInfo != null) | |||
{ | |||
var res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt()); | |||
if (res != null) | |||
@@ -95,14 +121,11 @@ namespace HKCardOUT.ViewModels | |||
if (!res.ResData.IsMatch(new Regex("0{20}"))) | |||
ReadFunc?.Invoke(res); | |||
} | |||
}); | |||
} | |||
} | |||
else | |||
Growl.InfoGlobal("系统已离线,请连接网络,并重启程序!"); | |||
} | |||
Thread.Sleep(50); | |||
}), "串口监听"); | |||
Thread.Sleep(2000); | |||
}), $"监听{item.Address}"); | |||
}); | |||
}); | |||
// 打开串口 | |||
@@ -73,7 +73,7 @@ namespace UHFHelper | |||
} | |||
private object asy = new object(); | |||
/// <summary> | |||
/// 读卡方法,返回读卡器数据 | |||
/// </summary> | |||
@@ -81,36 +81,39 @@ namespace UHFHelper | |||
/// <returns></returns> | |||
public DKoutput? ReadCard(int adr) | |||
{ | |||
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 | |||
lock (asy) | |||
{ | |||
//获取校验码 | |||
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()) | |||
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) | |||
{ | |||
dKoutput.Address = address.ByteArrayToHexString(); | |||
dKoutput.ResData = cardNo.ByteArrayToHexString(); | |||
return dKoutput; | |||
return null; | |||
} | |||
else | |||
{ | |||
return 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()) | |||
{ | |||
dKoutput.Address = address.ByteArrayToHexString(); | |||
dKoutput.ResData = cardNo.ByteArrayToHexString(); | |||
return dKoutput; | |||
} | |||
else | |||
{ | |||
return null; | |||
} | |||
} | |||
} | |||
} | |||