Ver Mensaje Individual
  #14 (permalink)  
Antiguo 04/01/2017, 09:26
REHome
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 17 años
Puntos: 8
Respuesta: Contar las tramas de byte recibidas

Buenos días buen hombre:

Por fin me creó el archivo al disco duro y bien. ;)

Código:
using System;
using System.IO;
using System.IO.Ports; // No olvidar.

namespace Puerto_Serie_Rread_consola
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort mySerialPort = new SerialPort("COM4");

            mySerialPort.BaudRate = 115200;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.Two;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.RtsEnable = true;

            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            mySerialPort.Open();

            Console.WriteLine("Presione cualquier tecla para continuar...");
            Console.WriteLine();
            Console.ReadKey();
            mySerialPort.Close();
        }

        private static void DataReceivedHandler( object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            int count = 0;
            byte[] datosArray = new byte[30000];
            while (count < datosArray.Length)
            {
                try
                {
                    count += sp.Read(datosArray, count, datosArray.Length - count);
                    Console.WriteLine("Dato recibido:");
                    Console.Write(count);

                    if (count == 28256)
                    {
                        File.WriteAllBytes("fotón.jpg", datosArray); // Crear archivo en el disco duro.
                    }
                }
                catch (TimeoutException)
                {
                    //maybe increase ReadTimeout or something, use exponential backoff, your call
                }
            }
        }
    }
}
Algo que quiero saber. Tengo que poner aproximado los bytes a recibir. ¿Y si no se los que vienen?
¿Qué hago para no preocuparme de eso?
Saludos.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar