locked
char 255) can't be read by SerialPort. RRS feed

  • Question

  • Hello all,

    I'm trying to read the SerialPort and find the device that is connected. The device sends a char 255.
    I open the port and then try to Read the port using ReadExcisting. But everytime I get to ReadExcisting I get an empty string "".
    What I want to get is a char 255.
    I know this because I've seen this in Hyperterminal. In Hyperterminal I get the DEL character but in my own application I don't.

    I even tried to see if the DataReceived event is triggered when this is created but this event never gets triggered.

    Kind Regards

    JKorsten

    PS: I read the port using

    private Boolean Detect()
    {
    Boolean bGevonden = false; try { // Neem de volgende poort en toon in het infoveld waar we zijn lblPoort.Text = "COM" + miCurrPoort.ToString(); lblPoort.Update();
    serialPort.PortName = lblPoort.Text; serialPort.BaudRate = 115200; serialPort.DataBits = 8; serialPort.Parity = System.IO.Ports.Parity.None; serialPort.StopBits = System.IO.Ports.StopBits.One; serialPort.Open(); if (serialPort.IsOpen) { // Openen poort gelukt String sPortData = ""; for (int i = 0; i < 24; i++) { sPortData = serialPort.ReadExisting();
    if (sPortData.IndexOf((char)255) != -1) { bGevonden = true; break; } } serialPort.Close(); } } catch { }
    return bGevonden; }
    Monday, September 7, 2009 7:11 AM

Answers

  • You can't use ReadExisting, code 255 is not a valid character.  Reading binary data requires the Read() method.

    Hans Passant.
    • Proposed as answer by Guang-Ming Bian - MSFT Tuesday, September 15, 2009 7:23 AM
    • Marked as answer by nobugz Saturday, September 19, 2009 8:13 PM
    Monday, September 7, 2009 8:03 PM

All replies

  • How often is the device sending this 255 char? In your code, the serial port will be open only for a brief amount of time before being closed. Maybe that is the cause you are missing the byte....
    Monday, September 7, 2009 7:56 PM
  • You can't use ReadExisting, code 255 is not a valid character.  Reading binary data requires the Read() method.

    Hans Passant.
    • Proposed as answer by Guang-Ming Bian - MSFT Tuesday, September 15, 2009 7:23 AM
    • Marked as answer by nobugz Saturday, September 19, 2009 8:13 PM
    Monday, September 7, 2009 8:03 PM
  • SerialPort Class (System.IO.Ports)

    There is a sample chat program at the link.
    I suspect that Ernesto's suggestion is accurate.
    Be sure that you port settings are correct, too.

    Serial port communications sound like a good use for a thread or the Asynchronous Programming Model
    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, September 7, 2009 8:07 PM
  • Hi All,

    Thanks for the reply.

    http://garylcoxjr.blogspot.com/2007/02/serial-port-usage-i-had-project.html


    I had also found the blog posted above which said the same as nobugz.
    But this doesn't work.
    The char 255 doesn't trigger a datareceived event, so it isn't possible to get BytesToRead and without that it isn't possible use Read.

    I have tried it in the code posted below but the iBTR is always 0.

    Hope someone can help

    Kind Regards

    JKorsten

    PS: the code 255 is send continuously, but how should I get it to read the byte then?
    PS2: The portsettings are the same as with hyperterminal so they should be correct.

    private Boolean Detect()
    {
      Boolean bGevonden = false;
    
      try
      {
        // Neem de volgende poort en toon in het infoveld waar we zijn
        lblPoort.Text = "COM" + miCurrPoort.ToString();
        lblPoort.Update();
    
        serialPort.PortName = lblPoort.Text;
        serialPort.BaudRate = 115200;
        serialPort.DataBits = 8;
        serialPort.Parity = System.IO.Ports.Parity.None;
        serialPort.StopBits = System.IO.Ports.StopBits.One;
                
        serialPort.Open();
        if (serialPort.IsOpen)
        {
          // Openen poort gelukt
          lblPoort.Text += Function.GetMessageText(Program.mrLocRM, "txtSearch"); ;
          lblPoort.Update();
    
          // String sPortData = "";
          int iBTR;
          for (int i = 0; i < 24; i++)
          {
            iBTR = serialPort.BytesToRead;
            if (iBTR != 0)
            {
              byte[] buffer = new byte[iBTR];
    
              // sPortData = serialPort.ReadExisting();
              serialPort.Read(buffer, 0, iBTR);
              // if ((sPortData.IndexOf((char)127) != -1) || (sPortData.IndexOf((char)255) != -1))
              for (int j = 0; j < iBTR; j++)
              {
                if ((buffer[j] == (byte)(char)127) || (buffer[j] == (byte)(char)255))
                {
                  bGevonden = true;
                  break;
                }
              }
            }
          }
          serialPort.Close();
        }        
      }
      catch { }
    
      return bGevonden;
    }
    
    Tuesday, September 8, 2009 6:41 AM
  • Is the handshaking correctly configured? You don't set it so is the default setting correct?
    Do you use any form of handshaking (hardware or xon/xoff) else you should set to None.

    At least what I know for sure is that 255 can be received (else my own application is working on miracles)
    Tuesday, September 8, 2009 8:08 AM
  • Yes I also know it can be received. I have tried it from PC to PC and then it works. But now I have a terminal sending the 255.
    I've tried to change the handshake but it doesn't change anything. In hyperterminal the handshaking is set to Hardware. but what is this for the serialport component?
    Tuesday, September 8, 2009 8:14 AM
  • The following line should enable hardware handshake:

    serialPort.HandShake = HankShake.RequestToSend;

    If you have set your terminal to hardware and the other side not, the terminal is not sending because RTS is not set.

    Hopefully this gets you further. (easiest to test is just to switch off handshaking, than at least anyone can send without clearance from other side)

    A RS232 monitor or scoop can also help in these circumstances, if you have them.
    Tuesday, September 8, 2009 8:45 AM
  • no I don't have a RS232 monitor or scoop.
    and I have put the handshake to requesttosend but it doesn't work.

    But it seems to be a hardware issue because when I connect 2 pc's to eachother then I get the character 255

    So thanx all for the help but I think I need to look into the hardware to solve the problem.
    Tuesday, September 8, 2009 9:02 AM
  • Did you try no handshaking, it might also be the cable. Not all rs232 cables are fully wired.
    Tuesday, September 8, 2009 9:08 AM
  • Set the SerialPort.DtrEnable and RtsEnable properties to True to allow the terminal to send. 

    Beware of the wiring differences between DTE and DCE.  Two PCs need a null-modem cable, one that swaps the signals.  When you wire a terminal, you don't. 

    Use Hyperterminal as an independent test of the hardware.  If it works with Hyperterminal, you'll need to look for the problem in your code.

    Hans Passant.
    Tuesday, September 8, 2009 9:30 AM
  • Hi jkorsten,

    If sender side also use .net framework to send data. There are some common scenarios, which will cause receiver never receive the data..

    1.   The sender close the serialport before receiver receive it, like the code below:


    SerialPort sp;

            private void SendData()

            {

                sp= new SerialPort("COM1");

                sp.Open();

                sp.WriteLine("it a test");

                sp.Close();

     }

    Sender just open the serial port and send data, then close it. If receiver don’t receive the data before sender closing the port, receiver will never receive the data. The fix is closing the port when the application is shutting down.

     

    2.   Let’s  see  the second scenario, see the code first:
    private void SendData()

            {

                SerialPort sp= new SerialPort("COM1");

                sp.Open();

                sp.WriteLine("it a test");

               

     }

    From code above, after the SendData running, SerialPort variable will be GC’d, because there is no more references to the object. Once serialport variable is released, receiver side will never receive the data. The fix is using SerialPort variable as global .

    Please check your code, whether it has the problems above. If you have further question, please let me know.

     

    Have a nice day.

     

    Guang-Ming Bian - MSFT

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Tuesday, September 8, 2009 9:56 AM