Click here to Skip to main content
15,896,487 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Problem

When split msg variable to more lines it not give me values in lines ?
msg variable in debug give me text scanning bellow


Details

i work in windows form c# vs 2015

i using bar code reader to read qr code

bar code scanner working as USB keyboard and define as HID Drivers

if i replace msg variable with text box it working in read data and spiting

SUCCESS so that

what is the problem in splitting below code ?

text scanning :

30 General Conference of Arab Pharmaceutical Unions

UserName : khalid ramzy

Country : Saudia

Membership : part

Serial : 1014

my code



C#
public partial class Form1 : Form
    {
        DateTime _lastKeystroke = new DateTime(0);
        List<char> _barcode = new List<char>(10);
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
            if (elapsed.TotalMilliseconds > 100)
                _barcode.Clear();

            // record keystroke & timestamp
            _barcode.Add(e.KeyChar);
            _lastKeystroke = DateTime.Now;

            // process barcode
            if (e.KeyChar == 13 && _barcode.Count > 0)
            {

                    string msg = new String(_barcode.ToArray());

                string[] lines = msg.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                if (lines.Length > 5)
                {
                    string msg1 = lines[1].Substring(lines[1].IndexOf(":") + 1);
                    label3.Text = msg1;
                    string msg2 = lines[2].Substring(lines[2].IndexOf(":") + 1);
                    label4.Text = msg2;
                    string msg3 = lines[3].Substring(lines[3].IndexOf(":") + 1);
                    label5.Text = msg3;
                    string msg4 = lines[4].Substring(lines[4].IndexOf(":") + 1);
                    label6.Text = msg4;
                    label2.Text = lines[5].Substring(lines[5].IndexOf(":") + 1);
                }
                    _barcode.Clear();
            }
        }
    }
}


msg variable in debug have values of scanning qr code

but problem now

How to split them to lines as above ?

What I have tried:

when split msg variable to more lines it not give me values in lines?
Posted
Updated 24-Feb-17 1:12am

1 solution

Have you loaded the data into a HexViewer? You heed to confirm if the break/seperator is a CR, NL, or CR+NL character(s). The scanner may not be using the same as the device/PC running your code, so Environment.NewLine may be a mismatch.
 
Share this answer
 
Comments
ahmed_sa 24-Feb-17 7:19am    
so that how loaded the data into a HexViewer?
Graeme_Grant 24-Feb-17 7:25am    
Yes, look at the bytes. You are obviously unaware of what the break/separator character(s) are.
ahmed_sa 24-Feb-17 10:32am    
i open file and scan qr and read data by scanner to this file after that install hex viewer and open file scanned or readed data to it i get values but i dont know what separator or break so that what i do
ahmed_sa 24-Feb-17 10:38am    
http://www.mediafire.com/view/dbpn167nne76dwz/readed_data_from_file.jpg
Graeme_Grant 24-Feb-17 10:44am    
so 0xOD 0xOA = CR+LF = "\r\n"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900