Example #1
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);
        }
Example #2
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;
            }
        }