locked
SerialPort ReadByte methot sometimes lose first byte c#

    Question

  •  


    Posts 2
    serailport reading error first byte lose c#

    I have writen a serial port program. I used Serialport class in C#.

    If I readbyte(), readchar() or other function I doesn't get first character.

    please find my sample below.

    thanks

    void ReadData(int _timeout)

    {

    _continue = true;

    Array.Clear(oku, 0, oku.Length);

    int i = 0;

    try

    {

    TimerWaitData.Enabled = true;

    TimerWaitData.Start();

    while (_continue)

    {

    System.IO.Stream s = _serialPort.BaseStream;

    _serialPort.Read(oku, oku.Length, 0);

    //object dd = TimerWaitData.InitializeLifetimeService();

    if ((_serialPort.BytesToRead) > 0)

    {

    oku = (byte)_serialPort.ReadByte();

    if (BitConverter.ToChar(oku, 0) == 's')

    MessageBox.Show("S ALDIM");

    if (_serialPort.BytesToRead == 0 && i == message.Length)// oku == 57)

    _continue = false;

    i++;

    TimerWaitData.Stop();

    //TimerWaitData.Enabled = true;

    TimerWaitData.Start();

    }

    else if (i == message.Length)

    _continue = false;

    if (errorMessagesClass.ErrorStatus > 0)

    throw errorMessagesClass;

    }

    TimerWaitData.Stop();

    TimerWaitData.Enabled = false;

    }

     

     




    xanim

    Thursday, November 29, 2007 12:32 AM

All replies

  •  

    Hi,

    Assume that you have sent “abcd”; do you just receive “bcd”?

    And would you please be more specific? Such as the property setting of the serial port.

     

    By the way, is oku an array or a byte variable in your code? It seems an array in the first part, but you use it as a byte variable.

    Wednesday, December 05, 2007 7:00 AM
  •  

    Hi,

    Thanks your reply.

    Yes I send "abcd" with byte. But I get bcd.

    Oku is byte array.

    byte[] oku = new byte[120];

     

     

    sport.PortName = portName;

    sport.BaudRate = baudRate;

    sport.DataBits = dataBits;

    sport.Parity = (System.IO.Ports.Parity)praty;

    sport.StopBits =(System.IO.Ports.StopBits)stopBits;

    sport.ReadTimeout = 500;

    sport.WriteTimeout = 500;

     

     

    if (!sport.IsOpen)

    { try

    {

    sport.Open();

    }

     

    Thank you very much for your interested.

    Xanim

    Wednesday, December 05, 2007 10:38 PM