コード例 #1
0
        private void ProcessReceivedData(object networkConnectionParameter)
        {
            lock (_lockProcessReceivedData){
                Byte[] bytes = new byte[((TCPHandler.NetworkConnectionParameter)networkConnectionParameter).Bytes.Length];
                Array.Copy(((TCPHandler.NetworkConnectionParameter)networkConnectionParameter).Bytes, 0, bytes, 0, ((TCPHandler.NetworkConnectionParameter)networkConnectionParameter).Bytes.Length);

                _ui.Log("Yeni TCP istek -> " + BitConverter.ToString(bytes).Replace("-", " ") + " | cihaz: " + bytes[6] + " - komut:" + bytes[7]);

                Byte[] tcpHeader = new byte[4];

                int restLength = bytes.Length - 4;

                Byte[] theRest = new byte[restLength];

                try{
                    for (int i = 0; i < 4; i++)
                    {
                        tcpHeader[i] = bytes[i];
                    }

                    for (int i = 0; i < restLength; i++)
                    {
                        if (i + 6 > bytes.Length - 1)
                        {
                            continue;
                        }

                        theRest[i] = bytes[6 + i];
                    }

                    _serialHandler.SendRequest(theRest, (data) => {
                        byte[] response = new byte[data.Length + 4];

                        for (int i = 0; i < 4; i++)
                        {
                            response[i] = tcpHeader[i];
                        }

                        int len = data.Length - 2;

                        Byte[] responseLength = BitConverter.GetBytes(len).Reverse().ToArray();

                        response[4] = responseLength[2];
                        response[5] = responseLength[3];

                        for (int i = 0; i < len; i++)
                        {
                            response[6 + i] = data[i];
                        }

                        NetworkStream stream = ((TCPHandler.NetworkConnectionParameter)networkConnectionParameter).Stream;

                        _ui.Log("COM cevap -> " + BitConverter.ToString(bytes).Replace("-", " ") + " | cihaz: " + bytes[6] + " -  komut:" + bytes[7]);

                        if (stream.CanWrite)
                        {
                            stream.Write(response, 0, response.Length);
                        }
                    });
                }
                catch (Exception e) {
                }
            }
        }