using System.IO.Ports;


namespace NDSD_Screwdriver.Tool
{
    public class SerialPortFactory
    {
        private SerialPort serialPortModbus;
        private hfIIReader reader;
        public SerialPortFactory(string portName="COM13")
        {
            reader = new hfIIReader();
            serialPortModbus = new SerialPort();
            serialPortModbus.BaudRate = 9600;
            serialPortModbus.PortName = portName;
            serialPortModbus.ReadTimeout = 0;
        }


        public string Read()
        {

            if (!serialPortModbus.IsOpen)
            {
                serialPortModbus.Open();
                var b= reader.Connect(serialPortModbus, 2000, 0);
            }
            
           
            string str2 = "";
            ushort[] array6 = reader.ReadTagMemory(ref str2, 28, 4);
            
            string text6 = "";
            for (int j = 0; j < 4; j += 1)
            {
                text6 += array6[j].ToString("X").PadLeft(4, '0');
                if (text6.Length > 0)
                {
                    text6 += "";
                }
            }

            return text6;
        }


        public void Disconnect()
        {
            if (!serialPortModbus.IsOpen)
            {
                serialPortModbus.Close();
            }
            reader.Disconnect();
        }
        
        
    }
}