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?
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