C# Serial Communication Null Exception

Salih

New member
Hello everyone,

I am trying to communicate with my Teensy 3.5 via serial port. I write C# code like below;

Code:
if (serialPort.IsOpen)
            {

                string gelen = serialPort.ReadLine();
                string[] split = gelen.Split(new char[] { ',' });
                int stLength = split.Length;
                
                if (stLength == 21)
                {
                    if (Convert.ToUInt16(split[0]) == HEADER && Convert.ToUInt16(split[19]) == FINISH)
                    {
                        paketLength = Convert.ToByte(split[1]);
                        if (paketLength == gelecekVeriSayisi)
                        {
                            int i;
                            for (i = 0; i < paketLength; i++)
                            {
                                datas[i] = split[paketBegin_Off + i];
                            }
                        }
                        else if (paketLength != gelecekVeriSayisi)
                        {
                            datas = null;
                        }

                    }
                    else
                    {
                        datas = null;
                    }
                }
                else
                {
                    datas = null;
                }
}


When I try to get datas from teensy 3.5, two datas come properly but after I get null exception from C#.

Note: I use serialdatareceived function to understand data is come.
 
Back
Top