Perdido en la recepción de datos. A menudo recibo vuestras preguntas pidiéndome si os puedo aportar alguna idea para resolver casuísticas concretas. Lamentablemente por falta de tiempo no siempre puedo dedicar un post a cada una de vuestras inquietudes, sin embargo en ocasiones se plantean situaciones que se repiten y en caso de resolverlas considero que pueden ayudar a más personas con la misma inquietud y creo que este es el caso : Pedro me escribe con lo siguiente: <Estoy encallado en un punto del cual me está costando salir… tengo un hardware especifico por el que el puerto serie me envía una trama conteniendo la información de varios valores separados por unos delimitadores> La pregunta es: Como puedo detectar cuando recibo los indicadores, como distingo la letra que identifica el dato y finalmente como puedo leer únicamente su valor y colocarlo en un textbox. |
I’m lost receiving RS232 data. I often get questions asking me if I can contribute with any idea to solve concrete cases. Unfortunately due to lack of time I cannot always devote a post to each of your concerns, however sometimes there are situations that are repeated in case of solving them believe that they can help more people with the same concern and provably that may be the case: Peter write me with the next question : < I am aground at a point which is costing me leave; I have an specific hardware with a serial port, that port sends me a frame containing information from several values separated by a few delimiters>
The question is: How I can detect the indicators positions, and how I can distinguish the letter that identifies the data and finally how can read only its value and send this to a textbox.
|
Visual basic =
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim frame = "{W111}{M22}{F33}{S44}" Dim separators As Char() = {"{", "W", "M", "S", "F", "}"} Dim fields As String() = (From f As String In frame.Split(separators) Where f.Length > 0).ToArray Dim textBoxesNames() = (From Tbn As Object In Me.Controls Where Tbn.GetType.Name = "TextBox" Select CType(Tbn, TextBox).Name Order By Name).ToArray For Index As Integer = 0 To textBoxesNames.Count - 1 Me.Controls.Item(textBoxesNames(Index)).Text = fields(Index) Next End Sub End Class
C# =
using System; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string frame = "{W111}{M22}{F33}{S44}"; char[] separators = new char[] { '{', 'W', 'M', 'S', 'F', '}' }; string[] fields = (from string f in frame.Split(separators) where f.Length > 0 select f).ToArray(); string[] textBoxesNames = (from Tbn in this.Controls.OfType<TextBox>() where Tbn.GetType().Name == "TextBox" orderby Tbn.Name select Tbn.Name).ToArray(); for (int Index = 0; Index < textBoxesNames.Count(); Index++) { this.Controls[textBoxesNames[Index]].Text = fields[Index]; } } } }
Regards,
PepLluis,
¡Muchas gracias!
Acabo de probarlo y me será de mucha ayuda. En cuanto lo tenga listo lo comentaré.
Saludos,