0

I have a timer event that executes the following code. I'm trying to read from a TCP connection for a specific string, but it doesn't seem like the buffer gets updated on each passing timer tick event. The source that I'm getting this data from will send 4 different types of strings in a byte array depending on the current state of the system. They are sent to me on a continuous basis. What appears to be happening is that I read just once and then not again for some reason. I've verified that the source I'm receiving data from is indeed sending me different messages, but I don't seem to be able to "read" them. Just the first time only apparently. I tried using the Array.Clear() method, but I still only seem to get one buffer of data and not something that is continuously updating itself. The point of this timer event is to continuously update a C# Windows Form app to alert someone of a fault. I created the "PartnerClient TCPClient at the top of my program.

I'm hopeful that someone has some advice. Perhaps I need an EndRead, but I have tried this approach. Any advice would help

public void FaultDataTimer_Tick(object sender, EventArgs e)
        {
            byte[] mRx = new byte[9];
            byte[] statusBytes = new byte[9];
            string strRecv;
            string[] values = { "ULI_Fault", "DynoFault", "ULI_AOkay", "DynoAOkay" };

            if (PartnerClient.Connected == true)
            {
                try
                {
                    PartnerClient.GetStream().BeginRead(mRx, 0, mRx.Length, null, PartnerClient);
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }



                for (int i = 0; i < 9; i++)
                {
                    statusBytes[i] = mRx[i];
                }
                strRecv = Encoding.ASCII.GetString(statusBytes);

                if (values.Any(strRecv.Contains) || values.Any(strRecv.Contains))
                {
                    if (strRecv == "ULI_Fault")
                    {
                        uliPanel.BackColor = Color.Red;
                    }
                    else if (strRecv == "DynoFault")
                    {
                        dynoPanel.BackColor = Color.Red;
                    }

                    else if (strRecv == "ULI_AOkay")
                    {
                        uliPanel.BackColor = greenColor;
                    }
                    else if (strRecv == "DynoAOkay")
                    {
                        dynoPanel.BackColor = greenColor;
                    }
                }

            }
            Array.Clear(mRx, 0, mRx.Length);
        }
Justin
  • 357
  • 2
  • 7
  • 18
  • You need a read loop (with some way of exiting it such as `CancellationToken`). I strongly suggest you convert this code to `async` `await`, then you can just do a simple `await ReadAsync` loop on it. Note that you also need some kind of framing mechanism to know where the beginning and end of each message is, as `Read` and `ReadAsync` will not necessary fill the buffer each time. – Charlieface Aug 12 '22 at 16:55
  • @Charlieface Thanks Charlie! Would it be possible for you to quickly mock up some code as an example for me? I've really been banging my head against the wall on this – Justin Aug 12 '22 at 17:54
  • This one is a good start https://stackoverflow.com/a/73226673/14868997 although I urge if possible to change your messaging protocol to something more standardized like HTTP or gRPC – Charlieface Aug 12 '22 at 18:02
  • It needs to be TCP – Justin Aug 12 '22 at 18:05

0 Answers0