优化读取稳定磅重

master
ZR20090193-陈敬勇 2 years ago
parent 5f4d983919
commit 3054b39721

3
.gitignore vendored

@ -0,0 +1,3 @@
.idea/
.vs/
obj/

@ -6,6 +6,7 @@
<!--//预定义配置节--> <!--//预定义配置节-->
<appSettings> <appSettings>
<add key="RedisStr" value="192.168.1.11:6379,defaultDatabase=1,poolsize=50,ssl=false,writeBuffer=10240"/> <add key="RedisStr" value="192.168.1.11:6379,defaultDatabase=1,poolsize=50,ssl=false,writeBuffer=10240"/>
<!--<add key="RedisStr" value="127.0.0.1:6379,defaultDatabase=1,poolsize=50,ssl=false,writeBuffer=10240"/>-->
<add key="RedisKey" value="EnterWeight"/> <add key="RedisKey" value="EnterWeight"/>
<add key="pIP" value="192.168.1.98"/> <add key="pIP" value="192.168.1.98"/>
<add key="pIPUser" value="1"/> <add key="pIPUser" value="1"/>

25
Form1.Designer.cs generated

@ -80,6 +80,7 @@
this.radioButtonReceiveDataHEX = new System.Windows.Forms.RadioButton(); this.radioButtonReceiveDataHEX = new System.Windows.Forms.RadioButton();
this.radioButtonReceiveDataASCII = new System.Windows.Forms.RadioButton(); this.radioButtonReceiveDataASCII = new System.Windows.Forms.RadioButton();
this.groupBoxReceiveData = new System.Windows.Forms.GroupBox(); this.groupBoxReceiveData = new System.Windows.Forms.GroupBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.buttonClearRecData = new System.Windows.Forms.Button(); this.buttonClearRecData = new System.Windows.Forms.Button();
this.textBoxReceive = new System.Windows.Forms.TextBox(); this.textBoxReceive = new System.Windows.Forms.TextBox();
this.groupBoxSendData = new System.Windows.Forms.GroupBox(); this.groupBoxSendData = new System.Windows.Forms.GroupBox();
@ -89,7 +90,7 @@
this.button1 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox(); this.button4 = new System.Windows.Forms.Button();
this.menuStrip1.SuspendLayout(); this.menuStrip1.SuspendLayout();
this.groupBoxSerialPortSetting.SuspendLayout(); this.groupBoxSerialPortSetting.SuspendLayout();
this.groupBoxSendSetting.SuspendLayout(); this.groupBoxSendSetting.SuspendLayout();
@ -405,6 +406,13 @@
this.groupBoxReceiveData.Text = "接收数据"; this.groupBoxReceiveData.Text = "接收数据";
this.groupBoxReceiveData.Enter += new System.EventHandler(this.groupBoxReceiveData_Enter); this.groupBoxReceiveData.Enter += new System.EventHandler(this.groupBoxReceiveData_Enter);
// //
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(3, 219);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 27);
this.textBox1.TabIndex = 2;
//
// buttonClearRecData // buttonClearRecData
// //
this.buttonClearRecData.Location = new System.Drawing.Point(195, 219); this.buttonClearRecData.Location = new System.Drawing.Point(195, 219);
@ -430,6 +438,7 @@
this.groupBoxSendData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) this.groupBoxSendData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.groupBoxSendData.Controls.Add(this.button4);
this.groupBoxSendData.Controls.Add(this.buttonSendData); this.groupBoxSendData.Controls.Add(this.buttonSendData);
this.groupBoxSendData.Controls.Add(this.textBoxSend); this.groupBoxSendData.Controls.Add(this.textBoxSend);
this.groupBoxSendData.Location = new System.Drawing.Point(254, 384); this.groupBoxSendData.Location = new System.Drawing.Point(254, 384);
@ -498,12 +507,15 @@
this.button3.UseVisualStyleBackColor = true; this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click); this.button3.Click += new System.EventHandler(this.button3_Click);
// //
// textBox1 // button4
// //
this.textBox1.Location = new System.Drawing.Point(3, 219); this.button4.Location = new System.Drawing.Point(20, 71);
this.textBox1.Name = "textBox1"; this.button4.Name = "button4";
this.textBox1.Size = new System.Drawing.Size(192, 27); this.button4.Size = new System.Drawing.Size(75, 23);
this.textBox1.TabIndex = 2; this.button4.TabIndex = 14;
this.button4.Text = "测试";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
// //
// Form1 // Form1
// //
@ -591,6 +603,7 @@
private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button4;
} }
} }

@ -18,6 +18,7 @@ using PlatformScalePortReadData.Model;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Runtime.InteropServices.ComTypes;
namespace PlatformScalePortReadData namespace PlatformScalePortReadData
{ {
@ -195,6 +196,9 @@ namespace PlatformScalePortReadData
//{ //{
// saveDataFS = File.Create(saveDataFile); // saveDataFS = File.Create(saveDataFile);
//} //}
saveDataFile = StringUtil.GetShortNo() + ".txt";
saveDataFS = File.Create(saveDataFile);
saveDataFS.Close();
//打开串口 //打开串口
serialPort.Open(); serialPort.Open();
@ -299,23 +303,17 @@ namespace PlatformScalePortReadData
do do
{ {
//获取磅秤数据 取40到最后一位 //获取磅秤数据 取40到最后一位
var list = redis.LRange(key, 40, -1); var all = redis.LRange(key, 0, -1);
if (list.First() == list.Last()) var list = all.TakeLast(10);
if (list.GroupBy(x => x).All(g => g.Count() == 10))
{ {
weight = Convert.ToDecimal(list.Last()); weight = Convert.ToDecimal(list.Last());
a = false; a = false;
} }
} }
while (a == true); while (a == true);
textBoxReceive.AppendText("获取磅秤数据:" + weight); textBoxReceive.AppendText("获取磅秤数据:" + weight);
//var record = new TurnOverRecord();
//record.BILLTYPE = false;
//record.TRUCKNO = recRes1.chLicense;
//record.BoxWeigth = weight;
//record.BoxCode = "MAGU5445689";
//textBoxReceive.AppendText("推送数据:车牌 " + record.TRUCKNO + " 箱号:" + record.BoxCode + " 磅秤:" + record.BoxWeigth);
//YARDService.PublicFunc(record);
//获取箱号信息 //获取箱号信息
var CTNNO = CONLOGService.GetCTNNO(1); var CTNNO = CONLOGService.GetCTNNO(1);
@ -379,10 +377,10 @@ namespace PlatformScalePortReadData
do do
{ {
//获取磅秤数据 取40到最后一位 //获取磅秤数据 取40到最后一位
var list = redis.LRange(key, 40, -1); var all = redis.LRange(key, 0, -1);
if (list.First() == list.Last()) var list = all.TakeLast(10);
if (list.GroupBy(x => x).All(g => g.Count() == 10))
{ {
weight = Convert.ToDecimal(list.Last()); weight = Convert.ToDecimal(list.Last());
a = false; a = false;
} }
@ -443,6 +441,7 @@ namespace PlatformScalePortReadData
#endregion #endregion
private class MeterNum private class MeterNum
{ {
private static List<decimal> meternoArray = new List<decimal>(); private static List<decimal> meternoArray = new List<decimal>();
@ -531,15 +530,6 @@ namespace PlatformScalePortReadData
//写入Redis List 超过50移除 //写入Redis List 超过50移除
decimal num = Convert.ToDecimal(tempArr[1].Trim()); decimal num = Convert.ToDecimal(tempArr[1].Trim());
if (num > 600) if (num > 600)
{
//redis = CsRedisHelper.GetInstance();
//redis.RPush(key, num);
//if (redis.LLen(key) > 50)
//{
// redis.LPop(key);
//}
if (MeterNum.newMeterNum(num))
{ {
redis = CsRedisHelper.GetInstance(); redis = CsRedisHelper.GetInstance();
redis.RPush(key, num); redis.RPush(key, num);
@ -547,6 +537,21 @@ namespace PlatformScalePortReadData
{ {
redis.LPop(key); redis.LPop(key);
} }
//if (saveDataFile != null)
//{
// //写入临时文件
// File.AppendAllText(saveDataFile, num + "\r\n");
//}
if (MeterNum.newMeterNum(num))
{
textBoxReceive.Text += string.Format("{0}\r\n", dateTimeNow);
textBoxReceive.AppendText("最近20次一致磅重: " + num + "\r\n");
//redis = CsRedisHelper.GetInstance();
//redis.RPush(key, num);
//if (redis.LLen(key) > 50)
//{
// redis.LPop(key);
//}
} }
} }
#endregion #endregion
@ -605,7 +610,7 @@ namespace PlatformScalePortReadData
} }
catch (System.Exception ex) catch (System.Exception ex)
{ {
//textBoxReceive.AppendText(ex.Message); textBoxReceive.AppendText(ex.Message);
return; return;
} }
@ -808,5 +813,16 @@ namespace PlatformScalePortReadData
var CTNNO = CONLOGService.GetCTNNO(2); var CTNNO = CONLOGService.GetCTNNO(2);
textBoxReceive.AppendText("获取箱号成功!箱号1:" + CTNNO.ConID1 + " 箱号2" + CTNNO.ConID2 + " 识别时间:" + CTNNO.EntryTime); textBoxReceive.AppendText("获取箱号成功!箱号1:" + CTNNO.ConID1 + " 箱号2" + CTNNO.ConID2 + " 识别时间:" + CTNNO.EntryTime);
} }
private void button4_Click(object sender, EventArgs e)
{
//获取配置
key = ConfigurationManager.AppSettings["RedisKey"];
redis = CsRedisHelper.GetInstance();
var list = redis.LRange(key, 0, -1);
var temp = list.TakeLast(10);
var res= temp.GroupBy(x => x).All(g => g.Count() == 10);
MessageBox.Show("测试结果:" + res + " 最后一位:" + temp.Last());
}
} }
} }

@ -24,5 +24,27 @@ namespace PlatformScalePortReadData
return -1; return -1;
} }
public static string GetShortNo()
{
string No = ""; //生成编号
DateTime now = DateTime.Now;
string Year = now.Year.ToString().Substring(2, 2);
string Month = now.Month < 10 ? "0" + now.Month : now.Month.ToString();
string Day = now.Day < 10 ? "0" + now.Day : now.Day.ToString();
string Hour = now.Hour < 10 ? "0" + now.Hour : now.Hour.ToString();
string Minute = now.Minute < 10 ? "0" + now.Minute : now.Minute.ToString();
string Second = now.Second < 10 ? "0" + now.Second : now.Second.ToString();
string Millisecond = now.Millisecond < 10 ? "0" + now.Millisecond : now.Millisecond.ToString();
//Random rd = new Random();
//int a = rd.Next(0, 9);
//int b = rd.Next(0, 9);
//int c = rd.Next(0, 9); + a + b + c
No = Year + Month + Day + Hour + Minute ;
return No;
}
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> coll, int N)
{
return coll.Reverse().Take(N).Reverse();
}
} }
} }

Loading…
Cancel
Save