Browse Source

提交代码

yi eason 3 years ago
parent
commit
fa0bc2cf9e

+ 9 - 1
CarLocalMeter/App.config

@@ -5,7 +5,7 @@
     </startup>
 	<appSettings>
 		<!--ping服务器的地址-->
-		<add key="ServiceIp" value="jgwzjl.jiugang.com"/>
+		<add key="ServiceIp" value="jgwzjl.jiugang.comx"/>
 		
 		<!--<add key="ServiceUrl" value="http://10.104.4.130:9004/v1" />-->
 		<add key="ServiceUrl" value="http://10.99.81.20/icore.icp.web/pass/systemBase/v1"/>
@@ -73,6 +73,7 @@
 		<add key="dzValue" value="1"/>
 		
 		<!--*************MoXa配置 10.130.0.1是1#南 10.130.0.69是1#北  10.130.1.173是8#西*****************-->
+		<add key="getWgtType" value="0"/><!--0使用串口获取,1使用moxa获取-->
 		<add key="sleepTime" value="100"/><!--100毫秒取一次重量-->
 		<add key="moxaIP" value="10.130.0.1" />
 		<add key="moxaPort" value="4001" /><!--4001是1#南/北,4004是8#西-->
@@ -91,6 +92,13 @@
 		<add key="undulateValue" value="50" />
 		<!--零点判断绝对值在0到多少内为零点-->
 		<add key="minValue" value="50" />
+
+		<!--走串口采集重量的情况-->
+		<add key="comPort" value="COM11" /><!--COM口-->
+		<add key="comBaud" value="28800" /><!--波特率-->
+		<add key="comParity" value="None" /><!--奇偶校验方式-->
+		<add key="comLength" value="8" /><!--数据长度-->
+		<add key="comStopBits" value="1" /><!--停止位-->
 		
 		<!--led地址 10.130.0.68-->
 		<add key="ledIp" value="10.130.0.68"/>

+ 1 - 0
CarLocalMeter/CarLocalMeter.csproj

@@ -113,6 +113,7 @@
     <Compile Include="ModelCls\DjPbModel.cs" />
     <Compile Include="ModelCls\MeterBaseRfidInfo.cs" />
     <Compile Include="ModelCls\MeterWorkPreLink.cs" />
+    <Compile Include="OptionCls\ComCls.cs" />
     <Compile Include="OptionCls\DbUpload.cs" />
     <Compile Include="OptionCls\Led\LedDll.cs" />
     <Compile Include="OptionCls\Led\LED_Control.cs" />

+ 317 - 0
CarLocalMeter/OptionCls/ComCls.cs

@@ -0,0 +1,317 @@
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Diagnostics;
+using System.IO.Ports;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace CarLocalMeter
+{
+    public class ComCls
+    {
+        Log lg = Log.GetInstance();
+        bool blThreadFlag;//数据采集线程开关
+        Thread DataCollectThread = null;//采集进程
+        SerialPort serialPort1;
+        private object obj = new object();
+        string msg = "";
+
+        public ComCls()
+        {
+            //新数据采集
+            blThreadFlag = true;
+            DataCollectThread = new Thread(new ThreadStart(DataCollect));
+        }
+
+        public void start()
+        {
+            #region 串口采集配置
+            Parity parity = Parity.None;
+
+            if (AppConfigCache.comParity.ToUpper() == "ODD")
+            {
+                parity = Parity.Odd;
+            }
+            if (AppConfigCache.comParity.ToUpper() == "EVEN")
+            {
+                parity = Parity.Even;
+            }
+
+            StopBits stopBits = StopBits.None;
+            if (AppConfigCache.comStopBits == "1")
+            {
+                stopBits = StopBits.One;
+            }
+            if (AppConfigCache.comStopBits == "1.5")
+            {
+                stopBits = StopBits.OnePointFive;
+            }
+            if (AppConfigCache.comStopBits == "2")
+            {
+                stopBits = StopBits.Two;
+            }
+            serialPort1 = new SerialPort(AppConfigCache.comPort, AppConfigCache.comBaud, parity, AppConfigCache.comLength, stopBits);
+            int icnt = 0;
+            msg = "";
+            while (!serialPort1.IsOpen)
+            {
+                try
+                {
+                    icnt++;
+                    serialPort1.Open();
+                }
+                catch (Exception err)
+                {
+                    lg.WriteLog(LogType.MoxaLog, $"打开串口失败,正重试{err.Message}");
+                }
+                System.Threading.Thread.Sleep(500);
+                if (icnt > 5)
+                {
+                    msg = "串口打开失败,请检查串口是否存在,且是否被占用";
+                    return;
+                }
+            }
+
+            #endregion
+
+            DataCollectThread.Start();
+        }
+
+        public void ClosingCollect()
+        {
+            blThreadFlag = false;
+            if (serialPort1?.IsOpen == true) serialPort1?.Close();
+        }
+
+        #region 数据采集
+        private List<int> lWeight = new List<int>();
+
+        /// <summary>
+        /// 数据采集线程
+        /// </summary>
+        private void DataCollect()
+        {
+            if (!blThreadFlag) return;
+            
+            byte[] btDbs = new byte[AppConfigCache.messageLength];//数据填充bt
+            byte[] buffer = new byte[AppConfigCache.messageLength];
+            int isLd = 0;
+            while (blThreadFlag)
+            {
+                try
+                {
+                    lock (obj) 
+                    {
+                        byte[] buffers = new byte[AppConfigCache.messageLength];
+
+                        //Debug.WriteLine("client开始:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
+                        //int n = client.Receive(buffers); //由于仪表每1秒是采集多少次是不确定的,如果这个buffer的长度设置的太长会导致这里读取的时间很长
+                        //Debug.WriteLine("client结束:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
+
+                        Thread.Sleep(50);
+                        int n = serialPort1.BytesToRead;
+                        if (n > 0)
+                        {
+                            serialPort1.Read(buffers, 0, buffers.Length);
+                        }
+
+                        int icn = 0, iGet = 0;
+                        for (int i = 0; i < buffer.Length; i++)
+                        {
+                            if (buffer[i] != 0)
+                            {
+                                icn++;
+                            }
+                            else
+                            {
+                                break;
+                            }
+                        }
+                        if (icn != buffer.Length) //没有12个长度则继续接收直到满12个长度
+                        {
+                            for (int i = 0; i < n; i++)
+                            {
+                                if (icn < buffer.Length)
+                                {
+                                    buffer[icn++] = buffers[i];
+                                    iGet++;
+                                }
+                                else break;
+                            }
+                        }
+                        if (icn == buffer.Length)
+                        {
+                            if (AppConfigCache.isFz == "true")
+                            {
+                                byte[] bf = buffer.Reverse().ToArray();
+                                int mx = 0;
+                                foreach (byte b in bf)
+                                {
+                                    if (b > 0) buffer[mx++] = b;
+                                }
+                            }
+
+
+                            int iLastNew = 0;
+                            byte[] btNew = new byte[btDbs.Length + buffer.Length];
+                            for (int i = 0; i < btDbs.Length; i++)
+                            {
+                                if (btDbs[i] != 0)
+                                {
+                                    btNew[iLastNew++] = btDbs[i];
+                                }
+                            }
+
+                            int bufferLast = 0;
+                            for (int i = 0; i < buffer.Length; i++)
+                            {
+                                if (buffer[i] == AppConfigCache.separate)
+                                {
+                                    bufferLast = i;
+                                    break;
+                                }
+                                else
+                                {
+                                    btNew[iLastNew++] = buffer[i];
+                                }
+                            }
+                            for (int i = 0; i < btDbs.Length; i++)
+                            {
+                                btDbs[i] = 0;
+                            }
+
+                            for (int i = bufferLast; i < buffer.Length; i++)
+                            {
+                                btDbs[i - bufferLast] = buffer[i];
+                            }
+
+
+                            byte[] bt = new byte[AppConfigCache.messageLength];
+
+                            if (iLastNew == AppConfigCache.messageLength)
+                            {
+                                for (int i = 0; i < iLastNew; i++)
+                                {
+                                    bt[i] = btNew[i];
+                                }
+                            }
+
+
+                            if (iLastNew != AppConfigCache.messageLength) continue;
+
+                            #region 在这里要数一下buffer里面的数据,看下结束符的byte值是多少,每个秤都确定好之后,就删除这个即可
+                            //*
+                            int m = 0;
+                            string strX = "[";
+                            for (int i = 0; i < bt.Length; i++)
+                            {
+                                if (bt[i] > 0)
+                                {
+                                    m++;
+                                    strX += $"{bt[i]} ";
+                                }
+                            }
+                            strX += "]";
+                            string strx = Encoding.UTF8.GetString(bt, 0, bt.Length);
+                            //Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + strX);
+                            Debug.WriteLine(strx);
+                            //*/
+                            #endregion
+
+
+                            byte[] btDb = new byte[AppConfigCache.dataLength];
+                            int iC = 0;
+                            for (int i = AppConfigCache.startPosition; i < (AppConfigCache.startPosition + AppConfigCache.dataLength); i++)
+                            {
+                                btDb[iC++] = bt[i - 1];
+                            }
+
+                            int weight = 0;
+
+                            string str = Encoding.UTF8.GetString(btDb, 0, btDb.Length);
+                            bool flag = int.TryParse(str, out weight);
+
+
+                            CacleCls.weight = weight;
+                            CacleCls.weightFlag = "0";
+                            CacleCls.weightMsgInfo = "";
+
+                            if (CacleCls.weight <= AppConfigCache.undulateValue)
+                            {
+                                CacleCls.isWd = 2;
+                                lWeight.Clear();
+                            }
+                            else
+                            {
+                                if (lWeight.Count == 0)
+                                {
+                                    lWeight.Add(weight);
+                                    CacleCls.isWd = 1;
+                                }
+                                else if (Math.Abs(weight - lWeight[0]) > AppConfigCache.undulateValue)
+                                {
+                                    lWeight.Clear();
+                                    lWeight.Add(weight);
+                                    CacleCls.isWd = 1;
+                                }
+                                else if (lWeight.Count < AppConfigCache.undulateCount)
+                                {
+                                    lWeight.Add(weight);
+                                }
+                                else
+                                {
+                                    CacleCls.isWd = 0;
+                                }
+                            }
+
+                            if (Math.Abs(weight) > 0)
+                            {
+                                if (Math.Abs(weight) <= AppConfigCache.minValue)
+                                {
+                                    if (isLd < 4) isLd++; //连续4次采集到不为0,且满足零点最小绝对值
+                                    if (isLd == 4) CacleCls.isZeroState = true;
+                                }
+                            }
+                            else
+                            {
+                                CacleCls.isZeroState = false;
+                                isLd = 0;
+                            }
+
+
+                            for (int i = 0; i < buffer.Length; i++)
+                            {
+                                buffer[i] = 0;
+                            }
+
+                            if (iGet < n)
+                            {
+                                for (int i = 0; i < n - iGet; i++)
+                                {
+                                    buffer[i] = buffers[iGet + i];
+                                }
+                            }
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    lg.WriteLog(LogType.MoxaLog, $"串口采集异常:{ex.Message}");
+                    CacleCls.isWd = CacleCls.isWd == 0 ? 1 : CacleCls.isWd;
+                    CacleCls.weightFlag = "1";
+                    CacleCls.weightMsgInfo = $"串口采集异常:{ex.Message}";
+                }
+                finally
+                {
+                    //Thread.Sleep(100);
+                }
+            }
+        }
+        #endregion
+    }
+}

+ 31 - 1
CarLocalMeter/PbCls/AppConfigCache.cs

@@ -198,7 +198,37 @@ namespace CarLocalMeter
         /// 图片过期时间
         /// </summary>
         public static int imgTimeOut = Convert.ToInt32(ConfigurationManager.AppSettings["imgTimeOut"].ToString());
-        
+
+        /// <summary>
+        /// 0使用串口获取重量,1使用moxa获取重量
+        /// </summary>
+        public static string getWgtType = ConfigurationManager.AppSettings["getWgtType"].ToString();
+
+        /// <summary>
+        /// COM口
+        /// </summary>
+        public static string comPort = ConfigurationManager.AppSettings["comPort"].ToString();
+
+        /// <summary>
+        /// 波特率
+        /// </summary>
+        public static int comBaud = Convert.ToInt32(ConfigurationManager.AppSettings["comBaud"].ToString());
+
+        /// <summary>
+        /// 奇偶校验方式
+        /// </summary>
+        public static string comParity = ConfigurationManager.AppSettings["comParity"].ToString();
+
+        /// <summary>
+        /// 数据长度
+        /// </summary>
+        public static int comLength = Convert.ToInt32(ConfigurationManager.AppSettings["comLength"].ToString());
+
+        /// <summary>
+        /// 结束符
+        /// </summary>
+        public static string comStopBits = ConfigurationManager.AppSettings["comStopBits"].ToString();
+
 
         /// <summary>
         /// 

+ 7 - 1
CarLocalMeter/PbCls/FileOption.cs

@@ -22,11 +22,16 @@ namespace CarLocalMeter
             Log lg = Log.GetInstance();
             try
             {
+                List<string> ls = new List<string>();
+                ls.Add(JsonConvert.SerializeObject(actualDb));
+                File.AppendAllLines(dbFile, ls);
+
+                /*
                 //判断文件是否存在
                 if (!File.Exists(dbFile))
                 {
                     //不存在则创建文件并直接写入数据
-                    File.WriteAllText(dbFile, JsonConvert.SerializeObject(actualDb));
+                    File.WriteAllText(dbFile, JsonConvert.SerializeObject(actualDb) + "\n");
                 }
                 else
                 {
@@ -34,6 +39,7 @@ namespace CarLocalMeter
                     ls.Add(JsonConvert.SerializeObject(actualDb));
                     File.AppendAllLines(dbFile, ls);
                 }
+                //*/
                 msgInfo = "计量完成";
                 return true;
             }

+ 7 - 7
CarLocalMeter/frmMain.Designer.cs

@@ -851,7 +851,7 @@ namespace CarLocalMeter
             this.tableLayoutPanel7.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 90F));
             this.tableLayoutPanel7.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 222F));
             this.tableLayoutPanel7.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
-            this.tableLayoutPanel7.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 79F));
+            this.tableLayoutPanel7.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 82F));
             this.tableLayoutPanel7.Controls.Add(this.label1, 5, 0);
             this.tableLayoutPanel7.Controls.Add(this.txtCarNo, 4, 0);
             this.tableLayoutPanel7.Controls.Add(this.cbCarNo, 3, 0);
@@ -875,7 +875,7 @@ namespace CarLocalMeter
             this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
             this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
             this.label1.ForeColor = System.Drawing.Color.Blue;
-            this.label1.Location = new System.Drawing.Point(726, 1);
+            this.label1.Location = new System.Drawing.Point(723, 1);
             this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.label1.Name = "label1";
             this.label1.Size = new System.Drawing.Size(42, 36);
@@ -887,7 +887,7 @@ namespace CarLocalMeter
             // 
             this.txtCarNo.Dock = System.Windows.Forms.DockStyle.Fill;
             this.txtCarNo.Font = new System.Drawing.Font("宋体", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.txtCarNo.Location = new System.Drawing.Point(502, 3);
+            this.txtCarNo.Location = new System.Drawing.Point(499, 3);
             this.txtCarNo.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
             this.txtCarNo.Name = "txtCarNo";
             this.txtCarNo.Size = new System.Drawing.Size(216, 32);
@@ -935,7 +935,7 @@ namespace CarLocalMeter
             "琼",
             "京",
             "津"});
-            this.cbCarNo.Location = new System.Drawing.Point(411, 3);
+            this.cbCarNo.Location = new System.Drawing.Point(408, 3);
             this.cbCarNo.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
             this.cbCarNo.Name = "cbCarNo";
             this.cbCarNo.Size = new System.Drawing.Size(84, 31);
@@ -952,7 +952,7 @@ namespace CarLocalMeter
             this.lbPointName.Location = new System.Drawing.Point(86, 1);
             this.lbPointName.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.lbPointName.Name = "lbPointName";
-            this.lbPointName.Size = new System.Drawing.Size(256, 36);
+            this.lbPointName.Size = new System.Drawing.Size(253, 36);
             this.lbPointName.TabIndex = 3;
             this.lbPointName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
             // 
@@ -978,7 +978,7 @@ namespace CarLocalMeter
             this.label2.Dock = System.Windows.Forms.DockStyle.Fill;
             this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
             this.label2.ForeColor = System.Drawing.Color.Blue;
-            this.label2.Location = new System.Drawing.Point(351, 1);
+            this.label2.Location = new System.Drawing.Point(348, 1);
             this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.label2.Name = "label2";
             this.label2.Size = new System.Drawing.Size(52, 36);
@@ -989,7 +989,7 @@ namespace CarLocalMeter
             // pbLineOn
             // 
             this.pbLineOn.Image = ((System.Drawing.Image)(resources.GetObject("pbLineOn.Image")));
-            this.pbLineOn.Location = new System.Drawing.Point(776, 4);
+            this.pbLineOn.Location = new System.Drawing.Point(773, 4);
             this.pbLineOn.Name = "pbLineOn";
             this.pbLineOn.Size = new System.Drawing.Size(39, 30);
             this.pbLineOn.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

+ 16 - 2
CarLocalMeter/frmMain.cs

@@ -60,6 +60,7 @@ namespace CarLocalMeter
         /// 重量采集类
         /// </summary>
         MoxaCls mx = new MoxaCls();
+        ComCls cx = new ComCls();
 
         /// <summary>
         /// 语音播放
@@ -164,7 +165,13 @@ namespace CarLocalMeter
             //*
             ping.start();
             upload.start();
-            mx.start();
+
+            if(AppConfigCache.getWgtType=="0")
+                cx.start();
+            else
+                mx.start();
+
+
             rfid.Start();
             imgControl.Start();
             carCls.Login(AppConfigCache.voiceCarNoIp, AppConfigCache.voiceCarNoPort, AppConfigCache.voiceCarNoUid, AppConfigCache.voiceCarNoPwd);
@@ -849,7 +856,14 @@ namespace CarLocalMeter
         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
         {
             stop();
-            try { mx?.ClosingCollect(); } catch { }
+            try {
+                if (AppConfigCache.getWgtType == "0")
+                    cx?.ClosingCollect(); 
+                else
+                    mx?.ClosingCollect();
+            } catch { }
+
+
             try { rfid?.ClosingCollect(); } catch { }
             try { if (bVoice) ce?.StopTalk(); } catch { }