Ejemplo n.º 1
0
        /// <summary>
        /// 스레드를 2번 실행시켜 join할때 메인 스레드를 멈추지 않도록 한다
        /// </summary>
        /// <param name="parameter"></param>
        public void getMachineDataAndDisplayFromClient(DataDisplayParameter parameter)
        {
            //ThreadPool.QueueUserWorkItem(ThreadStart, parameter);
            Thread tcpThread = new Thread(new ParameterizedThreadStart(ThreadStart));

            tcpThread.IsBackground = true;
            tcpThread.Start(parameter);
        }
Ejemplo n.º 2
0
 public static void showMachineDataFromClient(DataDisplayParameter parameter)
 {
     try
     {
         PcTcpClient client = new PcTcpClient(ServerIpEnd);
         client.getMachineDataAndDisplayFromClient(parameter);
     }
     catch
     {
     }
 }
Ejemplo n.º 3
0
        public Dictionary <string, string>[] getMultipleMachineData(string[] machineCodes, string[] fieldCodes)
        {
            DataDisplayParameter parameter = new DataDisplayParameter();

            parameter.MachineCodes = machineCodes;
            parameter.FieldCodes   = fieldCodes;

            Thread tcpThread = new Thread(new ParameterizedThreadStart(DoAll));

            tcpThread.IsBackground = true;
            tcpThread.Start(parameter);
            tcpThread.Join(500);

            return(DataResponse);
        }
Ejemplo n.º 4
0
        private void ThreadStart(object obj)
        {
            Thread tcpThread = new Thread(new ParameterizedThreadStart(ThreadWorkFromClient));

            tcpThread.IsBackground = true;
            tcpThread.Start(obj);


            if (tcpThread.Join(3000) == false)
            {
                tcpResponseData.Exception = (new Exception("Connection Timeout"));
            }

            DataDisplayParameter parameter = obj as DataDisplayParameter;

            parameter.Callback(this.tcpResponseData);
        }
Ejemplo n.º 5
0
        public static void showMachineDataFromServer(DataDisplayParameter parameter)
        {
            string[]            machineCodes    = parameter.MachineCodes;
            string[]            fieldCodes      = parameter.FieldCodes;
            DataDisplayCallback displayCallback = parameter.Callback;

            TcpResponse tcpResponse = new TcpResponse();

            MachineResponseData [] responseDatas = new MachineResponseData[machineCodes.Length];

            for (int i = 0; i < machineCodes.Length; i++)
            {
                responseDatas[i] = new MachineResponseData();
                Dictionary <string, string> codesAndvalues = new Dictionary <string, string>();
                string machineCode = machineCodes[i];
                if (EntireMachineData.ContainsKey(machineCode))
                {
                    Dictionary <string, string> singleMachineData = EntireMachineData[machineCode];
                    for (int j = 0; j < fieldCodes.Length; j++)
                    {
                        if (singleMachineData.ContainsKey(fieldCodes[j]))
                        {
                            codesAndvalues[fieldCodes[j]] = EntireMachineData[machineCode][fieldCodes[j]];
                        }
                        else
                        {
                            codesAndvalues[fieldCodes[j]] = string.Empty;
                        }
                    }
                    responseDatas[i].MachineCode = machineCode;
                    responseDatas[i].MachineData = codesAndvalues;
                }
                responseDatas[i].IsConnected = GetMachineConnectionStateByMachineCode(machineCode);
            }
            tcpResponse.MachineResponses = responseDatas;

            displayCallback(tcpResponse);
        }
Ejemplo n.º 6
0
        /*
         * 콜백 메소드 적용 스레드 구현중
         * */
        public void DoAll(object obj)
        {
            DataDisplayParameter parameter = obj as DataDisplayParameter;

            string[] machineCodes = parameter.MachineCodes;
            string[] fieldCodes   = parameter.FieldCodes;

            try
            {
                DataResponse = new Dictionary <string, string> [machineCodes.Length];
                for (int i = 0; i < machineCodes.Length; i++)
                {
                    ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                    ClientSocket.Connect(ServerIpEnd);


                    byte[] requestPacket = makeRequestPacket(machineCodes[i], fieldCodes);
                    ClientSocket.Send(requestPacket, SocketFlags.None);

                    byte[] receivedData = new byte[4096];
                    int    dataLength   = ClientSocket.Receive(receivedData, SocketFlags.None);

                    if (dataLength > 0)
                    {
                        if (receivedData[0] == 120)
                        {
                            //check Checksum
                            int checksum = 0;
                            for (int j = 0; j < dataLength - 2; j++)
                            {
                                checksum += receivedData[j];
                            }
                            int packetCheckSum = receivedData[dataLength - 2] * 128 + receivedData[dataLength - 1];

                            if (checksum == packetCheckSum)
                            {
                                //parse machineCode
                                bool isConnnected = receivedData[1] == 1 ? true : false;

                                int    machineBytesLength = receivedData[2] * 128 + receivedData[3];
                                byte[] machineBytes       = new byte[machineBytesLength];
                                Array.Copy(receivedData, 4, machineBytes, 0, machineBytesLength);

                                int    fieldBytesLength = receivedData[machineBytesLength + 4] * 128 + receivedData[machineBytesLength + 5];
                                byte[] fieldBytes       = new byte[fieldBytesLength];
                                Array.Copy(receivedData, machineBytesLength + 6, fieldBytes, 0, fieldBytesLength);

                                //getValues from modbus then send back value data
                                string value = Encoding.UTF8.GetString(fieldBytes);

                                string[] values = value.Split('=');

                                Dictionary <string, string> codesAndValues = new Dictionary <string, string>();
                                for (int j = 0; j < fieldCodes.Length; j++)
                                {
                                    codesAndValues[fieldCodes[j]] = values[j];
                                }
                                DataResponse[i] = codesAndValues;
                            }
                        }
                    }
                    ClientSocket.Disconnect(true);
                }// end of for(;;)
            }
            catch (Exception ex)
            {
                CaughtException = ex;
            }
            finally
            {
            }
        }
Ejemplo n.º 7
0
        /*
         * 콜백 메소드 적용 쓰레드 구현중
         * */



        private void ThreadWorkFromClient(object obj)
        {
            DataDisplayParameter parameter = obj as DataDisplayParameter;

            string[]            machineCodes    = parameter.MachineCodes;
            string[]            fieldCodes      = parameter.FieldCodes;
            DataDisplayCallback displayCallback = parameter.Callback;

            this.tcpResponseData = new TcpResponse();
            MachineResponseData[] responseDatas = new MachineResponseData[machineCodes.Length];
            try
            {
                DataResponse = new Dictionary <string, string> [machineCodes.Length];
                for (int i = 0; i < machineCodes.Length; i++)
                {
                    responseDatas[i] = new MachineResponseData();

                    ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                    ClientSocket.Connect(ServerIpEnd);

                    byte[] requestPacket = makeRequestPacket(machineCodes[i], fieldCodes);
                    ClientSocket.Send(requestPacket, SocketFlags.None);

                    byte[] receivedData = new byte[4096];
                    int    dataLength   = ClientSocket.Receive(receivedData, SocketFlags.None);

                    if (dataLength > 0)
                    {
                        if (receivedData[0] == 120)
                        {
                            //check Checksum
                            int checksum = 0;
                            for (int j = 0; j < dataLength - 2; j++)
                            {
                                checksum += receivedData[j];
                            }
                            int packetCheckSum = receivedData[dataLength - 2] * 128 + receivedData[dataLength - 1];

                            //checksum error 발생
                            //if (checksum == packetCheckSum)
                            if (true)
                            {
                                //parse machineCode
                                bool isConnnected = receivedData[1] == 1 ? true : false;

                                int    machineBytesLength = receivedData[2] * 128 + receivedData[3];
                                byte[] machineBytes       = new byte[machineBytesLength];
                                Array.Copy(receivedData, 4, machineBytes, 0, machineBytesLength);

                                int    fieldBytesLength = receivedData[machineBytesLength + 4] * 128 + receivedData[machineBytesLength + 5];
                                byte[] fieldBytes       = new byte[fieldBytesLength];
                                Array.Copy(receivedData, machineBytesLength + 6, fieldBytes, 0, fieldBytesLength);

                                //getValues from modbus then send back value data
                                string   machineCode = Encoding.UTF8.GetString(machineBytes);
                                string   value       = Encoding.UTF8.GetString(fieldBytes);
                                string[] values      = value.Split('=');

                                Dictionary <string, string> codesAndValues = new Dictionary <string, string>();
                                for (int j = 0; j < fieldCodes.Length; j++)
                                {
                                    codesAndValues[fieldCodes[j]] = values[j];
                                }
                                responseDatas[i].MachineCode = machineCode;
                                responseDatas[i].MachineData = codesAndValues;
                                responseDatas[i].IsConnected = isConnnected;
                            }
                        }
                        else
                        {
                            responseDatas[i].Message = "수신 데이터가 없습니다";
                        }
                    }
                    ClientSocket.Disconnect(true);
                }

                tcpResponseData.MachineResponses = responseDatas;
            }
            catch (Exception ex)
            {
                CaughtException           = ex;
                tcpResponseData.Exception = ex;
            }
        }