Browse Source

串口通讯修改

undefined
pry 2 years ago
parent
commit
e18737ebbc
2 changed files with 84 additions and 23 deletions
  1. +69
    -15
      HKCardOUT/ViewModels/RootViewModel.cs
  2. +15
    -8
      UHFHelper/UHF_RS485_Helper.cs

+ 69
- 15
HKCardOUT/ViewModels/RootViewModel.cs View File

@@ -8,6 +8,7 @@ using Stylet;
using StyletIoC; using StyletIoC;
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
@@ -54,23 +55,76 @@ namespace HKCardOUT.ViewModels
{ {
ReadFunc -= ReadCard; ReadFunc -= ReadCard;
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> /// <summary>
/// 读取信息 /// 读取信息


+ 15
- 8
UHFHelper/UHF_RS485_Helper.cs View File

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

public Action DisConnect { get; set; }


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

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




Loading…
Cancel
Save