Visual C# Developer Center > Visual C# Forums > Visual C# General > SerialPort 0x1A character reading problem
Ask a questionAsk a question
 

AnswerSerialPort 0x1A character reading problem

  • Thursday, June 04, 2009 12:06 PMCristopher Pereria Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi,

    I'm developing an application wich receives 32 bytes packages, by doing this:

    byte[] comBuffer = new byte[32];
    comPort.Read(comBuffer, 0, 32);
    The problem is, when the packages contains the 0x1A char the reading operations is interrupted.
    Does anyone know how may I contour this problem?

    Thanks

Answers

  • Thursday, June 04, 2009 4:47 PMCristopher Pereria Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Lol, I know what you meant to said with "sanity check". The remainder bytes are there.

    I tried a solution that has fixed my problem. It might not be a good solution but it works.
    Since the problem was that the 0x1A was making the Read() method to "jump out", so I read just one byte at the time and then I proceed with rest of the operation.

    Thanks you for all your help
                        for (int i = 0; i < 32; i++)
                        {
                            comPort.Read(comBuffer,i, 1);
                        }

All Replies

  • Thursday, June 04, 2009 1:47 PMTergiver Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    0x1A is the Ctrl+Z character which for a text file reader means end of file. What type is the "comPort" stream above?
  • Thursday, June 04, 2009 3:08 PMCristopher Pereria Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    comPort is a System.IO.Ports.SerialPort instance.
  • Thursday, June 04, 2009 3:13 PMTergiver Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    What is the type of comPort.BaseStream?
  • Thursday, June 04, 2009 3:24 PMCristopher Pereria Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Maybe I'm going to say something absurd.
    I haven't define the BaseStream, so I supose that it is the default one.

    This is all what I'm doing:

    portaCOMProp = new SerialPort();
    comPort.BaudRate = "9600";
    comPort.Parity = "0";
    comPort.StopBits = "1";
    comPort.DataBits = "8";
    comPort.PortName = "COM5";
    comPort.ReceivedBytesThreshold = 32;
    comPort.ReadBufferSize = 32;

    comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

    comPort.Open();


  • Thursday, June 04, 2009 3:40 PMTergiver Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    You have set ReceivedBytesThreshold to 32, meaning that you only want the DataReceived event to fire when there are (at least) 32 bytes in the buffer to read. Are you saying that if the device sends a chunk of 32 bytes and one of those bytes is Ctrl+Z, the DataReceived event does not fire?

  • Thursday, June 04, 2009 3:48 PMCristopher Pereria Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes it fires. The problem is that the comBuffer from the position where is the 0x1A to is not filled.

    For example, this is the content of the comBuffer if there isn't the 0x1A:

    7E 4B 00 00 13 A0 64 1F 01 01 99 19 D3 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 F1 FF

    And this is ther is 0x1A:
    7E 24 00 00 13 A0 54 1A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

  • Thursday, June 04, 2009 4:04 PMTergiver Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I don't know. I've never encountered this problem. It looks like you have done something odd like:

    StreamReader reader = new StreamReader(comPort.BaseStream);
    reader.Read(buffer, 0, 32);

    Injecting a text reader over the SerialPort's native stream. Can you show me your comPort_DataReceived code?
  • Thursday, June 04, 2009 4:11 PMStephen Cleary Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You are using the return value from SerialPort.Read, correct? Because it might be less than count.

           -Steve
    Programming blog: http://nitoprograms.blogspot.com/
    I will be in Chicago for the WPF training: http://blogs.msdn.com/jaimer/archive/2009/04/01/announcing-the-using-wpf-to-build-lob-applications-training-tour.aspx
  • Thursday, June 04, 2009 4:11 PMCristopher Pereria Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I'm not using the BaseStrean to read, should I?

    Here is the code from comPort_DataReceived:

    void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
          byte[] comBuffer = new byte[32];
          comPort.Read(comBuffer, 0, 32);
    
          Thread.Sleep(20);
          comPort.DiscardInBuffer();
    
          if (comBuffer[0] == 126){
             ...
             create a new thread to handle the package
          }
    }
         

  • Thursday, June 04, 2009 4:12 PMScottyDoesKnow Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    What are you connecting to? This may be a stupid question but are you sure this isn't supposed to happen? Maybe that's the end of data char for whatever you connect to.
  • Thursday, June 04, 2009 4:15 PMTergiver Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You're also calling DiscardInBuffer, (possibly) throwing away part of the next message from the device.

    Just for a sanity check, try adding a second Read call after the first to see if the remainder of the message is there (the bytes after the 0x1a).
  • Thursday, June 04, 2009 4:22 PMCristopher Pereria Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm connecting to a base station that receives those packages from wireless nodes. The 0x1A may be data, and on this particular case it belongs to the serial number from the node that sent the package.
  • Thursday, June 04, 2009 4:31 PMTergiver Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    "sanity check" was the wrong term. What I mean is, by calling Read twice, we will know if the Read call is dropping the bytes, or if something else has.
  • Thursday, June 04, 2009 4:47 PMCristopher Pereria Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Lol, I know what you meant to said with "sanity check". The remainder bytes are there.

    I tried a solution that has fixed my problem. It might not be a good solution but it works.
    Since the problem was that the 0x1A was making the Read() method to "jump out", so I read just one byte at the time and then I proceed with rest of the operation.

    Thanks you for all your help
                        for (int i = 0; i < 32; i++)
                        {
                            comPort.Read(comBuffer,i, 1);
                        }
  • Thursday, August 27, 2009 9:09 AMbarbyyy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    same problem here!

    I use a microcontroller with a usb to serial cable and a C# application to read data...

    If I cut the C# application and I use hyper terminal for serial data read it's all ok....
    If i use C# I've the same problem...

    And it's a big problem because I've to read 800 byte (115200 baud/s) and send a new request every 200ms...If i have the 0x1a in the packet....:(

    cristopher, can u write here your complete:

    void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    ....
    }

    If it's a serial port component problem of c#...i think it's a huge problem!
    And I'm surprise that Microsoft don't fix it!

    thanks