-1

When using C# I can only read the last 4 characters of the value in COM2.

Putty reads it correctly

I have no control or information over the source that is putting the value on COM2.

My C# settings resemble the basic settings on Putty configuration screen, but C# offers more settings, so Putty must have some underlying settings not being shown.

I checked the documentation on Putty, I can't find the default settings on serial port.

My code:

 using System.IO.Ports;

...

SerialPort mySerialPort = null;
mySerialPort = new SerialPort("COM2");
mySerialPort.BaudRate = 19200;
mySerialPort.Parity = System.IO.Ports.Parity.None;
mySerialPort.StopBits = System.IO.Ports.StopBits.One
mySerialPort.DataBits = 8;
mySerialPort.Handshake =  System.IO.Ports.Handshake.None;
mySerialPort.DiscardNull = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.ReadBufferSize = 2147483647;

...

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string value= sp.ReadExisting(); //I onle get the last four characters 
    }

So I should be obtaining the value "17159160118A" but I onle get "118A", Putty reads the whole string correctly

So given that I have no information about the signal source, my best bet is to use similar configuration as Putty in my code, do you know what configuration this could be? or what am I doing wrong?

The One
  • 4,560
  • 5
  • 36
  • 52
  • 2
    This is a "quit programming too soon" problem. If you don't do anything useful with the string, like Putty does, then your eyes are not nearly quick enough to see the ones that were produced by earlier DataReceived events. High odds you should be using ReadLine() btw. – Hans Passant Jun 12 '17 at 15:17
  • Show us [mcve]. It's not clear what you are doing with `mySerialPort`, once you set up everything. – Martin Prikryl Jun 12 '17 at 15:24
  • mySerialPort is just there, declared as an instance variable, I have methods to close and open the port, and I dispose it when the user closes the application – The One Jun 12 '17 at 15:41
  • Thanks @HansPassant indeed I'm receiving the string in two parts, I'll need to concatenate. – The One Jun 12 '17 at 21:35

1 Answers1

1

So, I deleted all the code and started again with a clean console application, the source was sending the data in two parts, , I was just only reading the second, I just needed to know what was the string terminator, I found out that it was the null terminator character ('\0') so I used that to concatenate my strings, here's my code:

class Program
{
    static void Main(string[] args)
    {

        using (SerialPort sp = new SerialPort("COM2", 19200, Parity.None, 8, StopBits.One))
        {
            //sp.DiscardNull = true;
            sp.Handshake = Handshake.XOnXOff;
            sp.ReadBufferSize = 16384;
            sp.Open();
            sp.DataReceived += sp_DataReceived;
            AppDomain.CurrentDomain.ProcessExit += new EventHandler((x, y) =>
            {
                sp.Close();
            });
            Console.ReadKey();
        }

    }
    static string myString = string.Empty;
    static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {

        SerialPort sp = (sender as SerialPort);
        if (sp.BytesToRead > 0)
        {
            myString += sp.ReadExisting().Trim();
            if (myString.Last() == '\0')
            {
                myString = myString.Trim('\0').Trim();
                if (!string.IsNullOrWhiteSpace(myString))
                {
                    Console.WriteLine(myString);
                }
                sp.DiscardInBuffer();
                myString = string.Empty;
            }
        }
    }
}

I commented out //sp.DiscardNull = true; as it was removing all the null characters and it worked, so any way thanks for the downvote.

The One
  • 4,560
  • 5
  • 36
  • 52