Exemplo n.º 1
0
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                SerialPort serialPort   = (SerialPort)sender;
                string     dataReceived = serialPort.ReadExisting();

                string[] buffer = dataReceived?.Split(';');
                if (buffer != null && buffer.Length > 0)
                {
                    UartTelegram telegram = new UartTelegram(buffer);
                    IMsgUart     msg      = MsgUartFactory.CreateMsg(telegram.Header.ID);

                    PropertyInfo[] propsInfo = msg.GetType().GetProperties();
                    if ((propsInfo != null) && (propsInfo.Length == telegram.Data.Length))
                    {
                        for (int i = 0; i < propsInfo.Length; i++)
                        {
                            var converter = TypeDescriptor.GetConverter(propsInfo[i].PropertyType);
                            var result    = converter.ConvertFrom(telegram.Data[i]);
                            propsInfo[i].SetValue(msg, result);
                        }

                        Program.UartService.TreatMessage(msg);
                    }
                }
            }
            catch (Exception exc)
            {
            }
        }
Exemplo n.º 2
0
        internal static int GetID(this IMsgUart self)
        {
            string className       = self.GetType().Name;
            int    firstDigitIndex = className.IndexOfAny("0123456789".ToCharArray());

            if (firstDigitIndex < 0)
            {
                throw new IndexOutOfRangeException("Nome da classe com tamanho inválido.");
            }
            return(int.Parse(className.Substring(firstDigitIndex)));
        }
Exemplo n.º 3
0
        internal static string ToString(this IMsgUart self)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("[{0}] ->", self.GetID());

            PropertyInfo[] propsInfo = self.GetType().GetProperties();
            if ((propsInfo != null) && (propsInfo.Length > 0))
            {
                foreach (PropertyInfo prop in propsInfo)
                {
                    sb.AppendFormat(" {0}:{1}", prop.Name, prop.GetValue(self, null));
                }
            }

            return(sb.ToString());
        }
 public void WriteRegisters(IMsgUart msg)
 {
     try
     {
         short startAddress = GetStartAddress(msg);
         if (startAddress != -1)
         {
             var propsInfo = msg.GetType().GetProperties();
             if ((propsInfo != null) && (propsInfo.Length > 0))
             {
                 for (int i = 0; i < propsInfo.Length; i++)
                 {
                     ushort data = Convert.ToUInt16(propsInfo[i].GetValue(msg, null));
                     _Master.WriteSingleRegister(_SlaveId, (ushort)(startAddress + i), data);
                 }
             }
         }
     }
     catch (Exception e)
     {
     }
 }
Exemplo n.º 5
0
        public void SendDataHandler(IMsgUart msg)
        {
            try
            {
                UartHeader header = null;
                string[]   data   = null;

                PropertyInfo[] propsInfo = msg.GetType().GetProperties();
                if ((propsInfo != null) && (propsInfo.Length > 0))
                {
                    header = new UartHeader
                    {
                        Size = propsInfo.Length,
                        ID   = msg.GetID()
                    };

                    data = new string[propsInfo.Length];
                    for (int i = 0; i < propsInfo.Length; i++)
                    {
                        data[i] = propsInfo[i].GetValue(msg, null).ToString();
                    }
                }

                if (header != null)
                {
                    UartTelegram telegram = new UartTelegram(header, data);
                    _SerialPort.WriteLine(string.Join(";", telegram.Buffer));
                }
                else
                {
                    throw new Exception("Impossível criar telegrama sem cabeçalho.");
                }
            }
            catch (Exception exc)
            {
            }
        }