using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; using System.Threading; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.InteropServices; using System.Reflection; using System.Diagnostics; namespace FPER { public enum default_value_define : byte { prt_header_id = 0x02, size_PRTReceiver_Type1 = 0x08, size_PRTReceiver_Type2 = 0x09, size_PRTReceiver_Type3 = 0x0A, size_PRTReceiver_Type4 = 0x0B, size_PRTReceiver_Type5 = 0x0C, size_PRTReceiver_Type6 = 0x0D } public enum CMD_define : byte { say_hello = 0x01, resd_tbd_state = 0x05, write_tbd_state = 0x06, search_repeater_all = 0x12, read_repeater_all = 0x14, write_repeater_output_unit = 0x15, read_repeater_info_all = 0x18, write_repeater_info_unit = 0x19, reset_repeater_unit = 0x1B, reset_repeater_all = 0x1C, write_repeater_input_unit = 0x1D } public enum RES_define : byte { general_ack = 0x81, general_nack = 0x80, say_hello = 0x81, resd_tbd_state = 0x85, write_tbd_state = 0x86, search_repeater_all = general_ack, read_repeater_all = 0x94, write_repeater_output_unit = 0x95, read_repeater_info_all = 0x98, write_repeater_info_unit = 0x99, reset_repeater_unit = general_ack, reset_repeater_all = general_ack, write_repeater_input_unit = 0x1D } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct PRTReceiver_Type1 { public byte head; public byte RPT_loop; public byte TBD_ID; public byte length; public byte cmd; public byte IVK_ID; public byte RCVR_ID; public byte TBD_loop; public byte cs; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct PRTReceiver_Type2 { public byte head; public byte RPT_loop; public byte TBD_ID; public byte length; public byte cmd; public byte IVK_ID; public byte RCVR_ID; public byte TBD_loop; public byte Option1; public byte cs; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct PRTReceiver_Type3 { public byte head; public byte RPT_loop; public byte TBD_ID; public byte length; public byte cmd; public byte IVK_ID; public byte RCVR_ID; public byte TBD_loop; public byte Option1; public byte Option2; public byte cs; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct PRTReceiver_Type4 { public byte head; public byte RPT_loop; public byte TBD_ID; public byte length; public byte cmd; public byte IVK_ID; public byte RCVR_ID; public byte TBD_loop; public byte Option1; public byte Option2; public byte Option3; public byte cs; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct PRTReceiver_Type5 { public byte head; public byte RPT_loop; public byte TBD_ID; public byte length; public byte cmd; public byte IVK_ID; public byte RCVR_ID; public byte TBD_loop; public byte Option1; public byte Option2; public byte Option3; public byte Option4; public byte cs; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct PRTReceiver_Type6 { public byte head; public byte RPT_loop; public byte TBD_ID; public byte length; public byte cmd; public byte IVK_ID; public byte RCVR_ID; public byte TBD_loop; public byte Option1; public byte Option2; public byte Option3; public byte Option4; public byte Option5; public byte cs; } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct Receiver { public byte head; public byte cmdID; public byte Reserved_1; public byte length; public byte cmd; public byte option1; public byte option2; public byte option3; public byte option4; public byte cs; } public class RCVData_ACK { // base data // public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int repeater_ID = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID public void Set_Data(byte[] buffer) { this.TBD_ID = buffer[2]; this.TBD_loop = buffer[7]; this.repeater_loop = buffer[1]; this.InvorkNo = buffer[5]; this.ReceiverID = buffer[6]; //insert data } } public class RCVData_NACK { // base data // public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int repeater_ID = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID public void Set_Data(byte[] buffer) { this.TBD_ID = buffer[2]; this.TBD_loop = buffer[7]; this.repeater_loop = buffer[1]; this.InvorkNo = buffer[5]; this.ReceiverID = buffer[6]; //insert data } } public class RCVData_ReadTBoardStatus { // base data // public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int repeater_ID = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID public int TBD_MainVersion; public int TBD_SubVersion; public int RCV_BaudRate; public int TBD_Loop1_BaudRate; public int TBD_Loop2_BaudRate; public int TBD_Term; public int TBD_24Voltage; public int TBD_5Voltage; public int TBD_33Voltage; public int TBD_Cut; public void Set_Data(byte[] buffer) { this.TBD_ID = buffer[2]; this.TBD_loop = buffer[7]; this.repeater_loop = buffer[1]; this.InvorkNo = buffer[5]; this.ReceiverID = buffer[6]; //insert data } } public class RCVData_WriteTBoardStatus { // base data // public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int repeater_ID = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID public void Set_Data(byte[] buffer) { this.TBD_ID = buffer[2]; this.TBD_loop = buffer[7]; this.repeater_loop = buffer[1]; this.InvorkNo = buffer[5]; this.ReceiverID = buffer[6]; //insert data } } public class RCVData_ReadRepeaterPoint_unit { // repeater id// public int repeater_ID = 0; // repeater state // public int Err = 0; //¿¡·¯»óÅ public int Non = 0; //Á¸ÀçÇÏÁö ¾Ê´Â Áß°è±â public int R0 = 0; //Reserved public int R1 = 0; //Reserved public int R2 = 0; //Reserved public int R = 0; //Áß°è±â º¹±¸ ¸í·É 0-¿Ï·á,1-¿Ï·á¾ÈÇÔ public int W = 0; //Ãâ·Â¾²±â ¿Ï·á 0-¿Ï·á,1-¿Ï·á¾ÈÇÔ public int S = 0; //Search Complete 0-¿Ï·á,1-¿Ï·á¾ÈÇÔ // repeater input // public int repeater_state_1 = 0; //0-°¨Áö,1-Á¤»ó,3-´Ü¼± public int repeater_state_2 = 0; //0-°¨Áö,1-Á¤»ó,3-´Ü¼± public int repeater_state_3 = 0; //0-°¨Áö,1-Á¤»ó,3-´Ü¼± public int repeater_state_4 = 0; //0-°¨Áö,1-Á¤»ó,3-´Ü¼± //repeater output // public int repeater_output_4_C = 0; //Ãâ·ÂÈ®ÀÎ 0-¿Ï·á,1-¿Ï·á¾ÈÇÔ public int repeater_output_3_C = 0; //Ãâ·ÂÈ®ÀÎ 0-¿Ï·á,1-¿Ï·á¾ÈÇÔ public int repeater_output_2_C = 0; //Ãâ·ÂÈ®ÀÎ 0-¿Ï·á,1-¿Ï·á¾ÈÇÔ public int repeater_output_1_C = 0; //Ãâ·ÂÈ®ÀÎ 0-¿Ï·á,1-¿Ï·á¾ÈÇÔ public int repeater_output_4_W = 0; //Ãâ·Â¾²±â public int repeater_output_3_W = 0; //Ãâ·Â¾²±â public int repeater_output_2_W = 0; //Ãâ·Â¾²±â public int repeater_output_1_W = 0; //Ãâ·Â¾²±â //public int repeater_state_1 { get { return this.repeater_state_1; } } public void Set_Data(byte repeater_ID,byte state,byte input,byte output) { this.repeater_ID = repeater_ID; set_state_data(state); set_input_data(input); set_output_data(output); } public void set_state_data(byte in_data){ Err = (in_data >> 7) & 0x01; Non = (in_data >> 6) & 0x01; R0 = (in_data >> 5) & 0x01; R1 = (in_data >> 4) & 0x01; R2 = (in_data >> 3) & 0x01; R = (in_data >> 2) & 0x01; W = (in_data >> 1) & 0x01; S = (in_data >> 0) & 0x01; } public void set_input_data(byte in_data){ repeater_state_4 = (in_data >> 6) & 0x03; repeater_state_3 = (in_data >> 4) & 0x03; repeater_state_2 = (in_data >> 2) & 0x03; repeater_state_1 = (in_data >> 0) & 0x03; } public void set_output_data(byte in_data){ repeater_output_4_C = (in_data >> 7) & 0x01; repeater_output_3_C = (in_data >> 6) & 0x01; repeater_output_2_C = (in_data >> 5) & 0x01; repeater_output_1_C = (in_data >> 4) & 0x01; repeater_output_4_W = (in_data >> 3) & 0x01; repeater_output_3_W = (in_data >> 2) & 0x01; repeater_output_2_W = (in_data >> 1) & 0x01; repeater_output_1_W = (in_data >> 0) & 0x01; } } public class RCVData_ReadRepeaterPointAll { // repeater id // -> bd no , loop no , repeater id. public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID public RCVData_ReadRepeaterPoint_unit[] data = new RCVData_ReadRepeaterPoint_unit[127]; public void Set_Data(byte[] buffer) { this.TBD_ID = buffer[2]; this.TBD_loop = buffer[9]; this.repeater_loop = buffer[1]; this.InvorkNo = buffer[7]; this.ReceiverID = buffer[8]; for(int i=0; i<127; i++) { // data[i] = new RCVData_ReadRepeaterPoint_unit(); data[i].Set_Data((byte)(i + 1), buffer[3 * i + 10], buffer[3 * i + 11], buffer[3 * i + 12]); } } } public class RCVData_WriteRepeaterOutputUNIT { // base data // public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int repeater_ID = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID public RCVData_ReadRepeaterPoint_unit data = new RCVData_ReadRepeaterPoint_unit(); public void Set_Data(byte[] buffer) { this.TBD_ID = buffer[2]; this.TBD_loop = buffer[7]; this.repeater_loop = buffer[1]; this.InvorkNo = buffer[5]; this.ReceiverID = buffer[6]; this.data.Set_Data(buffer[8], buffer[9], buffer[10], buffer[11]); } } public class RCVData_ReadRepeaterInfo_unit { // repeater id// public int repeater_ID = 0; // repeater info // public int B = 0; public int Rserved1 = 0; public int Rserved2 = 0; public int Rserved3 = 0; public int Info = 0; public void Set_Data(byte repeater_ID,byte InfoData) { this.repeater_ID = repeater_ID; this.set_infodata(InfoData); } void set_infodata(byte InfoData) { this.B = (InfoData >> 7) & 0x01; this.Rserved1 = (InfoData >> 6) & 0x01; this.Rserved2 = (InfoData >> 5) & 0x01; this.Rserved3 = (InfoData >> 4) & 0x01; int tmp_data = (InfoData >> 0) & 0x0f; switch(tmp_data) { case 0x00: { this.Info = 0; break; } case 0x01: { this.Info = 1; break; } case 0x02: { this.Info = 2; break; } case 0x04: { this.Info = 3; break; } case 0x05: { this.Info = 4; break; } default : { this.Info = -1; break; } } } } //Áß°è±â ȸ·Î µ¥ÀÌÅÍ-- public class RCVData_ReadRepeaterInfoAll { // base data // public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int repeater_ID = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID public RCVData_ReadRepeaterInfo_unit[] data = new RCVData_ReadRepeaterInfo_unit[127]; public void Set_Data(byte[] buffer) { this.TBD_ID = buffer[2]; this.TBD_loop = buffer[7]; this.repeater_loop = buffer[1]; this.InvorkNo = buffer[5]; this.ReceiverID = buffer[6]; for(int i=0; i<128; i++) { this.data[i].Set_Data((byte)(i+1), buffer[i+8]); } } } public class RCVData_WriteRepeaterInfoUNIT { // base data // public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int repeater_ID = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID public RCVData_ReadRepeaterInfo_unit data = new RCVData_ReadRepeaterInfo_unit(); public void Set_Data(byte[] buffer) { this.TBD_ID = buffer[2]; this.TBD_loop = buffer[7]; this.repeater_loop = buffer[1]; this.InvorkNo = buffer[5]; this.ReceiverID = buffer[6]; this.data.Set_Data(buffer[8], buffer[9]); } } public class RCVData_WriteRepeaterInputUNIT { // base data // public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int repeater_ID = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID public RCVData_ReadRepeaterPoint_unit data = new RCVData_ReadRepeaterPoint_unit(); public void Set_Data(byte[] buffer) { this.TBD_ID = buffer[2]; this.TBD_loop = buffer[7]; this.repeater_loop = buffer[1]; this.InvorkNo = buffer[5]; this.ReceiverID = buffer[6]; this.data.Set_Data(buffer[8], buffer[9], buffer[10], buffer[11]); } } public class Tprotocol { private MDIParent main; private TcpClient cmd_tcpclient; private TcpClient event_tcpclient; private String DestIP; private int commandPort; private int eventPort; private Thread cmd_hn_th; private int cmd_hn_th_state = 0; private Thread cmd_rcv_th; private int cmd_rcv_th_state = 0; //private Socket sc; private NetworkStream cmd_sendStm, cmd_recvStm; private NetworkStream event_sendStm, event_recvStm; private Thread event_rcv_th; private int event_rcv_th_state; private Paket pkt; private int cmd_state = 0; // 0 : init , cmd don't exist , 1: cmd exist, before send , 2 : cmd exist , after sned , before rcv , 3: cmd exist , after rcv , -1 : cmd exit, after send , rcv time out error , -2 : unknown cmd, -3: unknown rcv [DllImport("kernel32.dll")] static extern void OutputDebugString(string message); public void DoPrintDBGMSG(string str) { str = "[FPER]" + System.DateTime.Now.ToString("[hh:mm:ss]") + ":" + str; OutputDebugString(str); } public void OpenDBGMSG(string str) { DoPrintDBGMSG("+" + str); } public void CloseDBGMSG(string str) { DoPrintDBGMSG("-" + str); } public Tprotocol(MDIParent main) { this.main = main; this.pkt = new Paket(); } public int ConnectToDemon(String Dest_ip, int commandPort , int eventPort ,int Demon_ID) { // start rcv thread // send connect cmd // check connect cmd' response // return result try { this.DestIP = Dest_ip; this.commandPort = commandPort; this.eventPort = eventPort; this.cmd_tcpclient = new TcpClient(); this.event_tcpclient = new TcpClient(); this.Start_Connection(); } catch(Exception ex) { throw ex; } return this.cmd_tcpclient.Connected ? 1:0; } public int DisconnectToDemon(int Own_ID, int Demon_ID) { // send disconnect cmd // check disconneect cmd' response // stop rcv thread // return result Stop_Connection(); return 0; } public void SayHello_To_Board(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] bt; bt = pkt.CreatePKT_SayHello_To_Board(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } public void ReadBoardStatus(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] bt; bt = pkt.CreatePKT_ReadBoardStatus(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } public void WriteBoardStatus(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte RT_BaudRate,byte loop1_BaudRate,byte loop2_BaudRate,byte RT_Term, byte BD_Reset) { byte[] bt; bt = pkt.CreatePKT_WriteBoardStatus(loop, TBD_ID ,IVK_ID,RCV_ID, TBD_loop,RT_BaudRate,loop1_BaudRate,loop2_BaudRate,RT_Term, BD_Reset); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } public void SearchRepeaterAll(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] bt; bt = pkt.CreatePKT_SearchRepeaterAll(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } //Áß°è±â Á¤º¸Àбâ public void ReadRepeaterPointAll(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] bt; bt = pkt.CreatePKT_ReadRepeaterPointAll(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } public void WriteRepeaterOutputUNIT(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte Repeater_ID,byte Repeater_Out_Value) { byte[] bt; bt = pkt.CreatePKT_WriteRepeaterOutputUNIT(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop, Repeater_ID, Repeater_Out_Value); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } public void ReadRepeaterInfoAll(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] bt; bt = pkt.CreatePKT_ReadRepeaterInfoAll(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } public void WriteRepeaterInfoUNIT(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte Repeater_ID,byte Repeater_Out_Value) { byte[] bt; bt = pkt.CreatePKT_WriteRepeaterInfoUNIT(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop, Repeater_ID, Repeater_Out_Value); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } public void ResetRepeaterUNIT(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte Repeater_ID) { byte[] bt; bt = pkt.CreatePKT_ResetRepeaterUNIT(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop, Repeater_ID); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } public void ResetRepeaterAll(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] bt; bt = pkt.CreatePKT_ResetRepeaterAll(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } public void WriteRepeaterInputUNIT(byte loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte Repeater_ID,byte Repeater_Out_Value) { byte[] bt; bt = pkt.CreatePKT_WriteRepeaterInputUNIT(loop, TBD_ID, IVK_ID, RCV_ID, TBD_loop, Repeater_ID, Repeater_Out_Value); DoPrintDBGMSG(ToHex(bt)); Cmd_Send(bt,(int)bt[3] +1); } private CmdInfo cmd; // private int TimeOut = 3000; // private int cmd_send_time = 0; // private bool simulation_mode = false; // ----------------------- ¸í·ÉÀ» ¹Þ´ÂÇÔ¼ö ----------------------- public void getCommand(CmdInfo cmd) { try { // byte[] bt; if (cmd_tcpclient != null) { //if (cmd_tcpclient.Connected) //{ //this.TimeOut = cmd.TimeOut; // cmd_send_time = Environment.TickCount & Int32.MaxValue; switch (cmd.Command) { //#if (false) // case CMD_define.read_repeater_all : { // this.cmd = cmd; // ReadRepeaterPointAll((byte)cmd.loopNo,(byte)cmd.boardNo,(byte)cmd.invorkNo,(byte)cmd.receiverID); // if(simulation_mode) { // //°á°ú°ª ¸®ÅÏ----------------½ÃÀÛ // byte[] bt = new byte [1024]; // for(int i=0; i<1024 ; i++) { // bt[i] = (byte)i; // } // this.cmd.ResponseData = bt; // main.ui.getResponse(this.cmd); // //°á°ú°ª ¸®ÅÏ----------------³¡ // } // break; // } //#endif case CMD_define.say_hello: { this.cmd = cmd; SayHello_To_Board((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01); break; } case CMD_define.resd_tbd_state: { this.cmd = cmd; ReadBoardStatus((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01); break; } case CMD_define.write_tbd_state: { this.cmd = cmd; WriteBoardStatus((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00); break; } case CMD_define.search_repeater_all: { this.cmd = cmd; SearchRepeaterAll((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01); break; } case CMD_define.read_repeater_all: { this.cmd = cmd; ReadRepeaterPointAll((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01); ////°­Á¦·Î 0À¸·Î ¼ÂÆÃ---------------------------------------- //byte[] cmdbuffer = new byte[1200]; //for (int i = 0; i < 9; i++) //¼³Á¤ //{ // cmdbuffer[i] = 0x03; //} //for (int i = 0; i < 127; i++) //{ // cmdbuffer[(i * 3) + 9] = 0x00; //»óÅ // cmdbuffer[(i * 3) + 10] = 0x03; //ÀÔ·Â // cmdbuffer[(i * 3) + 11] = 0x14; //Ãâ·Â //} //RCVData_ReadRepeaterPointAll ResponseData = new RCVData_ReadRepeaterPointAll(); //ResponseData.Set_Data(cmdbuffer); //this.cmd.ResponseData = ResponseData; //main.ui.getResponse(this.cmd); break; } case CMD_define.write_repeater_output_unit: { this.cmd = cmd; //2010.3.24_moon WriteRepeaterOutputUNIT((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01, (byte)cmd.repeaterNo, 0x00); break; } case CMD_define.read_repeater_info_all: { this.cmd = cmd; ReadRepeaterInfoAll((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01); break; } case CMD_define.write_repeater_info_unit: { this.cmd = cmd; WriteRepeaterInfoUNIT((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01, (byte)cmd.repeaterNo, 0x00); break; } case CMD_define.reset_repeater_unit: { this.cmd = cmd; ResetRepeaterUNIT((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01, (byte)cmd.repeaterNo); break; } case CMD_define.reset_repeater_all: { this.cmd = cmd; ResetRepeaterAll((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01); break; } case CMD_define.write_repeater_input_unit: { this.cmd = cmd; WriteRepeaterInputUNIT((byte)cmd.loopNo, (byte)cmd.boardNo, (byte)cmd.invorkNo, (byte)cmd.receiverID, 0x01, (byte)cmd.repeaterNo, 0x00); break; } default: { break; } } } //} } catch(Exception ex) { throw ex; } //string str; //str = ToHex(bt); //DoPrintDBGMSG(str); //Send(bt, 7); } // ----------------------- ¸í·ÉÀ» ¹Þ´ÂÇÔ¼ö ----------------------- // ----------------------- ¸íÇöÀç ¿¬°áµÈ ¼ÒÄϼö ----------------------- public int TcpSockeCnt { get { int TcpCnt = 0; // if (this.cmd_tcpclient != null) { if (this.cmd_tcpclient.Connected) ++TcpCnt; } if (this.event_tcpclient != null) { if (this.event_tcpclient.Connected) ++TcpCnt; } return TcpCnt; } } public Boolean cmd_Connected { get { Boolean TcpConnected = false; try { if (this.cmd_tcpclient != null) TcpConnected = this.cmd_tcpclient.Connected; } catch { } return TcpConnected; } } public Boolean event_Connected { get { Boolean TcpConnected = false; try { if (this.event_tcpclient != null) TcpConnected = this.event_tcpclient.Connected; } catch { } return TcpConnected; } } // ----------------------- ¸íÇöÀç ¿¬°áµÈ ¼ÒÄϼö ----------------------- public int SendDataToDemon(byte[] data , int size) {// don't using now. //2010.3.16_moon //if conencted // send msg // check response return 0; } public int SendDataToDemon(String strdata, int size) {// don't using now. //2010.3.16_moon //if conencted // send msg // check response return 0; } string ToHex(byte[] bin_data) { string result = ""; foreach (byte ch in bin_data) { result += string.Format("{0:x2} ", ch); } return result; } string ToHex(byte[] bin_data,int len) { string result = ""; for(int i=0; i retry // or time out this.cmd_state = 1; // result setting. // DoPrintDBGMSG(this.cmd_state.ToString()); // send result to main. } } } void Cmd_ReceiveThread() { byte[] ReadBuffer = new byte[1024]; int offset = 0; while(this.cmd_rcv_th_state == 1) { if(this.cmd_tcpclient.Connected) { DoPrintDBGMSG("ReceiveThread"); for(int i=0; i<1024; i++) { ReadBuffer[i] = 0x00; } offset = 0; try { if(this.cmd_recvStm.CanRead) { offset = this.cmd_recvStm.Read(ReadBuffer, 0, ReadBuffer.Length); if(offset>0) { DoPrintDBGMSG(ToHex(ReadBuffer,offset)); // rcv msg process // if rcv msg ok -> send cmd handling thread // else if rcv msg short -> wait next msg // else if rcv msg error -> sned error handling thread ( header error , cs error, CMDReceiveMSGProc(ReadBuffer,offset); } } this.cmd_recvStm.Flush(); } catch (Exception e) { //error DoPrintDBGMSG("error : " + e.Message); } } else { // don't connected break; } } this.cmd_rcv_th_state = 0; } byte[] CMD_RcvBuffer = new byte [2048]; int CMD_RcvBufferPos = 0; void CMDReceiveMSGProc(byte[] ReadBuffer, int size) { DoPrintDBGMSG("ReceiveMSGProcess"); for(int i=0; i<2048 ; i++) {//init buffer CMD_RcvBuffer[i] = 0x00; } CMD_RcvBufferPos = 0; for(int i=0; i current_time) { //time out DoPrintDBGMSG("ReceiveMSGProcess to "); for(int i=0; i<2048 ; i++) {//init buffer RcvBuffer[i] = 0x00; } RcvBufferPos = 0; } else { DoPrintDBGMSG("ReceiveMSGProcess 2"); for(int i=0; i RcvBufferPos) {//need more data DoPrintDBGMSG("ReceiveMSGProcess 7 "); } else { DoPrintDBGMSG("ReceiveMSGProcess 8"); int datasize = RcvBuffer[3]; CMDResponceProcess(datasize); switch(RcvBuffer[4]) { case 0x94: { break; } default : { break; } } } } } /* switch(ReadBuffer[4]) { case 0x94: { //°á°ú°ª ¸®ÅÏ----------------½ÃÀÛ byte[] bt = new byte [size]; for(int i=0; i bd no , loop no , repeater id. public int TBD_ID = 0; public int TBD_loop = 1; public int repeater_loop = 0; public int repeater_ID = 0; public int ReceiverID = 0; //¼ö½Å±âID public int InvorkNo = 0; //¸í·ÉID // repeater state // public int Err = 0; public int Non = 0; public int R0 = 0; public int R1 = 0; public int R2 = 0; public int R = 0; public int W = 0; public int S = 0; // repeater input // public int repeater_state_1 = 0; public int repeater_state_2 = 0; public int repeater_state_3 = 0; public int repeater_state_4 = 0; //repeater output // public int repeater_output_4_C = 0; public int repeater_output_3_C = 0; public int repeater_output_2_C = 0; public int repeater_output_1_C = 0; public int repeater_output_4_W = 0; public int repeater_output_3_W = 0; public int repeater_output_2_W = 0; public int repeater_output_1_W = 0; public void Set_Data(byte TBD_ID,byte TBD_loop,byte repeater_loop,byte repeater_ID,byte InvorkNo,byte ReceiverID,byte state,byte input,byte output) { this.TBD_ID = TBD_ID; this.TBD_loop = TBD_loop; this.repeater_loop = repeater_loop; this.repeater_ID = repeater_ID; this.InvorkNo = InvorkNo; this.ReceiverID = ReceiverID; set_state_data(state); set_input_data(input); set_output_data(output); } public void set_state_data(byte in_data){ Err = (in_data >> 7) & 0x01; Non = (in_data >> 6) & 0x01; R0 = (in_data >> 5) & 0x01; R1 = (in_data >> 4) & 0x01; R2 = (in_data >> 3) & 0x01; R = (in_data >> 2) & 0x01; W = (in_data >> 1) & 0x01; S = (in_data >> 0) & 0x01; } public void set_input_data(byte in_data){ repeater_state_4 = (in_data >> 6) & 0x03; repeater_state_3 = (in_data >> 4) & 0x03; repeater_state_2 = (in_data >> 2) & 0x03; repeater_state_1 = (in_data >> 0) & 0x03; } public void set_output_data(byte in_data){ repeater_output_4_C = (in_data >> 7) & 0x01; repeater_output_3_C = (in_data >> 6) & 0x01; repeater_output_2_C = (in_data >> 5) & 0x01; repeater_output_1_C = (in_data >> 4) & 0x01; repeater_output_4_W = (in_data >> 3) & 0x01; repeater_output_3_W = (in_data >> 2) & 0x01; repeater_output_2_W = (in_data >> 1) & 0x01; repeater_output_1_W = (in_data >> 0) & 0x01; } } public class res_data_header { public CMD_define cmd_define; public int ReceiverID = 0; //¼ö½Å±âID public int LoopNo = 0; //LoopNo public int BoardNo = 0; //BoardNo public int RepeaterNo = 0; //Áß°è±âNo public int InvorkNo = 0; //¸í·ÉID public res_data_repeater[] data; public int[] temp_data = new int[1024]; public res_data_header(byte[] RcvData, int size) { } } void Event_Send(byte[] bt, int size) { //2010.3.17_moon //don't using if( this.event_tcpclient != null){ if(this.event_tcpclient.Connected) { try { this.event_sendStm.Write(bt, 0, size); this.event_sendStm.Flush(); } catch { return; } } } } void Event_ReceiveThread() { byte[] ReadBuffer = new byte[1024]; int offset = 0; while(this.event_rcv_th_state == 1) { if(this.event_tcpclient.Connected) { // DoPrintDBGMSG("Event_ReceiveThread"); try { if(this.event_recvStm.CanRead) { for(int i=0; i<1024; i++) { ReadBuffer[i] = 0x00; } offset = this.event_recvStm.Read(ReadBuffer, 0, ReadBuffer.Length); if(offset>0) { string str; str = ToHex(ReadBuffer,offset); // DoPrintDBGMSG("size : " + offset.ToString() + "//" +str); // DoPrintDBGMSG(string.Format("{0:x2} ", ReadBuffer[offset-1])); //Thread.Sleep(1000); EVENTReceiveMSGFunc(ReadBuffer,offset); } } this.event_recvStm.Flush(); // call main func -> case by case } catch (Exception e) { //error DoPrintDBGMSG(e.Message); } } else { break; } } this.event_rcv_th_state = 0; } byte[] EVENT_RcvBuffer = new byte [1024*3]; int EVENT_RcvBufferPos = 0; void EVENTReceiveMSGFunc(byte[] ReadBuffer, int size) { DoPrintDBGMSG("+ EVENTReceiveMSGProc"); if((EVENT_RcvBufferPos <0)&&(EVENT_RcvBufferPos >2048)) { DoPrintDBGMSG("///////////////////ERROR////////////////"); for(int i=0; i<2048 ; i++) {//init buffer EVENT_RcvBuffer[i] = 0x00; } EVENT_RcvBufferPos = 0; } DoPrintDBGMSG("EVENTReceiveMSGProc EVENT_RcvBufferPos = "+ EVENT_RcvBufferPos.ToString()); DoPrintDBGMSG("EVENTReceiveMSGProc size = "+ size.ToString()); for(int i=0; i EVENT_RcvBufferPos) {//short , next DoPrintDBGMSG("EVENTReceiveMSGProc:3 "); break; } else {//processing DoPrintDBGMSG("EVENTReceiveMSGProc:4 "); EVENTCallFunc(extendsize); } } else if(EVENT_RcvBuffer[3]+1 > EVENT_RcvBufferPos) {//short , next DoPrintDBGMSG("EVENTReceiveMSGProc:5 "); break; } else {//processing DoPrintDBGMSG("EVENTReceiveMSGProc:6 " + EVENT_RcvBuffer[3].ToString()); EVENTCallFunc(EVENT_RcvBuffer[3]); } } else { // header error DoPrintDBGMSG("EVENTReceiveMSGProc:7 "); for(int i=0; i<1024*3 ; i++) {//init buffer EVENT_RcvBuffer[i] = 0x00; } EVENT_RcvBufferPos = 0; break; } // int datasize = EVENT_RcvBuffer[3]; // EVENTCallFunc(datasize); } // Thread.Sleep(1000); DoPrintDBGMSG("- EVENTReceiveMSGProc"); } void EVENTCallFunc(int size) { DoPrintDBGMSG ( " + EVENTCallFunc " ); byte[] cmdbuffer = new byte[size+1]; for(int i=0; i rawdatas.Length ) return null; IntPtr buffer = Marshal.AllocHGlobal( rawsize ); Marshal.Copy( rawdatas, 0, buffer, rawsize ); ret_obj = Marshal.PtrToStructure( buffer, anytype ); Marshal.FreeHGlobal( buffer ); return ret_obj; } byte DoCalcs(byte[] bt, int size) { byte ret_byte = 0x00; int counter = 0; do { ret_byte = (byte) (ret_byte ^ bt[counter]); ret_byte++; counter++; } while(counter < size); return ret_byte; } public int DoCheckcs(byte[] bt, int size,byte cs) { int ret = 0; byte calcs = 0x00; calcs = DoCalcs(bt,size); if(cs == calcs) { ret = 1; } else { ret = 0; } return ret; } public byte[] CreatePKT_SayHello_To_Board(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] ret_byte_data; PRTReceiver_Type1 PKT = new PRTReceiver_Type1(); int psize = (byte)default_value_define.size_PRTReceiver_Type1; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.say_hello; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_ReadBoardStatus(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] ret_byte_data; PRTReceiver_Type1 PKT = new PRTReceiver_Type1(); int psize = (byte)default_value_define.size_PRTReceiver_Type1; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.resd_tbd_state; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_WriteBoardStatus(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte RT_BaudRate,byte loop1_BaudRate,byte loop2_BaudRate,byte RT_Term, byte BD_Reset) { //2010.3.24_moon byte[] ret_byte_data; PRTReceiver_Type6 PKT = new PRTReceiver_Type6(); int psize = (byte)default_value_define.size_PRTReceiver_Type6; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.write_tbd_state; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.Option1 = RT_BaudRate; PKT.Option2 = loop1_BaudRate; PKT.Option3 = loop2_BaudRate; PKT.Option4 = RT_Term; PKT.Option5 = BD_Reset; PKT.TBD_loop = TBD_loop; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_SearchRepeaterAll(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] ret_byte_data; PRTReceiver_Type3 PKT = new PRTReceiver_Type3(); int psize = (byte)default_value_define.size_PRTReceiver_Type3; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.search_repeater_all; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; PKT.Option1 = 0xff; //2010.3.24_moon All PKT.Option2 = 0x00; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_ReadRepeaterPointAll(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] ret_byte_data; PRTReceiver_Type3 PKT = new PRTReceiver_Type3(); int psize = (byte)default_value_define.size_PRTReceiver_Type3; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.read_repeater_all; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; PKT.Option1 = 0xff; //2010.3.24_moon All PKT.Option2 = 0x00; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_WriteRepeaterOutputUNIT(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte Repeater_ID,byte Repeater_Out_Value) { byte[] ret_byte_data; PRTReceiver_Type3 PKT = new PRTReceiver_Type3(); int psize = (byte)default_value_define.size_PRTReceiver_Type3; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.write_repeater_output_unit; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; PKT.Option1 = Repeater_ID; PKT.Option2 = Repeater_Out_Value; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_ReadRepeaterInfoAll(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] ret_byte_data; PRTReceiver_Type3 PKT = new PRTReceiver_Type3(); int psize = (byte)default_value_define.size_PRTReceiver_Type3; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.read_repeater_info_all; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; PKT.Option1 = 0xff; //2010.3.24_moon All PKT.Option2 = 0x00; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_WriteRepeaterInfoUNIT(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte Repeater_ID,byte Repeater_Out_Value) { byte[] ret_byte_data; PRTReceiver_Type3 PKT = new PRTReceiver_Type3(); int psize = (byte)default_value_define.size_PRTReceiver_Type3; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.write_repeater_info_unit; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; PKT.Option1 = Repeater_ID; PKT.Option2 = Repeater_Out_Value; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_ResetRepeaterUNIT(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte Repeater_ID) { byte[] ret_byte_data; PRTReceiver_Type2 PKT = new PRTReceiver_Type2(); int psize = (byte)default_value_define.size_PRTReceiver_Type2; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.reset_repeater_unit; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; PKT.Option1 = Repeater_ID; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_ResetRepeaterAll(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop) { byte[] ret_byte_data; PRTReceiver_Type3 PKT = new PRTReceiver_Type3(); int psize = (byte)default_value_define.size_PRTReceiver_Type3; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.reset_repeater_all; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; PKT.Option1 = 0xff; //2010.3.24_moon All PKT.Option2 = 0x00; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public byte[] CreatePKT_WriteRepeaterInputUNIT(byte RPT_loop, byte TBD_ID ,byte IVK_ID,byte RCV_ID,byte TBD_loop,byte Repeater_ID,byte Repeater_Out_Value) { byte[] ret_byte_data; PRTReceiver_Type3 PKT = new PRTReceiver_Type3(); int psize = (byte)default_value_define.size_PRTReceiver_Type3; PKT.head = (byte)default_value_define.prt_header_id; PKT.RPT_loop = RPT_loop; PKT.TBD_ID = TBD_ID; PKT.length = (byte)psize; PKT.cmd = (byte)CMD_define.write_repeater_input_unit; PKT.IVK_ID = IVK_ID; PKT.RCVR_ID = RCV_ID; PKT.TBD_loop = TBD_loop; PKT.Option1 = Repeater_ID; PKT.Option2 = Repeater_Out_Value; ret_byte_data = RawSerialize(PKT); ret_byte_data[psize]= DoCalcs(ret_byte_data,psize); return ret_byte_data; } public void DecPacket(byte [] data , int size) { // check cs // make upload data // } } }