Exemplo n.º 1
0
        //打开盘点激光
        public static void OnOffLaser(PLC_Tcp_AP.LaserType type, int value)
        {
            while (spIsBusy)
            {
                Thread.Sleep(100);
            }
            spIsBusy = true;
            SerialPort spLaser = spLaserLeft;

            if (type == PLC_Tcp_AP.LaserType.Right)
            {
                spLaser = spLaserRight;
            }

            string send = "24" + "00" + "01" + "00" + "2000" + "0000" + "00000000" +
                          "0A" + "09" + "0000" + value.ToString().PadLeft(4, '0') + "0000" + "00000000" + "00000000";

            send += GetFCS(send) + "2E3B";

            string response = "";

            try
            {
                spLaser.DiscardInBuffer();
                spLaser.DiscardOutBuffer();
                byte[] buffer_send = PLC_Tcp_AP.GetBytes(send);
                spLaser.Write(buffer_send, 0, buffer_send.Length);

                DateTime begin = DateTime.Now;
                int      m = 0, n = 0;
                do
                {
                    Thread.Sleep(50);
                    m = spLaser.BytesToRead;
                    Thread.Sleep(50);
                    n = spLaser.BytesToRead;
                }while ((m == 0 || m < n) && DateTime.Now < begin.AddSeconds(1));
                byte[] buffer_response = new byte[m];
                spLaser.Read(buffer_response, 0, m);
                spIsBusy = false;
                response = PLC_Tcp_AP.GetString(buffer_response);
                //if (response.Length >= 79)
                //{
                //    string s = response.Substring(72, 8);
                //}
            }
            catch (Exception ex)
            {
                spIsBusy = false;
                csLOG.WriteLog(ex.Message);
                if (ThrowMsg == null)
                {
                    csMsg.ShowWarning(ex.Message, false);
                }
                else
                {
                    ThrowMsg(ex.Message);
                }
            }
        }
Exemplo n.º 2
0
        //读取盘点激光数据
        public static int ReadLaserData(PLC_Tcp_AP.LaserType type)
        {
            int result = -1;

            while (spIsBusy)
            {
                Thread.Sleep(100);
            }
            spIsBusy = true;
            SerialPort spLaser = spLaserLeft;

            if (type == PLC_Tcp_AP.LaserType.Right)
            {
                spLaser = spLaserRight;
            }

            if (spLaser != null)
            {
                string send = "24" + "00" + "01" + "00" + "2000" + "0000" + "00000000" +
                              "0A" + "00" + "0000" + "0000" + "0000" + "00000000" + "00000000";
                send += GetFCS(send) + "2E3B";

                string response = "";
                try
                {
                    spLaser.DiscardInBuffer();
                    spLaser.DiscardOutBuffer();
                    byte[] buffer_send = PLC_Tcp_AP.GetBytes(send);
                    spLaser.Write(buffer_send, 0, buffer_send.Length);

                    DateTime begin = DateTime.Now;
                    int      m = 0, n = 0;

                    Thread.Sleep(200);
                    do
                    {
                        Thread.Sleep(50);
                        m = spLaser.BytesToRead;
                        Thread.Sleep(50);
                        n = spLaser.BytesToRead;
                    }while ((m == 0 || m < n) && DateTime.Now < begin.AddSeconds(2));
                    byte[] buffer_response = new byte[m];
                    spLaser.Read(buffer_response, 0, m);
                    spIsBusy = false;
                    response = PLC_Tcp_AP.GetString(buffer_response);
                    if (response.Length >= 79)
                    {
                        string s     = response.Substring(72, 8);
                        string value = s.Substring(6, 2) + s.Substring(4, 2) + s.Substring(2, 2) + s.Substring(0, 2);
                        result = Convert.ToInt32(value, 16);
                    }
                }
                catch (Exception ex)
                {
                    spIsBusy = false;
                    csLOG.WriteLog(ex.Message);
                    if (ThrowMsg == null)
                    {
                        csMsg.ShowWarning(ex.Message, false);
                    }
                    else
                    {
                        ThrowMsg(ex.Message);
                    }
                }
            }
            else if (ThrowMsg != null)
            {
                ThrowMsg("激光串口不存在");
            }
            return(result);
        }