Code

Nodess
mail@pastecode.io avatar
unknown
plain_text
a month ago
4.4 kB
1
Indexable
Never
using LModbus;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WindowsFormsApp1
{
    public class ControlPLC_LX5S
    {
        private static ControlPLC_LX5S _instance;
        public EventHandler PLCTriggerEvent;
        public bool IsInspecting;


        private const int bitVisionReady = 80;
        private const int bitPLCTrigger = 81;
        private const int bitInspectionEnd = 82;
        private const int bitResultOK = 83;
        private const int bitResultNG = 84;
        private const int bitComfirmResult = 85;

        static object _lockObj = new object();
        public static ControlPLC_LX5S Instance
        {
            get
            {
                lock (_lockObj)
                {
                    if (_instance == null)
                    {
                        _instance = new ControlPLC_LX5S();
                    }
                }
                return _instance;
            }
        }

        private ModbusTCPIPMASTER plcPanasonic_LX5S;
        private string PLCHostIP = "192.168.0.11";
        private bool IsConnectPLC = false;
        private bool IsWritingDataToPLC = false;
        private int PLCPort = 502;
        private Thread readDataRegister;
        public void Init()
        {
            plcPanasonic_LX5S = new ModbusTCPIPMASTER()
            {
                ID = 1,
                Mode_TCP_Serial = false
            };
            readDataRegister = new Thread(new ThreadStart(this.ThreadTask));
            readDataRegister.IsBackground = true;
            readDataRegister.Name = "readDataRegister";
            readDataRegister.Start();
        }

        private void ThreadTask()
        {
            while (true)
            {
                if (plcPanasonic_LX5S != null)
                {
                    if (!plcPanasonic_LX5S.ConnectTCP(PLCHostIP, PLCPort))
                    {
                        IsConnectPLC = false;
                        Thread.Sleep(1000);
                    }
                    else
                    {
                        IsConnectPLC = true;
                        WriteDataPLC(80);
                        ReadDataPLC();
                        Thread.Sleep(20);
                    }
                }
            }
        }

        private void ReadDataPLC()
        {
            try
            {
                //neu dang ghi data xuong thanh ghi thi không đọc giá trị. đợi ghi xong
                if (IsWritingDataToPLC) return;
                //doc tu thanh ghi 4096... 200 thanh tiep theo
                bool[] arr = plcPanasonic_LX5S.ReadCoilsTCPIP(4096, 200);
                // plc alive thanh ghi 67
                //plc trigger thanh ghi 1

                if (arr[bitPLCTrigger] && !IsInspecting)
                {
                    //khi xu ly xong, phai clear gia tri IsInspecting
                    IsInspecting = true;
                    if (PLCTriggerEvent != null) PLCTriggerEvent(this, EventArgs.Empty);
                }

                if (arr[bitComfirmResult])
                {
                    ConfirmResult();
                }
            }
            catch (Exception)
            {
            }
        }

        private void ConfirmResult()
        {
            WriteDataPLCOFF(bitInspectionEnd);
            WriteDataPLCOFF(bitResultOK);
            WriteDataPLCOFF(bitResultNG);
        }

        public void SendResultToPLC(bool isResult)
        {
            if (isResult)
            {
                WriteDataPLC(bitInspectionEnd);
                WriteDataPLC(bitResultOK);
            }
            else
            {
                WriteDataPLC(bitInspectionEnd);
                WriteDataPLC(bitResultNG);
            }
        }

        public void WriteDataPLC(int register)
        {
            IsWritingDataToPLC = true;
            plcPanasonic_LX5S.WriteSingleCoilTCPIP(4096 + register, true);
            Thread.Sleep(20);
            IsWritingDataToPLC = false;
        }
        public void WriteDataPLCOFF(int register)
        {
            IsWritingDataToPLC = true;
            plcPanasonic_LX5S.WriteSingleCoilTCPIP(4096 + register, false);
            Thread.Sleep(20);
            IsWritingDataToPLC = false;
        }
    }
}
Leave a Comment