using EasyModbus; using Modbus.Device; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace KHModbus { public partial class FormModbus : Form { string mipAddr = "192.168.1.200"; //string mipAddr = "127.0.0.1"; int mPortNumber = 502; ModbusClient mModbusClient; List m_Data = new List(); public FormModbus() { InitializeComponent(); mModbusClient = new ModbusClient(mipAddr, mPortNumber); //Ip-Address and Port of Modbus-TCP-Server //timer_Connect.Enabled = true; SerialConnectionSingleton.Instance().InitialModBus(); SerialConnectionSingleton.Instance().PortName = "COM1"; SerialConnectionSingleton.Instance().BaudRate = 9600; SerialConnectionSingleton.Instance().Parity = System.IO.Ports.Parity.None; SerialConnectionSingleton.Instance().StopBits = System.IO.Ports.StopBits.One; SerialConnectionSingleton.Instance().DataBits = 8; SerialConnectionSingleton.Instance().SlaveNo = 1; //UpdateBaseoilSwitch(); } public void KHModbus2() { //client로 연결하는 부분 ModbusClient modbusClient = new ModbusClient(mipAddr, mPortNumber); //Ip-Address and Port of Modbus-TCP-Server modbusClient.Connect(); //Connect to Server modbusClient.WriteMultipleCoils(0, new bool[] { true, true, true, true, true, true, true, true, true, true }); //Write Coils starting with Address 5 bool[] readCoils = modbusClient.ReadCoils(9, 10); //Read 10 Coils from Server, starting with address 10 int[] readHoldingRegisters = modbusClient.ReadHoldingRegisters(0, 10); //Read 10 Holding Registers from Server, starting with Address 1 // Console Output //for (int i = 0; i < readCoils.Length; i++) // textBox1.Text += "Value of Coil " + (9 + i + 1) + " " + readCoils[i].ToString() +"\r\n"; //for (int i = 0; i < readHoldingRegisters.Length; i++) // textBox1.Text += "Value of HoldingRegister " + (i + 1) + " " + readHoldingRegisters[i].ToString() + "\r\n"; modbusClient.Disconnect(); //Disconnect from Server //Console.ReadKey(true); } public bool Connect() { try { if (!mModbusClient.Connected) mModbusClient.Connect(); if (mModbusClient.Connected) { textBox_State.Text += "Connected\r\n"; return true; }else { return false; } } catch(Exception ex) { textBox_State.Text += "PLC Connect Error : "+ ex.Message+ "\r\n"; return false; } } public bool[] DataReadCoils(int Addr, int Size) { if (mModbusClient.Connected) { bool[] read = mModbusClient.ReadCoils(Addr, Size); //Read 10 Holding Registers from Server, starting with Address 1 return read; } else { return null; } } public int[] DataReadHolding(int Addr, int Size) { try { if (mModbusClient.Connected) { int[] readHoldingRegisters = mModbusClient.ReadHoldingRegisters(Addr, Size); //Read 10 Holding Registers from Server, starting with Address 1 return readHoldingRegisters; } else { return null; } }catch(Exception ex) { textBox_State.Text += "DataReading Error : " + ex.Message + "\r\n"; mModbusClient.Disconnect(); return null; } } public int[] DataReadInput(int Addr, int Size) { try { if (mModbusClient.Connected) { int[] readHoldingRegisters = mModbusClient.ReadInputRegisters(Addr, Size); //Read 10 Holding Registers from Server, starting with Address 1 return readHoldingRegisters; } else { return null; } } catch (Exception ex) { textBox_State.Text += "DataReading Error : " + ex.Message + "\r\n"; mModbusClient.Disconnect(); return null; } } public async void modbusReadWrite() { TcpClient tcpc = new TcpClient(); tcpc.BeginConnect("127.0.0.1", 502, null, null); ModbusIpMaster master = ModbusIpMaster.CreateIp(tcpc); try { while(true) { bool[] coil = master.ReadCoils(0, 1); textBox_State.Text += coil[0].ToString(); ushort[] holding = master.ReadHoldingRegisters(0, 1); //textBox2.Text = holding[0].ToString(); ushort[] input = master.ReadHoldingRegisters(0, 9); //textBox2.Text = input[0].ToString(); await Task.Delay(500); } }catch (Exception ex) { } } private void simpleButton1_Click(object sender, EventArgs e) { KHModbus2(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (mModbusClient.Connected) { mModbusClient.Disconnect(); } else { } } private void timer_Connect_Tick(object sender, EventArgs e) { if(mModbusClient != null) { if(mModbusClient.Connected == false) { Connect(); } } } private void simpleButton2_Click(object sender, EventArgs e) { int[] Data = DataReadInput(400, 47); if (Data == null) return; textBox_State.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+ "[400,47]: "; foreach (int i in Data) { textBox_State.Text += i.ToString() + " "; } textBox_State.Text += "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); } private void simpleButton1_Click_1(object sender, EventArgs e) { DBConnectionSingleton.Instance().GetSqlData("select * from T_STD_COMPANY"); } private void simpleButton3_Click(object sender, EventArgs e) { if(DBConnectionSingleton.Instance().SetCommand("HTSaveRealData", DBConnectionSingleton.Instance().getParams("COMP_CD", "0001"), DBConnectionSingleton.Instance().getParams("MACH_CD", "M0001"), DBConnectionSingleton.Instance().getParams("REAL_DATA", "1"))) { textBox_State.Text += "DBInsert Complete\r\n"; } } private void simpleButton4_Click(object sender, EventArgs e) { //Stopwatch sw = new Stopwatch(); //sw.Reset(); //sw.Start(); //DB 설비정보내에서 온도계 정보를 가져옵니다.[온도계는 CODE정보로 E03으로 저장되어있다] DataTable dt = DBConnectionSingleton.Instance().GetSqlData("SELECT [COMP_CD],[MACH_CD],[MACH_NO],[MACH_NM] FROM [dbo].[T_STD_MACH] m where m.COMP_CD = '0001' and m.MACH_TYPE = '03' order by MACH_NO asc"); //sw.Stop(); //textBox_State.Text += "Timer Check 1 : "+sw.ElapsedMilliseconds.ToString() + "\r\n"; //sw.Start(); //ReadHolding데이터 0자리에서 10개를 가져온다 int[] Data = DataReadHolding(0, 10); //데이터를 가져오지 못하면 도루묵 if (Data == null) return; //sw.Stop(); //textBox_State.Text += "Timer Check 2 : " + sw.ElapsedMilliseconds.ToString() + "\r\n"; //sw.Start(); int Number= 1; string input_Mach_CD = ""; string input_RealData = ""; //설비정보에서 번호(1~10)와 가져온 정보 0~9)를 매칭하여 설비코드와 데이터를 나열한다. foreach (int i in Data) { for(int j = 0; j < dt.Rows.Count;j++) { if(dt.Rows[j]["MACH_NO"].ToString() == Number.ToString()) { input_Mach_CD += dt.Rows[j]["MACH_CD"].ToString() + ","; input_RealData += Data[i].ToString() + ","; } } Number++; } if(input_Mach_CD.LastIndexOf(',') == input_Mach_CD.Length-1) { input_Mach_CD = input_Mach_CD.Substring(0, input_Mach_CD.Length - 1); } if (input_RealData.LastIndexOf(',') == input_RealData.Length-1) { input_RealData = input_RealData.Substring(0, input_RealData.Length - 1); } //sw.Stop(); //textBox_State.Text += "Timer Check 3 : " + sw.ElapsedMilliseconds.ToString() + "\r\n"; //sw.Start(); if (DBConnectionSingleton.Instance().SetCommand("HTSaveRealDataAll", DBConnectionSingleton.Instance().getParams("COMP_CD", "0001"), DBConnectionSingleton.Instance().getParams("MACH_CD", input_Mach_CD), DBConnectionSingleton.Instance().getParams("REAL_DATA", input_RealData))) { textBox_State.Text += "DBInsert Complete : " + input_Mach_CD + "\r\n"; textBox_State.Text += "DBInsert Complete : " + input_RealData + "\r\n"; } //sw.Stop(); //textBox_State.Text += "Timer Check 4 : " + sw.ElapsedMilliseconds.ToString() + "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); } public int RunState = 0; List lst = new List(); string[,] array; private void timer_AutoInput_Tick(object sender, EventArgs e) { switch(RunState) { case 0: U3Util.ErrorLog(textBox_State.Text); textBox_State.Text = ""; lst = new List(); break; case 1: array = DataReadInputToInt64(textBox_Addr_1.Text, textBox_LENGTH_1.Text); if (array != null) if (array.Length > 1) lst.Add(array); break; case 2: array = DataReadInputToInt64(textBox_Addr_2.Text, textBox_LENGTH_2.Text); if (array != null) if (array.Length > 1) lst.Add(array); break; case 3: array = DataReadInput(textBox_Addr_2_2.Text, textBox_LENGTH_2_2.Text); if (array != null) if (array.Length > 1) lst.Add(array); break; case 4: array = DataReadInput(textBox_Addr_3.Text, textBox_LENGTH_3.Text); if (array != null) if (array.Length > 1) lst.Add(array); break; case 5: array = DataReadInput(textBox_Addr_4.Text, textBox_LENGTH_4.Text); if (array != null) if (array.Length > 1) lst.Add(array); break; case 6: array = DataReadInputToInt64(textBox_Addr_5.Text, textBox_LENGTH_5.Text); if (array != null) if (array.Length > 1) lst.Add(array); break; case 7: array = DataReadInputToBit(textBox_Addr_6.Text, textBox_LENGTH_6.Text); if (array != null) if (array.Length > 1) lst.Add(array); break; case 8: array = DataReadInputToBit(textBox_Addr_7.Text, textBox_LENGTH_7.Text); if (array != null) if (array.Length > 1) lst.Add(array); break; case 9: DB_Input(lst); break; } RunState++; if(RunState >=10) { RunState = 0; } //textBox_State.Text = ""; //List lst = new List(); //string[,] array; //array = DataReadInputToInt64(textBox_Addr_1.Text, textBox_LENGTH_1.Text); //if (array != null) if (array.Length > 1) lst.Add(array); //array = DataReadInputToInt64(textBox_Addr_2.Text, textBox_LENGTH_2.Text); //if (array != null) if (array.Length > 1) lst.Add(array); //array = DataReadInput(textBox_Addr_2_2.Text, textBox_LENGTH_2_2.Text); //if (array != null) if (array.Length > 1) lst.Add(array); //array = DataReadInput(textBox_Addr_3.Text, textBox_LENGTH_3.Text); //if (array != null) if (array.Length > 1) lst.Add(array); //array = DataReadInput(textBox_Addr_4.Text, textBox_LENGTH_4.Text); //if (array != null) if (array.Length > 1) lst.Add(array); //array = DataReadInputToInt64(textBox_Addr_5.Text, textBox_LENGTH_5.Text); //if (array != null) if (array.Length > 1) lst.Add(array); //array = DataReadInputToBit(textBox_Addr_6.Text, textBox_LENGTH_6.Text); //if (array != null) if (array.Length > 1) lst.Add(array); //DB_Input(lst); //if (SerialConnectionSingleton.Instance().IsOpen()) //{ // try // { // array = SerialConnectionSingleton.Instance().DataReadHolding("220", "1"); // if (array != null) // { // if (array.Length > 1) // { // lst.Add(array); // for (int i = 0; i < array.Length / 2; i++) // { // textBox_State.Text += "[" + array[i, 0] + ":" + array[i, 1] + "]"; // } // textBox_State.Text += "\r\n"; // } // } // array = SerialConnectionSingleton.Instance().DataReadHolding("231", "1"); // if (array != null) // { // if (array.Length > 1) // { // lst.Add(array); // for (int i = 0; i < array.Length / 2; i++) // { // textBox_State.Text += "[" + array[i, 0] + ":" + array[i, 1] + "]"; // } // textBox_State.Text += "\r\n"; // } // } // array = SerialConnectionSingleton.Instance().DataReadHolding("242", "1"); // if (array != null) // { // if (array.Length > 1) // { // lst.Add(array); // for (int i = 0; i < array.Length / 2; i++) // { // textBox_State.Text += "[" + array[i, 0] + ":" + array[i, 1] + "]"; // } // textBox_State.Text += "\r\n"; // } // } // array = SerialConnectionSingleton.Instance().DataReadHolding("253", "1"); // if (array != null) // { // if (array.Length > 1) // { // lst.Add(array); // for (int i = 0; i < array.Length / 2; i++) // { // textBox_State.Text += "[" + array[i, 0] + ":" + array[i, 1] + "]"; // } // textBox_State.Text += "\r\n"; // } // } // array = SerialConnectionSingleton.Instance().DataReadHolding("264", "1"); // if (array != null) // { // if (array.Length > 1) // { // lst.Add(array); // for (int i = 0; i < array.Length / 2; i++) // { // textBox_State.Text += "[" + array[i, 0] + ":" + array[i, 1] + "]"; // } // textBox_State.Text += "\r\n"; // } // } // array = SerialConnectionSingleton.Instance().DataReadHolding("275", "1"); // if (array != null) // { // if (array.Length > 1) // { // lst.Add(array); // for (int i = 0; i < array.Length / 2; i++) // { // textBox_State.Text += "[" + array[i, 0] + ":" + array[i, 1] + "]"; // } // textBox_State.Text += "\r\n"; // } // } // DB_Input(lst); // } // catch (Exception ex) // { // textBox_State.Text += "시리얼통신 연결중..." + "\r\n"; // } //} } private void toggleSwitch_Auto_Toggled(object sender, EventArgs e) { timer_Connect.Enabled = toggleSwitch_Auto.IsOn; timer_AutoInput.Enabled = toggleSwitch_Auto.IsOn; timer_DBInsert.Enabled = toggleSwitch_Auto.IsOn; } private void simpleButton5_Click(object sender, EventArgs e) { if(SerialConnectionSingleton.Instance().IsOpen()) { //ushort resultBit = SerialConnectionSingleton.Instance().ReadWords_To_ushort(4, 0, 2); //ushort resultBit2 = SerialConnectionSingleton.Instance().ReadWords_To_ushort(4, 1, 1); //for(int i = 1000; i<=1010;i++) //{ // int resultBit = SerialConnectionSingleton.Instance().ReadWords_To_int(1, 4, (ushort)i, 2); // textBox_State.Text += "Slave1_4_"+i+" : " + resultBit + "\r\n"; // textBox_State.Select(textBox_State.Text.Length, 0); // textBox_State.ScrollToCaret(); //} //for (int i = 31000; i <= 31010; i++) //{ // int resultBit = SerialConnectionSingleton.Instance().ReadWords_To_int(1, 4, (ushort)i, 2); // textBox_State.Text += "Slave1_4_" + i + " : " + resultBit + "\r\n"; // textBox_State.Select(textBox_State.Text.Length, 0); // textBox_State.ScrollToCaret(); //} //for (int i = 1000; i <= 1010; i++) //{ // int resultBit = SerialConnectionSingleton.Instance().ReadWords_To_int(2, 4, (ushort)i, 2); // textBox_State.Text += "Slave1_3_" + i + " : " + resultBit + "\r\n"; // textBox_State.Select(textBox_State.Text.Length, 0); // textBox_State.ScrollToCaret(); //} int resultBit1 = SerialConnectionSingleton.Instance().ReadWords_To_int(2, 4, 1002, 2); textBox_State.Text += "[SV]Slave2_4_" + 1002 + " : " + resultBit1 + "\r\n"; int resultBit2 = SerialConnectionSingleton.Instance().ReadWords_To_int(2, 4, 1005, 2); textBox_State.Text += "[PV]Slave2_4_" + 1005 + " : " + resultBit2 + "\r\n"; //for (int i = 31000; i <= 31010; i++) //{ // int resultBit = SerialConnectionSingleton.Instance().ReadWords_To_int(1, 3, (ushort)i, 2); // textBox_State.Text += "Slave1_3_" + i + " : " + resultBit + "\r\n"; // textBox_State.Select(textBox_State.Text.Length, 0); // textBox_State.ScrollToCaret(); //} //int resultBit3 = SerialConnectionSingleton.Instance().ReadWords_To_int(1, 4, 1005, 2); //int resultBit4 = SerialConnectionSingleton.Instance().ReadWords_To_int(2, 4, 31004, 2); //int resultBit3 = SerialConnectionSingleton.Instance().ReadWords_To_int(1, 4, 1005, 2); //int resultBit4 = SerialConnectionSingleton.Instance().ReadWords_To_int(4, 0, 2); //textBox_State.Text += "ReadSerial1 : " + resultBit + "\r\n"; //textBox_State.Text += "ReadSerial2 : " + resultBit2 + "\r\n"; //textBox_State.Text += "Slave1_4_31004 : " + resultBit3 + "\r\n"; //textBox_State.Text += "Slave2_4_31004 : " + resultBit4 + "\r\n"; } } private void simpleButton6_Click(object sender, EventArgs e) { if (SerialConnectionSingleton.Instance().IsOpen()) { try { int resultBit1 = SerialConnectionSingleton.Instance().ReadWords_To_int(Convert.ToByte(textBox_DEV_ID.Text), Convert.ToByte(textBox_FUNC.Text), Convert.ToUInt16(textBox_ADDR.Text), 2); textBox_State.Text += "[4Byte]Slave"+ textBox_DEV_ID.Text + "_"+ textBox_FUNC.Text + "_" + textBox_ADDR.Text + " : " + resultBit1 + "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); }catch(Exception ex) { textBox_State.Text += ex.Message+"\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); } } } private void simpleButton7_Click(object sender, EventArgs e) { if (SerialConnectionSingleton.Instance().IsOpen()) { try { ushort resultBit1 = SerialConnectionSingleton.Instance().ReadWords_To_ushort(Convert.ToByte(textBox_DEV_ID.Text), Convert.ToByte(textBox_FUNC.Text), Convert.ToUInt16(textBox_ADDR.Text), 1); textBox_State.Text += "[2Byte]Slave" + textBox_DEV_ID.Text + "_" + textBox_FUNC.Text + "_" + textBox_ADDR.Text + " : " + resultBit1 + "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); } catch (Exception ex) { textBox_State.Text += ex.Message + "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); } } } private void FormModbus_Load(object sender, EventArgs e) { } public string[,] DataReadHolding(string Addr, string Length) { int[] Data = DataReadHolding(Convert.ToInt32(Addr), Convert.ToInt32(Length)); if (Data == null) return null; string[,] result = new string[Data.Length, 2]; textBox_State.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": "; int aa = 0; foreach (int i in Data) { textBox_State.Text += "[" + (Convert.ToInt32(Addr) + aa).ToString() + ":" + i.ToString() + "]."; result[aa, 0] = (Convert.ToInt32(Addr) + aa).ToString(); result[aa, 1] = i.ToString(); aa++; } textBox_State.Text += "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); return result; } public string[,] DataReadInput(string Addr, string Length) { int[] Data = DataReadInput(Convert.ToInt32(Addr), Convert.ToInt32(Length)); if (Data == null) return null; string[,] result = new string[Data.Length, 2]; textBox_State.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": "; int aa = 0; foreach (int i in Data) { textBox_State.Text += "[" + (Convert.ToInt32(Addr) + aa).ToString() + ":" + i.ToString() + "]."; result[aa, 0] = (Convert.ToInt32(Addr) + aa).ToString(); result[aa, 1] = i.ToString(); aa++; } textBox_State.Text += "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); return result; } public string[,] DataReadInputToBit(string Addr, string Length) { int[] Data = DataReadInput(Convert.ToInt32(Addr), Convert.ToInt32(Length)); if (Data == null) return null; string[,] result = new string[Data.Length*32, 2]; textBox_State.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": "; int aa = 0; string Result = ""; for (int i = 0; i < Data.Length; i++) { byte[] a = BitConverter.GetBytes(Convert.ToInt32(Data[i])); textBox_State.Text += "[" + (Convert.ToInt32(Addr) + i).ToString() + ":"; for (int j = 0; j < a.Length; j++) { string s = Convert.ToString(a[j], 2).PadLeft(8, '0'); char[] sa = s.ToCharArray(); Array.Reverse(sa); for (int r = 0; r < sa.Length; r++) { result[((i * a.Length * 8) + (j * 8)) + r, 0] = (Convert.ToInt32(Addr) + i).ToString()+"."+((j*8)+r).ToString(); result[((i * a.Length * 8) + (j * 8)) + r, 1] = sa[r].ToString(); } textBox_State.Text += new string(sa); Result += new string(sa); } textBox_State.Text += "]."; } textBox_State.Text += "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); return result; } public string[,] DataReadInputTouUint64(string Addr, string Length) { //Stopwatch sw = new Stopwatch(); //sw.Start(); int[] Data = DataReadInput(Convert.ToInt32(Addr), Convert.ToInt32(Length)); if (Data == null) return null; string[,] result = new string[Data.Length / 2 + ((Data.Length & 2) == 1 ? 1 : 0), 2]; //sw.Stop(); //textBox_State.Text += "[Time1: " + sw.ElapsedMilliseconds + "] \r\n"; //sw.Reset(); //sw.Start(); textBox_State.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": "; int aa = 0; for (int i = 0; i < Data.Length - 1; i++) { byte[] a = BitConverter.GetBytes(Convert.ToInt16(Data[i])); byte[] b = null; if (Data.Length > i + 1) { b = BitConverter.GetBytes(Convert.ToInt16(Data[i + 1])); } else { b = new byte[2] { 0, 0 }; } byte[] c = new byte[4]; Array.Copy(a, 0, c, 0, a.Length); Array.Copy(b, 0, c, a.Length, b.Length); textBox_State.Text += "[" + (Convert.ToInt32(Addr) + i).ToString() + ":" + BitConverter.ToInt32(c, 0).ToString() + "]."; result[aa, 0] = (Convert.ToInt32(Addr) + i).ToString(); result[aa, 1] = BitConverter.ToUInt32(c, 0).ToString(); aa++; i++; } foreach (int i in Data) { } textBox_State.Text += "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); //sw.Stop(); //textBox_State.Text += "[Time2: " + sw.ElapsedMilliseconds + "] \r\n"; return result; } public string[,] DataReadInputToInt64(string Addr, string Length) { //Stopwatch sw = new Stopwatch(); //sw.Start(); int[] Data = DataReadInput(Convert.ToInt32(Addr), Convert.ToInt32(Length)); if (Data == null) return null; string[,] result = new string[Data.Length/2+((Data.Length&2)==1?1:0), 2]; //sw.Stop(); //textBox_State.Text += "[Time1: " + sw.ElapsedMilliseconds + "] \r\n"; //sw.Reset(); //sw.Start(); textBox_State.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": "; int aa = 0; for(int i=0 ; i i+1) { b = BitConverter.GetBytes(Convert.ToInt16(Data[i + 1])); }else { b = new byte[2] {0,0}; } byte[] c = new byte[4]; Array.Copy(a, 0, c, 0, a.Length); Array.Copy(b, 0, c, a.Length, b.Length); textBox_State.Text += "[" + (Convert.ToInt32(Addr) + i).ToString() + ":" + BitConverter.ToInt32(c,0).ToString() + "]."; result[aa, 0] = (Convert.ToInt32(Addr) + i).ToString(); result[aa, 1] = BitConverter.ToInt32(c, 0).ToString(); aa++; i++; } foreach (int i in Data) { } textBox_State.Text += "\r\n"; textBox_State.Select(textBox_State.Text.Length, 0); textBox_State.ScrollToCaret(); //sw.Stop(); //textBox_State.Text += "[Time2: " + sw.ElapsedMilliseconds + "] \r\n"; return result; } private void simpleButton_InputRegister_1_Click(object sender, EventArgs e) { textBox_State.Text = ""; string[,] Data = DataReadInputToInt64(textBox_Addr_1.Text, textBox_LENGTH_1.Text); DB_Input(Data); } private void simpleButton_InputRegister_2_Click(object sender, EventArgs e) { textBox_State.Text = ""; string[,] Data = DataReadInputToInt64(textBox_Addr_2.Text, textBox_LENGTH_2.Text); DB_Input(Data); } private void simpleButton_InputRegister_3_Click(object sender, EventArgs e) { textBox_State.Text = ""; string[,] Data = DataReadInput(textBox_Addr_3.Text, textBox_LENGTH_3.Text); DB_Input(Data); } private void simpleButton_InputRegister_4_Click(object sender, EventArgs e) { textBox_State.Text = ""; string[,] Data = DataReadInput(textBox_Addr_4.Text, textBox_LENGTH_4.Text); DB_Input(Data); } private void simpleButton_InputRegister_5_Click(object sender, EventArgs e) { textBox_State.Text = ""; string[,] Data = DataReadInputTouUint64(textBox_Addr_5.Text, textBox_LENGTH_5.Text); DB_Input(Data); } private void simpleButton_InputRegister_6_Click(object sender, EventArgs e) { //textBox_State.Text = ""; string[,] Data = DataReadInputToBit(textBox_Addr_6.Text, textBox_LENGTH_6.Text); //for (int i = 0; i < Data.Length / 2; i++) //{ // textBox_State.Text += "["+Data[i,0]+ " : "+Data[i,1]+"]"; //} //textBox_State.Text += "\r\n"; DB_Input(Data); } private void simpleButton_InputRegister_7_Click(object sender, EventArgs e) { string[,] Data = DataReadInputToBit(textBox_Addr_7.Text, textBox_LENGTH_7.Text); DB_Input(Data); } private void simpleButton_InputRegister_2_2_Click(object sender, EventArgs e) { textBox_State.Text = ""; string[,] Data = DataReadInput(textBox_Addr_2_2.Text, textBox_LENGTH_2_2.Text); DB_Input(Data); } public void DB_Input(List Data) { m_Data = Data; } public void DB_Input(string[,]Data) { if (!DBConnectionSingleton.Instance().isConnect()) return; //DB 설비정보내에서 온도계 정보를 가져옵니다.[온도계는 CODE정보로 E03으로 저장되어있다] DataTable dt = DBConnectionSingleton.Instance().GetSqlData("SELECT [COMP_CD],[MACH_CD],[MACH_NO],[MACH_NM] FROM [dbo].[T_STD_MACH] m where m.COMP_CD = '0001' order by MACH_NO asc"); if (dt == null) return; string input_Mach_CD = ""; string input_RealData = ""; textBox_State.Text += "TEST START \r\n"; if (Data != null) { for (int i = 0; i < Data.Length / 2; i++) { for (int j = 0; j < dt.Rows.Count; j++) { if (dt.Rows[j]["MACH_NO"].ToString() == Data[i,0].ToString()) { textBox_State.Text += "["+ dt.Rows[j]["MACH_CD"].ToString() + ":"+ Data[i, 1].ToString() + "]"; input_Mach_CD += dt.Rows[j]["MACH_CD"].ToString() + ","; input_RealData += Data[i,1].ToString() + ","; break; } } } } textBox_State.Text += "TEST END\r\n"; if (input_Mach_CD.LastIndexOf(',') == input_Mach_CD.Length - 1 && input_Mach_CD.LastIndexOf(',') != -1) { input_Mach_CD = input_Mach_CD.Substring(0, input_Mach_CD.Length - 1); } if (input_RealData.LastIndexOf(',') == input_RealData.Length - 1 && input_RealData.LastIndexOf(',') != -1) { input_RealData = input_RealData.Substring(0, input_RealData.Length - 1); } //sw.Stop(); //textBox_State.Text += "Timer Check 3 : " + sw.ElapsedMilliseconds.ToString() + "\r\n"; //sw.Start(); if (input_Mach_CD == "" || input_RealData == "") return; if (DBConnectionSingleton.Instance().SetCommand("HTSaveRealDataAll", DBConnectionSingleton.Instance().getParams("COMP_CD", "0001"), DBConnectionSingleton.Instance().getParams("MACH_CD", input_Mach_CD), DBConnectionSingleton.Instance().getParams("REAL_DATA", input_RealData))) { textBox_State.Text += "DBInsert Complete : " + input_Mach_CD + "\r\n"; textBox_State.Text += "DBInsert Complete : " + input_RealData + "\r\n"; } } class MACHDATA { public string MACH_NO { get; set; } public string DATA { get; set; } } public void All_Data_Insert() { //DB 설비정보내에서 온도계 정보를 가져옵니다.[온도계는 CODE정보로 E03으로 저장되어있다] DataTable dt = DBConnectionSingleton.Instance().GetSqlData("SELECT [COMP_CD],[MACH_CD],[MACH_NO],[MACH_NM] FROM [dbo].[T_STD_MACH] m where m.COMP_CD = '0001' order by MACH_CD asc"); if (dt == null) return; string input_Mach_CD = ""; string input_RealData = ""; List inputData = new List(); if (m_Data != null) { if (m_Data.Count >= 1) { foreach (string[,] strData in m_Data) { for (int i = 0; i < strData.Length / 2; i++) { for (int j = 0; j < dt.Rows.Count; j++) { if (dt.Rows[j]["MACH_NO"].ToString() == strData[i, 0].ToString()) { MACHDATA MData = new MACHDATA(); MData.MACH_NO = dt.Rows[j]["MACH_CD"].ToString(); MData.DATA = strData[i, 1].ToString(); inputData.Add(MData); //input_Mach_CD += dt.Rows[j]["MACH_CD"].ToString() + ","; //input_RealData += strData[i, 1].ToString() + ","; break; } } } } } } List sortList = inputData.OrderBy(x => x.MACH_NO).ThenBy(x => x.DATA).ToList(); foreach(MACHDATA md in sortList) { input_Mach_CD += md.MACH_NO + ","; input_RealData += md.DATA + ","; } if (input_Mach_CD.LastIndexOf(',') == input_Mach_CD.Length - 1 && input_Mach_CD.LastIndexOf(',') != -1) { input_Mach_CD = input_Mach_CD.Substring(0, input_Mach_CD.Length - 1); } if (input_RealData.LastIndexOf(',') == input_RealData.Length - 1 && input_RealData.LastIndexOf(',') != -1) { input_RealData = input_RealData.Substring(0, input_RealData.Length - 1); } //sw.Stop(); //textBox_State.Text += "Timer Check 3 : " + sw.ElapsedMilliseconds.ToString() + "\r\n"; //sw.Start(); if (input_Mach_CD == "" || input_RealData == "") return; if (DBConnectionSingleton.Instance().SetCommand("HTSaveRealDataAll", DBConnectionSingleton.Instance().getParams("COMP_CD", "0001"), DBConnectionSingleton.Instance().getParams("MACH_CD", input_Mach_CD), DBConnectionSingleton.Instance().getParams("REAL_DATA", input_RealData))) { textBox_State.Text += "DBInsert Complete : " + input_Mach_CD + "\r\n"; textBox_State.Text += "DBInsert Complete : " + input_RealData + "\r\n"; } UpdateBaseoilSwitch(); } private void UpdateBaseoilSwitch() { U3Database db = new U3Database(); string strSQL = ""; Dictionary myDictionary = new Dictionary(); strSQL = "select isnull(REMARK02, '') as REMARK02 from T_STD_MACH where MACH_TYPE = '05' group by REMARK02"; DataTable dt = db.OpenSQL(strSQL); foreach (DataRow dr in dt.Rows) { string s = U3Util.toStr(dr["REMARK02"]); if (s.Length > 0) { myDictionary.Add(s, ""); } } strSQL = ""; strSQL += " select a.MACH_CD, a.MACH_NM, isnull(b.REAL_DATA, 0) as REAL_DATA, a.REMARK01, a.REMARK02"; strSQL += " from T_STD_MACH a"; strSQL += " left join T_HT_REAL_DATA b on a.MACH_CD = b.MACH_CD"; strSQL += " where a.MACH_TYPE = '05'"; strSQL += " and isnull(b.REAL_DATA, 0) = 1"; dt = db.OpenSQL(strSQL); foreach (DataRow dr in dt.Rows) { string s1 = U3Util.toStr(dr["REMARK01"]); string s2 = U3Util.toStr(dr["REMARK02"]); if (s1.Length > 0 && s2.Length > 0) { myDictionary[s2] = s1; } } foreach (KeyValuePair item in myDictionary) { string sql1 = ""; sql1 += " update T_HT_REAL_BASEOIL set"; sql1 += " HBT_CD = '" + item.Value + "', "; sql1 += " REG_DT = getdate()"; sql1 += " where COMP_CD = '0001' and BASEOIL_CD = '" + item.Key + "'"; string sql2 = ""; sql2 += " insert into T_HT_REAL_BASEOIL (COMP_CD, BASEOIL_CD) values"; sql2 += " ('0001', '" + item.Key + "') "; int nResult = db.ExcuteSql(sql1); if (nResult == 0) { db.ExcuteSql(sql2); db.ExcuteSql(sql1); } } } private void simpleButton_All_Click(object sender, EventArgs e) { textBox_State.Text = ""; var lst = new List(); string[,] array; array = DataReadInputToInt64(textBox_Addr_1.Text, textBox_LENGTH_1.Text); if(array!=null) if (array.Length > 1) lst.Add(array); array = DataReadInputToInt64(textBox_Addr_2.Text, textBox_LENGTH_2.Text); if (array != null) if (array.Length > 1) lst.Add(array); array = DataReadInput(textBox_Addr_2_2.Text, textBox_LENGTH_2_2.Text); if (array != null) if (array.Length > 1) lst.Add(array); array = DataReadInput(textBox_Addr_3.Text, textBox_LENGTH_3.Text); if (array != null) if (array.Length > 1) lst.Add(array); array = DataReadInput(textBox_Addr_4.Text, textBox_LENGTH_4.Text); if (array != null) if (array.Length > 1) lst.Add(array); array = DataReadInputToInt64(textBox_Addr_5.Text, textBox_LENGTH_5.Text); if (array != null) if (array.Length > 1) lst.Add(array); array = DataReadInputToBit(textBox_Addr_6.Text, textBox_LENGTH_6.Text); if (array != null) if (array.Length > 1) lst.Add(array); DB_Input(lst); } private void timer_DBConnect_Tick(object sender, EventArgs e) { try { if (!DBConnectionSingleton.Instance().isConnect()) if (DBConnectionSingleton.Instance().Connect()) { textBox_State.Text += "DB_Connected\r\n"; panel_DBConnect.BackColor = Color.FromArgb(192, 255, 192); } else { panel_DBConnect.BackColor = Color.FromArgb(255, 192, 192); } } catch (Exception ex) { textBox_State.Text += "Connect Error : " + ex.Message + "\r\n"; } if (DBConnectionSingleton.Instance().isConnect()) { All_Data_Insert(); } } private void timer_Log_Tick(object sender, EventArgs e) { if (DBConnectionSingleton.Instance().SetCommand("HTInsertRealToLog")) { //textBox_State.Text += "DBInsert Complete : Log Insert\r\n"; } } private void toggleSwitch_Log_Toggled(object sender, EventArgs e) { timer_Log.Enabled = toggleSwitch_Log.IsOn; } private void simpleButton8_Click_1(object sender, EventArgs e) { } } }