none
Strange behaviour in displaying datas in a console app RRS feed

  • Question

  • Hello everyone,

     I have a problem with my console, sometimes I have some bizarre caracters displayed and I don't know why!!

    here is my source code

      private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
            {
                    SerialPort sp = (SerialPort)sender;
                    RxedData = sp.ReadLine();
                    Console.WriteLine(RxedData);
                    RxedData = sp.ReadLine();
                    Console.WriteLine(RxedData);   
            }

    Thanks in advance,


    • Edited by i.Haaa Tuesday, September 4, 2018 12:56 PM
    Tuesday, September 4, 2018 12:55 PM

All replies

  • This may be an encoding issue

    https://docs.microsoft.com/en-us/dotnet/api/system.text.asciiencoding?redirectedfrom=MSDN&view=netframework-4.7.2


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Stanly Fan Wednesday, September 5, 2018 6:03 AM
    Tuesday, September 4, 2018 1:07 PM
    Moderator
  • You're reading from a serial port here.  Serial port interfaces are not guaranteed to be error free.  It's possible you might sometimes get a missed bit or two.  Are you sure the ATNRAG command is supposed to return a text string?

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, September 5, 2018 4:50 AM
  • the command card should return NR which is a string followed by a numeric value, so I can't find out how to solve this 

    Wednesday, September 5, 2018 7:46 AM
  • I'm not familiar with those UART methods (or the SerialPort class to be honest), but it seems that you are always reading the data out on the assumption it will always be string by using ReadLine.

    Is it possible this is not always the case? You may need to use some more advanced handling to read out the data as binary data and then determine whether this represents a string or numeric data (depending on the 'NR' prefix for example).

    This example shows reading binary data from the SerialPort and then converting to a string, though you would possibly need to just look at the first two characters for example and say 'if this equals "NR" then process the next few bytes as a number, otherwise process all the data as a string'.

    Wednesday, September 5, 2018 8:11 AM
  • Thank you, I'll try it
    Wednesday, September 5, 2018 8:17 AM
  • Hello everyone,

     I have a problem with my console, sometimes I have some bizarre caracters displayed and I don't know why!!

    here is my source code

      private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
            {
                    SerialPort sp = (SerialPort)sender;
                    RxedData = sp.ReadLine();
                    Console.WriteLine(RxedData);
                    RxedData = sp.ReadLine();
                    Console.WriteLine(RxedData);   
            }

    Thanks in advance,


    The example found in Docs site, says you should use sp.ReadExisting(). Can you use the internet to check how to use SerialPort?

    Wednesday, September 5, 2018 8:19 AM