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);
}