Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: C# Serial Communication Null Exception

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    2

    C# Serial Communication Null Exception

    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.

  2. #2
    Senior Member manicksan's Avatar
    Join Date
    Jun 2020
    Location
    Sweden
    Posts
    314
    Can you post rest of the code? or at least all parts that involve the serialPort.
    then I can help.

  3. #3
    Senior Member manicksan's Avatar
    Join Date
    Jun 2020
    Location
    Sweden
    Posts
    314
    also on which line do you get the exception?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •