ross_p
New member
- Joined
- May 18, 2020
- Messages
- 2
- Programming Experience
- 1-3
Hi everyone, I'm a fairly new programmer, hobbyist only and much self taught... I've got a small application which polls numbered devices over a serial connection. As it does so i would like to to update some status blocks in the UI with different background colours depending on the status. I can easily assign a background colour to the item if i know the name of the item but my item name changes every time i cycle throught the for statement.
The items i want to change the colour on are named squentially eg: lblCms1, lblCms2, lblCms3 etc. and the for loop iterates through an integer i which would relate to each one of these.
I hope what i've written makes sense. I've attached a screenshot of the application and the relevant section of code is below. It's the list of CMS1, CMS2 etc labels i'd like to change the background colour of.
The items i want to change the colour on are named squentially eg: lblCms1, lblCms2, lblCms3 etc. and the for loop iterates through an integer i which would relate to each one of these.
I hope what i've written makes sense. I've attached a screenshot of the application and the relevant section of code is below. It's the list of CMS1, CMS2 etc labels i'd like to change the background colour of.
C#:
private async Task PollNetworkAsync()
{
cts = new CancellationTokenSource();
int _camOrigin = Int32.Parse(camOrigin.Text);
int _camNumber = Int32.Parse(camNumber.Text);
while (true)
{
for (int i = _camOrigin; i <= (_camOrigin + _camNumber); i++)
{
toolStripStatusLabel1.Text = "Polling CMS " + i + ". "; // Attempt: " + (myRetries + 1);
log.AppendText("Polling CMS " + i + ". "); // Attempt: " + (myRetries + 1) + " : ");
char message = '?';
await sendSerialWaitForReply(i, message);
}
}
toolStripStatusLabel1.Text = "Poll cycle complete";
serialPort1.DiscardInBuffer();
}
private async Task sendSerialWaitForReply(int dest, char message)
{
int myRetries = 0;
int _camRetries = Int32.Parse(camRetries.Text);
int _camTimeout = Int32.Parse(camTimeout.Text);
while (myRetries < _camRetries)
{
try
{
serialPort1.WriteLine("<" + dest + ",00," + message + ">");
serialPort1.ReadTimeout = _camTimeout;
string recievedParameters = await Task.Run(() => serialPort1.ReadTo("\n"), cts.Token);
serialPort1.DiscardInBuffer();
receivedData.Text = recievedParameters;
await parseIncomingSerial(recievedParameters);
break;
}
catch (OperationCanceledException)
{
log.AppendText("Polling Cancelled by user!" + Environment.NewLine);
cts = null;
}
catch (TimeoutException)
{
toolStripStatusLabel1.Text = "Timeout";
log.AppendText("CMS did not reply. " + Environment.NewLine);
myRetries++;
//break;
}
}
}