Ejemplo n.º 1
0
        public static byte[] StructureToByteArray(DataEntry_t[] val)
        {
            //int len = Marshal.SizeOf(_dataentry_t_size);
            int len = _dataentry_t_size;
            byte [] arr = new byte[(val.Length*len)];

            for (int i = 0; i< val.Length; i++) {
                byte[] ba = SingleStructureToByteArray(val[i]);
                System.Buffer.BlockCopy( ba, 0, arr, i*len, len );
            }
            /*
            int len = Marshal.SizeOf(obj);

            byte [] arr = new byte[len];

            IntPtr ptr = Marshal.AllocHGlobal(len);

            Marshal.StructureToPtr(obj, ptr, true);

            Marshal.Copy(ptr, arr, 0, len);

            Marshal.FreeHGlobal(ptr);
            */

            return arr;
        }
Ejemplo n.º 2
0
        public void myReadCallBack(IAsyncResult ar)
        {
            /*
            try
            {
                NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;
                //byte[] myReadBuffer = new byte[1024];
                String myCompleteMessage = "";
                int numberOfBytesRead;

                numberOfBytesRead = myNetworkStream.EndRead(ar);
                myCompleteMessage = String.Concat(myCompleteMessage, Encoding.UTF8.GetString(myReadBuffer, 0, numberOfBytesRead));

                myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length,
                                                           new AsyncCallback(myReadCallBack),
                                                           myNetworkStream);

                _messages.Add(myCompleteMessage);
                // Print out the received message to the console.
                Console.WriteLine("You received the following message(" + myCompleteMessage.Length + ") : " + myCompleteMessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR CLIENT MYREADCALLBACK:" + ex.Message);
            }
            */

            byte[] values = new byte[_dataentry_t_size];
            try
            {
                NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState;
                String myCompleteMessage = "";
                int numberOfBytesRead;

                numberOfBytesRead = myNetworkStream.EndRead(ar);

                //myCompleteMessage = String.Concat(myCompleteMessage, Encoding.UTF8.GetString(myReadBuffer, 0, numberOfBytesRead));
                myCompleteMessage = String.Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
                byte[] bytes = Utils.StringToByteArray(myCompleteMessage);
                //byte hb = (byte)_serialPort.ReadByte();
                //byte lb = (byte)_serialPort.ReadByte();

                //int count = (hb << 8) | lb;
                int count = numberOfBytesRead / _dataentry_t_size;
                DataEntry_t[] valuesArray = new DataEntry_t[count];

                for (int k = 0; k < count; k++)
                {
                    for (int i = 0; i < _dataentry_t_size; i++)
                    {
                        values[i] = (byte)bytes[(k * _dataentry_t_size) + i];
                    }
                    DataEntry_t de = Utils.ByteArrayToNewStuff(values);
                    if (data.ContainsKey(de.id))
                    {
                        DataEntry_t de_update;
                        if (data.TryGetValue(de.id, out de_update))
                        {
                            de_update.value = de.value;
                            data[de.id] = de_update;
                        }
                        else
                            Console.WriteLine("ERROR: DATAIO Read: UpdateValue:" + de.id + "___count:" + count);
                    }
                    else
                    {
                        data.Add(de.id, de);
                    }
                }

                myNetworkStream.BeginRead(myReadBuffer, 0, myReadBuffer.Length,
                                          new AsyncCallback(myReadCallBack),
                                          myNetworkStream);

                int j = 0;
                j++;
                j--;
            }
            catch (TimeoutException) { }
        }