Ver Mensaje Individual
  #2  
Antiguo 16-11-2023
tsk tsk is offline
Miembro
 
Registrado: dic 2017
Posts: 52
Reputación: 7
tsk Va por buen camino
Supongo que la trama viene después de enviar un comando, y dado que conoces dicho comando podrías usar un diccionario para aplicarle a la trama recibida el tratamiento correspondiente.

Aquí te muestro dos formas en que lo podrías hacer. En la primera tienes funciones específicas para cada comando y, en la segunda, tienes una sola función para todos los comandos.

Código PHP:
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public class 
Dict
{
    public static 
void Main()
    {
        
IDictionary<string,Func<string,string[]>> d0 = new Dictionary<string,Func<string,string[]>>();
        
IDictionary<string,stringd1 = new Dictionary<string,string>();

        
d0.Add("Comando1",func1);
        
d0.Add("Comando2",func2);

        
d1.Add("Comando1","Patron 1");
        
d1.Add("Comando2","Patron 2");

        
d0["Comando1"]("Frame 1");
        
d0["Comando2"]("Frame 2");

        
func3("Frame 1"d1["Comando1"]);
        
func3("Frame 2"d1["Comando2"]);
    }

    private static 
string[] func1(string frame)
    {
        
Console.WriteLine("Entra a func1 y el frame es {0}",frame);
        return new [] {
"0001","00002"};
    }

    private static 
string[] func2(string frame)
    {
        
Console.WriteLine("Entra a func2 y el frame es {0}",frame);
        return new [] {
"0001","00002"};
    }

    private static 
string[] func3(string framestring pattern)
    {
        
Console.WriteLine("Entra a func3 y el frame es {0} y este es el patron {1}",frame,pattern);
        return new [] {
"0001","00002"};
    }


Código:
Entra a func1 y el frame es Frame 1
Entra a func2 y el frame es Frame 2
Entra a func3 y el frame es Frame 1 y este es el patron Patron 1
Entra a func3 y el frame es Frame 2 y este es el patron Patron 2

También dale un rato a aprender expresiones regulares, esto fue algo así de rápido, por lo que no está completo, pero ve lo que puede hacer

Código PHP:
using System;
using System.Text.RegularExpressions;

public class 
Mr
{
    public static 
void Main()
    {
        
string pattern = @"([A-Z])([\d]*[\.]*[\d]*)|([\d]*[\.]*[\d]*)[,]*";
        
Regex rx = new Regex(pattern,
                
RegexOptions.Compiled RegexOptions.IgnoreCase);

        
string text "#I225.7O226.2L006B100V25.7F50.2H50.2R0080S€„€ˆ„À#2000,1400,230,45.0,55.0,8.6";

        
MatchCollection matches rx.Matches(text);

        foreach(
Match match in matches)
        {
            if(
match.Value != ""){
                
Console.WriteLine(match.Groups.Count);
                
Console.WriteLine("{0} : {1} : {2} : {3} : {4}",match.Value,
                                                          
match.Groups[0].Value,
                                                          
match.Groups[1].Value,
                                                          
match.Groups[2].Value,
                                                          
match.Groups[3].Value);
            }
        }
            
    }

Código:
4
I225.7 : I225.7 : I : 225.7 : 
4
O226.2 : O226.2 : O : 226.2 : 
4
L006 : L006 : L : 006 : 
4
B100 : B100 : B : 100 : 
4
V25.7 : V25.7 : V : 25.7 : 
4
F50.2 : F50.2 : F : 50.2 : 
4
H50.2 : H50.2 : H : 50.2 : 
4
R0080 : R0080 : R : 0080 : 
4
S : S : S :  : 
4
2000, : 2000, :  :  : 2000
4
1400, : 1400, :  :  : 1400
4
230, : 230, :  :  : 230
4
45.0, : 45.0, :  :  : 45.0
4
55.0, : 55.0, :  :  : 55.0
4
8.6 : 8.6 :  :  : 8.6
Responder Con Cita