Ver Mensaje Individual
  #1 (permalink)  
Antiguo 30/12/2015, 04:54
REHome
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 17 años
Puntos: 8
¿Guardar datos fijos en variables constantes, enumeraciones o en otro sitio?

Hola:

Quiero saber cual es la forma de guardar datos fijos de un byte y otros de varios bytes. Estos datos son enviados al puerto serie. No se envían todos al mismo tiempo, sino cuando de la orden cuando quiera.

Por ejemplo, tengo datos guardados en enumeraciones como puedes ver abajo.

Código:
enum DexCommands { INIT = 0x00, STATUS = 0x01, READ = 0x02, WRITE = 0x04, LIGHT = 0x07, MAGIC_HANDSHAKE = 0x27 };


También se puede almacenar de esta forma en varibales const.

Código:
        const byte INIT = 0x00;
        const byte STATUS = 0x01;
        const byte READ = 0x02;
        const byte SEEK = 0x03;
        const byte WRITE = 0x04;
        const byte PAGE = 0x05;
        const byte LIGHT = 0x07;
        const byte MAGIC_HANDSHAKE = 0x27;
Otro ejemplo para tratar de byte para enviar por el puerto serie.

Código:
        byte[] INIT = { 0x00 };
        byte[] STATUS = { 0x01 };
        byte[] READ = { 0x02 };
        byte[] SEEK = { 0x03 };
        byte[] WRITE = { 0x04 };
        byte[] PAGE = { 0x05 };
        byte[] LIGHT = { 0x07 };
        byte[] MAGIC_HANDSHAKE = { 0x27 };

1. Quiero saber cuál forma es mejor y por qué.

Si quiero enviar el byte STATUS que es 0x01 puedo hacerlo así, si no estoy equivocado. Eso si, llamándole directamente 0x01.

Código:
byte[] mBuffer = new byte[1];
    mBuffer[0] = 0x01;
    serialPort1.Write(mBuffer, 0, mBuffer.Length);
Ya que lo tengo guardado en enum, lo llamaré así:

Código:
            serialPort1.Open(); // Abrir puerto.
            serialPort1.Write((byte)Dex_Comandos.STATUS);
            serialPort1.Close(); // Cerrar puerto.
2. Este código no funciona. ¿Cómo es la mejor manera para usar datos, variables o enumeraciones en este caso?

Teniendo esta trama de byte de forma fija, por ejemmplo, quiero mandar el comandos STATUS que es de un solo byte por el puerto serie. ¿Cómo lo hago?

3. Tengo esta trama de byte abajo. ¿Cómo puedo enviarla?

Código:
byte[] TRAMAS = { 0x10, 0x29, 0x23, 0xbe, 0x84, 0xe1, 0x6c, 0xd6, 0xae, 0x52, 0x90, 0x49, 0xf1, 0xf1, 0xbb, 0xe9, 0xeb };
Felices fiestas 2015.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar