So I have been working on my C# project which required to do some serial communication. Everything was working fine until I have one really... weird issue.
So right now I am recieving a code in serial which is stored in myString. Then below where I get the issue which is that if I am not debugging the code then a whole if statement just... get ignored. However when I use a breakpoint it does everything right. For referance everything here works except the part of (*l) (the rest of the code was removed for needless parts. I can attach more if you guys think it can help.)
public delegate void AddDataDelegate(String myString);
public AddDataDelegate myDelegate;
//
//
this.myDelegate = new AddDataDelegate(AddDataMethod);
//
//
private void Receiver(object sender, SerialDataReceivedEventArgs e)
{
Byte[] str = System.Text.Encoding.ASCII.GetBytes(comPort.ReadExisting());
this.Invoke(this.myDelegate, new Object[]
{
System.Text.Encoding.Default.GetString(str)
});
}
//
private void button2_Click(object sender, EventArgs e)
{
if (connectionStatus.Text == "Connected")
{
comPort.Close();
}
else
{
try
{
comPort.PortName = port;
comPort.BaudRate = baudRate;
comPort.DataBits = dataBits;
comPort.StopBits = (StopBits)stopBits;
comPort.Parity = parity;
comPort.DataReceived += new SerialDataReceivedEventHandler(Receiver);
comPort.Open();
connectionButton.Text = "Disconnect";
connectionStatus.Text = "Connected";
}
catch
{
comPort.Close();
}
}
}
public void AddDataMethod(String myString)
{
try
{
string colorSensorValue;
string distanceSensorValue;
string proximitySwitch;
string limitSwitch;
if (myString.Contains("*c*"))
{
string[] colors;
int red;
int blue;
int green;
int max;
colorSensorValue = myString.Substring(myString.IndexOf("*c*") + 3);
if (colorSensorValue.Contains("*e")) colorSensorValue = colorSensorValue.Substring(0, colorSensorValue.IndexOf("*e*"));
colors = colorSensorValue.Split(',');
red = Convert.ToInt16(colors[0]);
green = Convert.ToInt16(colors[1]);
blue = Convert.ToInt16(colors[2]);
max = Math.Max(red, Math.Max(green, blue));
red = red * 255 / max;
blue = blue * 255 / max;
green = green * 255 / max;
color.BackColor = Color.FromArgb(red,blue,green);
colorSensorTextBox.Text = (colorSensorValue);
}
if (myString.Contains("*d"))
{
distanceSensorValue = myString.Substring(myString.IndexOf("*d*") + 3);
if (distanceSensorValue.Contains("*e")) distanceSensorValue = distanceSensorValue.Substring(0, distanceSensorValue.IndexOf("*e*"));
distanceSensorTextBox.Text = (distanceSensorValue);
}
if (myString.Contains("*l"))
{
limitSwitch = myString.Substring(myString.IndexOf("*l*") + 3);
if (limitSwitch.Contains("*e")) limitSwitch = limitSwitch.Substring(0, limitSwitch.IndexOf("*e*"));
limitRead.Text = limitSwitch;
}
if (myString.Contains("*p"))
{
proximitySwitch = myString.Substring(myString.IndexOf("*p*") + 3);
if (proximitySwitch.Contains("*e")) proximitySwitch = proximitySwitch.Substring(0, proximitySwitch.IndexOf("*e*"));
proximityRead.Text = proximitySwitch;
}
comPort.BaseStream.Flush();
}
catch
{
}
}
Example of myString:
*c*96,84,75*e**d*25.5*e**p*0*e**l*0*e*
so it will be read as: colors: 96 , 84 , 75 (happens right!) distance: 25.5 (happens right!) prox : 0 (happens right!) limit : 0 (doesnt happen..)
note that order of both sent and received data doesn't change which one (limit) that doesn't work unless I breakpoint
ifs, run the code in Release (i.e. not from VS). See if it behaves correctly.