예제 #1
0
 private void ReadRecordFromFile()
 {
     if (File.Exists("D:\\" + GetBanciDate() + GetBanci() + "\\" + GetBanciDate() + GetBanci() + ".csv"))
     {
         DataTable dt = new DataTable();
         dt.Columns.Add("日期", typeof(string));
         dt.Columns.Add("班次", typeof(string));
         dt.Columns.Add("机台号", typeof(string));
         dt.Columns.Add("机台穴号", typeof(string));
         dt.Columns.Add("产品barcode", typeof(string));
         DataTable dt1 = Csvfile.GetFromCsv("D:\\" + GetBanciDate() + GetBanci() + "\\" + GetBanciDate() + GetBanci() + ".csv", 1, dt);
         if (dt1.Rows.Count > 0)
         {
             foreach (DataRow item in dt1.Rows)
             {
                 RecordItem ri = new RecordItem();
                 ri.日期        = item["日期"].ToString();
                 ri.班次        = item["班次"].ToString();
                 ri.机台号       = item["机台号"].ToString();
                 ri.机台穴号      = item["机台穴号"].ToString();
                 ri.产品barcode = item["产品barcode"].ToString();
                 lock (LockObject)
                 {
                     recordItemList.Add(ri);
                 }
             }
         }
     }
     else
     {
         MsgText = AddMessage("本地记录不存在");
     }
 }
예제 #2
0
        public void PlcRun()
        {
            bool   first    = true;
            bool   scanFlag = false;
            Random rd       = new Random();

            while (true)
            {
                System.Threading.Thread.Sleep(10);
                if (Xinjie == null)
                {
                    System.Threading.Thread.Sleep(1000);
                }
                else
                {
                    try
                    {
                        PLCConnect = Xinjie.ReadM(24576);
                        if (PLCConnect)
                        {
                            PlcIn = Xinjie.ReadMultiMCoil(1500);
                            Xinjie.WritMultiMCoil(1800, PlcOut);
                            Xinjie.WriteW(150, rd.Next(0, 999).ToString());
                            dd8170     = Xinjie.ReadD(16554);
                            dd4208     = Xinjie.ReadD(4208);
                            RotalAngle = (dd8170 - dd4208) / 91776 * 360;

                            //扫码
                            if (scanFlag != PlcIn[0])
                            {
                                scanFlag = PlcIn[0];
                                if (scanFlag)
                                {
                                    Abarcode_f = false;
                                    Bbarcode_f = false;
                                    PlcOut[0]  = false;
                                    PlcOut[1]  = false;
                                    PlcOut[2]  = false;
                                    Xinjie.WritMultiMCoil(1800, PlcOut);
                                    ScanA.GetBarCode(ScanActionCallback1);
                                    ScanB.GetBarCode(ScanActionCallback2);
                                    TrigerTimes++;
                                    Inifile.INIWriteValue(ParameterIniPath, "Times", "TrigerTimes", TrigerTimes.ToString());
                                }
                                else
                                {
                                    PlcOut[0] = false;
                                }
                            }
                            if (Abarcode_f && Bbarcode_f)
                            {
                                Abarcode_f = false;
                                Bbarcode_f = false;
                                PlcOut[0]  = true;
                                PlcOut[1]  = Abarcode_s;
                                PlcOut[2]  = Bbarcode_s;
                                if (Abarcode_s && Bbarcode_s)
                                {
                                    ScanTimes++;
                                    Inifile.INIWriteValue(ParameterIniPath, "Times", "ScanTimes", ScanTimes.ToString());
                                }
                                RecordItem recordItem = new RecordItem();
                                recordItem.日期        = GetBanciDate();
                                recordItem.班次        = GetBanci();
                                recordItem.机台号       = JiTaiHao;
                                recordItem.产品barcode = Abarcode;
                                recordItem.机台穴号      = Bbarcode;
                                lock (LockObject)
                                {
                                    recordItemList.Add(recordItem);
                                }
                                if (Directory.Exists("D:\\" + GetBanciDate() + GetBanci()))
                                {
                                    string filename = "D:\\" + GetBanciDate() + GetBanci() + "\\" + GetBanciDate() + GetBanci() + ".csv";
                                    if (File.Exists(filename))
                                    {
                                        string[] heads = { "日期", "班次", "机台号", "机台穴号", "产品barcode" };
                                        Csvfile.AddNewLine(filename, heads);
                                    }
                                    string[] count = { recordItem.日期, recordItem.班次, recordItem.机台号, recordItem.机台穴号, recordItem.产品barcode };
                                    Csvfile.AddNewLine(filename, count);
                                }
                            }
                        }
                        else
                        {
                            if (first)
                            {
                                first = false;
                            }
                            else
                            {
                                MsgText = AddMessage("PLC断线,重新连接...");
                            }
                            Xinjie.ModbusDisConnect();
                            System.Threading.Thread.Sleep(1000);
                            Xinjie.ModbusInit(PLCPortCom, 19200, System.IO.Ports.Parity.Even, 8, System.IO.Ports.StopBits.One);
                            Xinjie.ModbusConnect();
                        }
                    }
                    catch (Exception ex)
                    {
                        MsgText    = AddMessage(ex.Message);
                        PLCConnect = false;
                    }
                }
            }
        }