Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > .NET
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

 
 
Herramientas Buscar en Tema Desplegado
  #2  
Antiguo 16-11-2023
tsk tsk is offline
Miembro
 
Registrado: dic 2017
Posts: 52
Poder: 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
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
XHTML, Java enviar tramas de bytes REHome JAVA 0 09-07-2016 13:24:02
Lectura de tramas en puerto serie vejerf OOP 1 04-02-2010 20:07:11
array de bytes con ICS 3NK1 Internet 1 15-04-2009 02:20:44
Lectura de tramas: <SYN><DLE><STX> ... <DLE><ETX> vejerf Varios 17 30-06-2008 15:20:26
Documentacion para Generar Tramas TCP bastardo10 Varios 0 07-06-2007 17:31:38


La franja horaria es GMT +2. Ahora son las 10:08:43.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi