1

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?

quatre432
  • 47
  • 1
  • 11

2 Answers2

0

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

You'll need to debug your code, put a break point &/or Debug.Print(data) statements to see why only one out of ten times it meets the first if condition:

if (comport.BytesToRead > 0)
{

if (data.StartsWith("S08"))
    {
        textBox1.Text = data.Substring(4).Trim();
        timer3.Stop();
        scan();
        timer3.Start();
    }

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

if (data == "S143")
    {
        label8.Text = "0.00";
    }

if (data =="S145")
    {
        label8.Text = "- - - - -";
    }

 if (data == "S141")
    {
        label8.Text = "- - - - -";
    }

    comport.DiscardInBuffer();
}

Edit:

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?

Dont make them else if. Make each one its own if. Obviously if there is a large number of if's you would want to architect it a bit smarter, eg: Alternative to writing many if statements?

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • I will try like I said it will always work I there is no weight while there is weight it is sending constant data and the little flip where it sends the first response may not be enough time or something – quatre432 Jul 21 '13 at 01:57
  • i tried making them all if's and it happens the same way the i took out all the comport.discards and put only one at the end, and still the same, and of course make it architecture a bit smarter once i get it working right lol – quatre432 Jul 21 '13 at 05:01
  • I added code as we discussed, double check yours is the same. Keep on logging the output and figure out what condition is preventing the barcode scanning from occuring 1/10. Also take a look at [this](http://stackoverflow.com/questions/15913924/improve-barcode-search-in-a-textbox-c-sharp/15913993#15913993), I reckon it will be simple once you can see it. Trees from the forest sort of stuff.. hunt it down like a dog after bone! Conditional Breakpoints, Debug.Print everything with # lines code number that's been executed, etc – Jeremy Thompson Jul 21 '13 at 05:26
  • Ill try looked at the suggestion i dont have a promblem with the data scanning, i did at one time, until i started checking the weight it was scanning fine like i said scanner responses are crazy and in no order for instance if the scanner writes back s143. that means it at 0 weight, it responds s141. it is calculating weight, s145. no weight or platter removed, s1440010. item weighs 0.10lbs , in the mist of all these response constantly when scanning the scale responds like this s143.s08AUPC. so i may need to find a way to break that response or check for the endline which is a "\r" – quatre432 Jul 21 '13 at 16:02
  • also another thing that may interest or shead light, if i set my textbox equal to data and what it, when i scan it shows up for a split second not enough for me to even read it – quatre432 Jul 21 '13 at 16:10
  • i got it check edited answer needed to change the intial if loop to a while loop – quatre432 Jul 21 '13 at 16:28
0
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

quatre432
  • 47
  • 1
  • 11