Main Site Documentation

Please help me to read string in serial port


#1

hello, Please help me
My problem is that EMX development system read 8 byte in the time when I send a string of 30 byte ie :
an exemlple:
I am sending a message “1, 12.42 .12,121,45556854.12\n” from PC
I receive the in EMX development system 8 byte cad "z, 12.42."
my prbplem in the line 15 : int count = UART.Read(rx_byte, 0, 60);///// MY PROBLEM IS (count = 8)
here is my code:

namespace change_this_to_your_namespace
{
public class Program
{
public static void Main()
{

        SerialPort UART = new SerialPort("COM2", 9600);
        int read_count = 0;
        byte[] rx_byte = new byte[60];         
        UART.Open();
        while (true)
        {

            int count = UART.Read(rx_byte, 0, 60);///// MY PROBLEM IS HERE (count = 8) when I send "z,12.42,12,121,45556854.12\n" read 8 byte(z,12.42,) 
            if (count > 0) // Minimum one byte read
            {
                char[] chars = Encoding.UTF8.GetChars(rx_byte);
                string dataLine = new string(chars, 1, count);
                       

                 string[] tram = dataLine.Split(',', '\n');  // parse the NMEA data sentence here
                  int l = tram.Length;
                 // int type =(int)  ConvertString.ToSingle(tram[0]);
                  int type = (int)double.Parse(tram[0]);
                //  float num = (int) ConvertString.ToSingle(tram[1]);
                //  float val_0 = ConvertString.ToSingle(tram[2]);
                //  float val_1 = ConvertString.ToSingle(tram[3]);
                //  float val_2 = ConvertString.ToSingle(tram[4]);
                  switch (type)
                  { case 1 :
                          double ax = double.Parse(tram[2]);
                          double ay = double.Parse(tram[3]);
                          double az = double.Parse(tram[4]);
                         System.Console.WriteLine("ax=" + ax + " " + "ay=" + ax + " " + "az=" + az);
                         break;
                    case 11 :
                         double Gx = double.Parse(tram[2]);
                         double Gy = double.Parse(tram[3]);
                         double Gz = double.Parse(tram[4]);
                         System.Console.WriteLine("Gx=" + Gx + " " + "Gy=" + Gy + " " + "Gz=" + Gz);
                         break;
                    case 2 :
                         double Mx = double.Parse(tram[2]);
                         double My = double.Parse(tram[3]);
                         double Mz = double.Parse(tram[4]);
                         System.Console.WriteLine("Mx=" + Mx + " " + "My=" + My + " " + "Mz=" + Mz);
                         break;
                    case 3:
                         double Rx = double.Parse(tram[2]);
                         double Ry = double.Parse(tram[3]);
                         double Rz = double.Parse(tram[4]);
                         Console.WriteLine("Rx=" + Rx + " " + "Ry=" + Ry + " " + "Rz=" + Rz + " ");
                         break;


                    case 95 :
                         double H = double.Parse(tram[2]);
                         double M = double.Parse(tram[3]);
                         double S = double.Parse(tram[4]);
                         System.Console.WriteLine("H=" + H + " " + "M=" + M + " " + "S=" + S + " ");
                         break;
                    case 98:

                         double lat = double.Parse(tram[2]);
                         double latVirgule = double.Parse(tram[3]);
                         int n = (tram[3].Length);
                         lat = lat + latVirgule / Math.Pow(10, n);
                          ///////////
                         double log = double.Parse(tram[4]);
                         double logVirgule = double.Parse(tram[5]);
                         int m = (tram[5].Length);
                         log = log + latVirgule / Math.Pow(10, m);
                              ///////////
                         double alt = double.Parse(tram[6]);
                         System.Console.WriteLine("lat=" + lat + " " + "log=" + log + " " + "alt=" + alt + " ");
                         break;

                    case 199:
                        
                         {
                         if (l ==8)
                             
                         {    
                             double bering = double.Parse(tram[2]);
                             double beringVirgule = double.Parse(tram[3]);
                             int m1 = (tram[3].Length);
                             bering = bering + beringVirgule / Math.Pow(10, m1);
                             /////////
                             double speed = double.Parse(tram[4]);
                             double speedVirgule = double.Parse(tram[5]);
                             int n1 = (tram[5].Length);
                             speed = speed + speedVirgule / Math.Pow(10, n1);
                             //////

                             double time = double.Parse(tram[l - 2]);
                             System.Console.WriteLine("speed=" + speed + " " + "bering=" + bering + " " + "time =" + time + " ");
                             
                         }
                             
                         else
                         {
                             
                             double bering = double.Parse(tram[2]);                                 
                             double speed = 0;
                             double time = double.Parse(tram[l - 2]);
                             System.Console.WriteLine("speed=" + speed + " " + "bering=" + bering + " " + "time =" + time + " ");
                             
                         }
                         }
                        //////

                         break;


                  }


            }



              
        }
    }
}

}
:’(


#2

Serial ports are byte-at-a-time devices.

You need to create your own string, and handle it when you have the full compliment of data you need.

You could open the Gadgeteer code and look at it’s readline addition, but there was a flaw in it that I’m not sure is addressed - but it might help.

You look like you’re splitting NMEA data from a GPS. There are several codeshare examples doing this.


#3

thank you but
can i use
int count = UART.Read (rx_byte, 0, 8)
int count1 = UART.Read (rx_byte, 8.8) ;/
int count2 = UART.Read (rx_byte 16.8) ;/
int count3 = UART.Read (rx_byte, 24.8) ;/


#4

is what I can use
int count = UART.Read (rx_byte, 0, 8) ;
int count1 = UART.Read (rx_byte, 8.8) ;/
int count2 = UART.Read (rx_byte 16.8) ;/
int = COUNT3 UART.Read (rx_byte, 24.8) ;/


#5

you can use the BytesToRead property to understand how many bytes have arrived, does that help you? I don’t understand what your next question is/was.


#6

Hi,
here are two links to examples that use eventhandler to get the data from a serial port and that show how complete strings can be retrieve from the data that arrive in chunks over the serial port.

https://www.ghielectronics.com/community/forum/topic?id=14927

https://www.ghielectronics.com/community/codeshare/entry/644


#7

thank you for your help