0

Hello all. I hope someone may be able to help me with two problems I have that I do not understand.

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        // If the com port has been closed, do nothing
        if (!comport.IsOpen) return;

        // This method will be called when there is data waiting in the port's buffer

        // Determain which mode (string or binary) the user is in

            // Read all the data waiting in the buffer
            string data = comport.ReadExisting();
            textBox1.AppendText(data);
            textBox1.ScrollToCaret();
            //scnbutton.PerformClick();

            // Display the text to the user in the terminal


    }

I am Dealing with barcodes. When my scanner scans the barcode it returns S08A07934068721. It returns this value every time the UPC is 07934068721 which is the value I wish to append to the textbox.

     string data1= data.Substring(4);
     textBox1.AppendText(data1);

This is an example of what have tried to use to substring it.

Every time I try to substring the string data it ends up breaking into pieces and I'm not sure how to stop it. After I get this fixed then I will have a problem with the code below

textBox1.Text = textBox1.Text.PadLeft(13, '0');

This works great and always pads 13 digits. But when the UPC or something types has a 0 in the front it drops down to 12 digits why is this?

Wes Crow
  • 2,971
  • 20
  • 24
quatre432
  • 47
  • 1
  • 11
  • "Breaking into pieces" is not very clear to me. Please give an example of data and data1 strings demonstrating your problem. – user2019047 May 24 '13 at 20:34
  • As for Text.PadLeft, the number 13 refers to the total length of the resulting string, so if your string is 10 in length, then the padded string will be 13 in length, i.e. 000ssssssssss. if your string is greater than 13 in length then no padding will occur. – user2019047 May 24 '13 at 20:44
  • i gave a example of the data string the string data returns S08A07934068721 i need it to return 07934068721 so i try to use a substring which i showed in the example, it returns the correct amount with the execption it first returns blank then the rest of the string. Since it first returns a blank my textbox does not populate. Also the length is not more than 13 this is what it does lets say the textbox= 07934068721 after it padded it looks like this 007934068721 which is 12 digits total take the orginal 0 out and it looks like this 0007934068721 totaling 13 digits – quatre432 May 25 '13 at 16:04
  • Just a note, when answering to a comment, address the person with @, that way the person get a message when logging on that there is an answer to a comment made earlier. I tried your code, and it works perfectly. If you get different results, maybe give more information about your environment (compiler, versions etc). Why do you use appendtext in stead of textbox1.Text = ... ??? – user2019047 May 26 '13 at 00:15

2 Answers2

0

I tried your string in a piece of code and it all worked.

string data = "S08A07934068721";                // results

string data1 = data.Substring(4);               //   07934068721

// test if padding correctly
string padded = data1.PadLeft(13, '0');         // 0007934068721

// textbox tbPadded is empty before adding text  
tbPadded.AppendText(data1);                     //   07934068721

// pad text
tbPadded.Text = tbPadded.Text.PadLeft(13, '0'); // 0007934068721
user2019047
  • 749
  • 7
  • 10
  • The string is coming from data in a scanner scale when a barcode is scanned, if I do not use append it does not show the entire buffer I guess is the right word, then taking that string data I try to subtring it and make the textbox= to it and nothing shows, so I put the string in a message box and I get first a blank message box and then after closing it I get the appropriate string in a second message box, not sure why this is. Also I may try to pad my data string as you showed I been padding the txtbox, I am using visual studio 2012, may be a bug on there part since its not padding correct – quatre432 May 26 '13 at 02:16
  • If I understand you correctly you append the substring to the textbox then try to pad it. You have to pad the substring first then append it to whatever text you have in the text box if there is any text there. – user2019047 May 26 '13 at 03:27
  • ok ill try that any ideal why I'm getting a blank after I substring before I get the actual data? – quatre432 May 26 '13 at 20:35
  • @quatre432 You will have to edit your code above to indicate EXACTLY where the problem occurs. At each line of code, indicate as a comment what the value is in the variable you are using, this includes the textbox text as well. You can answer your own question above by stepping through the code in the debugger and note every value of every variable (write it down on paper). If you still do not understand your error, post the results here and I will try to help. When you answer a poster, use @ as I did at beginning of this post. – user2019047 May 26 '13 at 21:28
  • @ That is my code above basically I scan a barcode the comport opens and captures it in string data the. Appends to the textbox, this works as long as I don't try to substring data, tommrow I will try and pad data1 and see of the first response I get is 13 0's or if its still blank, also every time I do the @ your name it disappears – quatre432 May 27 '13 at 02:12
0

After Much Playing around i discovered there there where invisible character in my textbox so using

textBox1.Text = textBox1.Text.Trim();

This got rid of the invisible characters allowing the padding to work correctly, i then changed my data received event to be on a timer to avoid cross thread issues like this

private void timer3_Tick(object sender, EventArgs e)
    {

        data = comport.ReadExisting();

        try
        {
            if (data != "")
            {
                textBox1.Clear();
                textBox1.AppendText(data);
                timer3.Stop();
                scan();
                timer3.Start();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        comport.DiscardInBuffer();

    }

My program is now working like i need. Thank you user2019047 for your help

quatre432
  • 47
  • 1
  • 11