From f2e0b69f3776f759d6b92513e00c269121bd1ecf Mon Sep 17 00:00:00 2001
From: "747575620@qq.com" <747575620@qq.com>
Date: Sat, 24 Sep 2022 19:33:27 +0800
Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8D=A1=E5=99=A8=E9=97=AE=E9=A2=98?=
=?UTF-8?q?=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
HKCardOUT/ViewModels/RootViewModel.cs | 67 +++++++++++++++++++++++++--
HKCardOUT/Views/RootView.xaml | 2 +-
UHFHelper/UHF_RS485_Helper.cs | 20 +++++---
test/Form1.Designer.cs | 49 ++++++++++++++++++++
test/Form1.cs | 58 ++++++++++++++++++++++-
5 files changed, 182 insertions(+), 14 deletions(-)
diff --git a/HKCardOUT/ViewModels/RootViewModel.cs b/HKCardOUT/ViewModels/RootViewModel.cs
index 8711ed2..9ed082b 100644
--- a/HKCardOUT/ViewModels/RootViewModel.cs
+++ b/HKCardOUT/ViewModels/RootViewModel.cs
@@ -35,7 +35,67 @@ namespace HKCardOUT.ViewModels
public RootViewModel(IContainer Container)
{
this.Container = Container;
- MainThread();
+
+
+ this.Activated += RootViewModel_Activated;
+ this.Closed += RootViewModel_Closed;
+
+ //MainThread();
+ }
+ ///
+ /// 关闭页面
+ ///
+ ///
+ ///
+ private void RootViewModel_Closed(object sender, CloseEventArgs e)
+ {
+ ti.Stop();
+ ti.Dispose();
+ UHF_RS485_Helper.GetInstance().Close();
+ }
+
+ System.Timers.Timer ti = new();
+ private void RootViewModel_Activated(object sender, ActivationEventArgs e)
+ {
+ 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("串口打开失败");
+
+ }
+ //开启循环监听数据
+ ti.Interval = 500;
+ ti.Elapsed += Ti_Elapsed;
+ ti.Start();
+ }
+ private void Ti_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
+ {
+ DataBus.StoreInfo.Devices.ForEach(item =>
+ {
+ if (!UHF_RS485_Helper.GetInstance().GetSerialPortState())
+ {
+ HandyControl.Controls.Growl.InfoGlobal("串口打开失败");
+ ti.Stop();
+ return;
+ }
+ else
+ {
+ var res = UHF_RS485_Helper.GetInstance().ReadCard(item.Address.AsInt()).Result;
+ if (res != null)
+ {
+ if (!res.ResData.IsMatch(new Regex("0{20}")))
+ ReadFunc?.Invoke(res);
+ }
+ }
+ });
}
protected override void OnViewLoaded()
@@ -76,11 +136,10 @@ namespace HKCardOUT.ViewModels
}
#endregion
- #region 方法
+ #region 方法 这个可以不要了
private void MainThread()
{
- ReadFunc -= ReadCard;
- ReadFunc += ReadCard;
+
ThreadManage.GetInstance().StartLong(new Action(() =>
{
try
diff --git a/HKCardOUT/Views/RootView.xaml b/HKCardOUT/Views/RootView.xaml
index e470d25..e68007b 100644
--- a/HKCardOUT/Views/RootView.xaml
+++ b/HKCardOUT/Views/RootView.xaml
@@ -81,7 +81,7 @@
HeaderStyle="{StaticResource SaleLogHeader}" />
diff --git a/UHFHelper/UHF_RS485_Helper.cs b/UHFHelper/UHF_RS485_Helper.cs
index bdcfd37..42f05c7 100644
--- a/UHFHelper/UHF_RS485_Helper.cs
+++ b/UHFHelper/UHF_RS485_Helper.cs
@@ -65,7 +65,7 @@ namespace UHFHelper
{
DKoutput dKoutput = new();
var readByte = ReadByte(adr);
- var result = await SendMessageAsync(readByte, TimeSpan.FromSeconds(1), 18);
+ var result = await SendMessageAsync(readByte, TimeSpan.FromSeconds(2), 18);
if (result == null)
{
return null;
@@ -132,16 +132,14 @@ namespace UHFHelper
var originalReadTimeout = _serialPort.ReadTimeout;
try
{
-
- _serialPort.WriteTimeout = (int)Math.Max((timeout - stopwatch.Elapsed).TotalMilliseconds, 0);
+ _serialPort.DiscardInBuffer();
_serialPort.Write(msg, 0, msg.Length);
-
var tmpCount = count;
byte[] buffer = new byte[tmpCount];
int offset = 0;
while (tmpCount > 0)
{
- _serialPort.ReadTimeout = (int)Math.Max((timeout - stopwatch.Elapsed).TotalMilliseconds, 0);
+
var readCount = _serialPort.Read(buffer, offset, tmpCount);
if (readCount==6)
{
@@ -150,8 +148,11 @@ namespace UHFHelper
}
offset += readCount;
tmpCount -= readCount;
+ if (stopwatch.ElapsedMilliseconds>=2000)
+ {
+ return null;
+ }
}
- _serialPort.DiscardInBuffer();
return buffer;
}catch(Exception ex)
{
@@ -164,6 +165,11 @@ namespace UHFHelper
}
}
+ private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
+ {
+ throw new NotImplementedException();
+ }
+
///
/// 发送数据
///
@@ -180,7 +186,7 @@ namespace UHFHelper
}
catch (TaskCanceledException)
{
- throw new TimeoutException();
+ return null;
}
return await sendTask;
}
diff --git a/test/Form1.Designer.cs b/test/Form1.Designer.cs
index d8361cf..05ce488 100644
--- a/test/Form1.Designer.cs
+++ b/test/Form1.Designer.cs
@@ -41,6 +41,11 @@
this.textBox4 = new System.Windows.Forms.TextBox();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
+ this.listView1 = new System.Windows.Forms.ListView();
+ this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
+ this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
+ this.button9 = new System.Windows.Forms.Button();
+ this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// button1
@@ -163,11 +168,50 @@
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button8_Click);
//
+ // listView1
+ //
+ this.listView1.AutoArrange = false;
+ this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
+ this.columnHeader1,
+ this.columnHeader2,
+ this.columnHeader3});
+ this.listView1.Location = new System.Drawing.Point(445, 12);
+ this.listView1.Name = "listView1";
+ this.listView1.Size = new System.Drawing.Size(343, 193);
+ this.listView1.TabIndex = 12;
+ this.listView1.UseCompatibleStateImageBehavior = false;
+ this.listView1.View = System.Windows.Forms.View.Details;
+ //
+ // columnHeader1
+ //
+ this.columnHeader1.Text = "序号";
+ this.columnHeader1.Width = 120;
+ //
+ // columnHeader2
+ //
+ this.columnHeader2.Text = "号码";
+ //
+ // button9
+ //
+ this.button9.Location = new System.Drawing.Point(88, 387);
+ this.button9.Name = "button9";
+ this.button9.Size = new System.Drawing.Size(75, 23);
+ this.button9.TabIndex = 13;
+ this.button9.Text = "button9";
+ this.button9.UseVisualStyleBackColor = true;
+ this.button9.Click += new System.EventHandler(this.button9_Click);
+ //
+ // columnHeader3
+ //
+ this.columnHeader3.Text = "次数";
+ //
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
+ this.Controls.Add(this.button9);
+ this.Controls.Add(this.listView1);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.textBox4);
@@ -204,5 +248,10 @@
private TextBox textBox4;
private Button button7;
private Button button8;
+ private ListView listView1;
+ private ColumnHeader columnHeader1;
+ private ColumnHeader columnHeader2;
+ private Button button9;
+ private ColumnHeader columnHeader3;
}
}
\ No newline at end of file
diff --git a/test/Form1.cs b/test/Form1.cs
index c63860e..56b084b 100644
--- a/test/Form1.cs
+++ b/test/Form1.cs
@@ -48,7 +48,7 @@ namespace test
var res = UHFCardHelper.GetInstance().WriteCard(textBox1.Text);
MessageBox.Show(res.ResMes);
}
-
+ List add = new List();
private void Form1_Load(object sender, EventArgs e)
{
@@ -57,6 +57,8 @@ namespace test
//{
// this.comboBox1.Items.Add(val);
//}
+ add.Add(01);
+ add.Add(02);
}
UHF_RS485_Helper uHF_RS485_Helper;
private void button5_Click(object sender, EventArgs e)
@@ -92,7 +94,59 @@ namespace test
private void button8_Click(object sender, EventArgs e)
{
- UHF_RS485_Helper.GetInstance().OpenBeep(1);
+ System.Timers.Timer ti = new();
+ ti.Interval = 500;
+ ti.Elapsed += Ti_Elapsed;
+ ti.Start();
+ }
+
+ private void Ti_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
+ {
+ foreach (var item in add)
+ {
+ var oldss = UHF_RS485_Helper.GetInstance().ReadCard(item).Result;
+ if (oldss != null)
+ {
+ //lvi.SubItems.Add(oldss.ResData ?? "");
+ //this.listView1.Items.Add(lvi);
+ this.Invoke(() =>
+ {
+ if (listView1.Items.Count==0)
+ {
+ ListViewItem lvi = new ListViewItem();
+ lvi.SubItems.Add(oldss.ResData ?? "");
+ lvi.SubItems.Add("1");
+ this.listView1.Items.Add(lvi);
+ }
+ bool isCunzai = false;
+
+ for (int i = 0; i < listView1.Items.Count; i++) //жǷListviewб
+ {
+ if (oldss.ResData == listView1.Items[i].SubItems[1].Text)
+ {
+ var aListItem = listView1.Items[i];
+ aListItem.SubItems[2].Text = Convert.ToString(Convert.ToInt32(aListItem.SubItems[2].Text) + 1);
+ isCunzai = true;
+ }
+ }
+ if (!isCunzai)
+ {
+ ListViewItem lvi = new ListViewItem();
+ lvi.SubItems.Add(oldss.ResData ?? "");
+ lvi.SubItems.Add("1");
+ this.listView1.Items.Add(lvi);
+ }
+ });
+ }
+ }
+ }
+
+ private void button9_Click(object sender, EventArgs e)
+ {
+ ListViewItem lvi = new ListViewItem("1");
+ lvi.SubItems.Add("1,1");
+ lvi.SubItems.Add("2,2");
+
}
}
}
\ No newline at end of file