@@ -35,7 +35,67 @@ namespace HKCardOUT.ViewModels | |||
public RootViewModel(IContainer Container) | |||
{ | |||
this.Container = Container; | |||
MainThread(); | |||
this.Activated += RootViewModel_Activated; | |||
this.Closed += RootViewModel_Closed; | |||
//MainThread(); | |||
} | |||
/// <summary> | |||
/// 关闭页面 | |||
/// </summary> | |||
/// <param name="sender"></param> | |||
/// <param name="e"></param> | |||
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 | |||
@@ -81,7 +81,7 @@ | |||
HeaderStyle="{StaticResource SaleLogHeader}" /> | |||
<DataGridTextColumn | |||
Width="300" | |||
Binding="{Binding CreateTime, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}}" | |||
Binding="{Binding CreateTime, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}, UpdateSourceTrigger=PropertyChanged}" | |||
ElementStyle="{StaticResource CenterAlignmentStyle}" | |||
Header="消费时间" | |||
HeaderStyle="{StaticResource SaleLogHeader}" /> | |||
@@ -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(); | |||
} | |||
/// <summary> | |||
/// 发送数据 | |||
/// </summary> | |||
@@ -180,7 +186,7 @@ namespace UHFHelper | |||
} | |||
catch (TaskCanceledException) | |||
{ | |||
throw new TimeoutException(); | |||
return null; | |||
} | |||
return await sendTask; | |||
} | |||
@@ -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; | |||
} | |||
} |
@@ -48,7 +48,7 @@ namespace test | |||
var res = UHFCardHelper.GetInstance().WriteCard(textBox1.Text); | |||
MessageBox.Show(res.ResMes); | |||
} | |||
List<int> add = new List<int>(); | |||
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行"); | |||
} | |||
} | |||
} |