I have a question involving the way a serial port is reading and the code is handled.
The device I'm working with is a scanner/scale i am making my program show constant weight as well as scan the barcode
comport.NewLine = "\r";
comport.Write("S14\r");
while (comport.BytesToRead > 0)
{
data = comport.ReadLine();
if (data.StartsWith("S08"))
{
try
{
string data1 = data.Substring(4);
data1 = data1.Trim();
textBox1.Clear();
textBox1.AppendText(data1);
timer3.Stop();
scan();
timer3.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
comport.DiscardInBuffer();
}
else if (data.StartsWith("S144"))
{
if (data == "S1440000")
{
label8.Text = "0.00";
}
else
{
string data3 = data.Substring(4);
data3 = data3.Trim();
var data4 = data3.Insert(2, ".");
string data5 = double.Parse(data4).ToString("F", CultureInfo.GetCultureInfo("en-US"));
label8.Text = data5;
comport.DiscardInBuffer();
}
}
else if (data == "S143")
{
label8.Text = "0.00";
}
else if(data =="S145")
{
label8.Text = "- - - - -";
}
else if(data == "S141")
{
label8.Text = "- - - - -";
}
}
This will display the weight constantly and as long as there is not weight it will scan the barcode im trying to figure out why when there is weight will it not scan, note that if I scan 7 times it may catch it once 1 out of 10 times
Edit ok i now know why its not going through, it only processes the upc when it is first in the received data from the scanner but i have no clue to how properly sort this i watched how the data was comming in and it was comming in multiple ways
sometime i will receive this from the scanner
S143.S08A07166200024. sometimes S1440050.S08A0716620024. sometimes S08A0716620024.S143.
does anyone have any suggestions on how i should go about reading this so all my if statements fire no matter what the order the data is recevied in?
comport.NewLine = "\r";
comport.Write("S14\r");
while (comport.BytesToRead > 0)
{
data = comport.ReadLine();
if (data.StartsWith("S08"))
{
try
{
string data1 = data.Substring(4);
data1 = data1.Trim();
textBox1.Clear();
textBox1.AppendText(data1);
timer3.Stop();
scan();
timer3.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
comport.DiscardInBuffer();
}
else if (data.StartsWith("S144"))
{
if (data == "S1440000")
{
label8.Text = "0.00";
}
else
{
string data3 = data.Substring(4);
data3 = data3.Trim();
var data4 = data3.Insert(2, ".");
string data5 = double.Parse(data4).ToString("F", CultureInfo.GetCultureInfo("en-US"));
label8.Text = data5;
comport.DiscardInBuffer();
}
}
else if (data == "S143")
{
label8.Text = "0.00";
}
else if(data =="S145")
{
label8.Text = "- - - - -";
}
else if(data == "S141")
{
label8.Text = "- - - - -";
}
}
Changed my if to a while loop