Barcode Capture in how many lines of code ?


I’m working on a demo to show at Embedded World in Germany next month – My plan is to show how Windows CE, Windows XP Embedded, and Windows Embedded for Point of Service (WEPOS) can streamline your embedded operating system development experience in much the same way that developers have gradually moved from assembler to C/C++ to C/C++ with class libraries (OWL, MFC, STL, WTL etc…) and managed code – at each evolutionary step developers have been given a set of tools and technologies that accelerate their development experience – the same should also be true for embedded operating system development.


So, I want to write an application that runs on an embedded device that reads barcodes from a connected Symbol barcode scanner and display the barcode ‘text’ on a form/dialog/application.


How many lines of code do you think this will take ? – and how long should I spend writing the code to implement this application ?


– Mike

Comments (22)

  1. Ronald says:

    Are you testing me?

  2. tzagotta says:

    Unless there is some complicating factor, reading a bar code from a scanner is pretty trivial – just amounts to reading a CR-limited line of text from a serial port. Seems like a few minutes of work to me. Is there more to your application than that?

  3. Frank Zimmermann says:

    Should be :

    Open COM port

    Read string to programmed Symbol EOL char

    Display string

    Close COM port

    I am not using CF2.0 yet but i believe there is a COM port component right? We implemented the Symbol serial scanner in our CF1.0 based POS application and are using the Franson COM component. Works like a charm !

  4. > I want to write an application that runs on

    > an embedded device that reads barcodes from

    > a connected Symbol barcode scanner and display

    > the barcode ‘text’ on a form/dialog/application

    Are you going to compare also number of count GUI related lines too?

  5. mikehall says:

    let’s add a twist here…

    This is a USB barcode scanner, the code needs to be generic enough to deal with just about any barcode scanner being plugged into my device, so this shouldn’t matter whether this is an Intermec, HHP, Symbol, or other device – perhaps even a Wireless/USB scanner.

    – Mike

  6. mikehall says:

    oh, and the UI for this is going to be trivial, when you get content from the scanner drop this into a listbox.

    – Mike

  7. OK, so please let me subvert the myth of less-code-to-write using .NET :-)))

    After a small Googling I can *estimate* that C++ solution will occupy about 2 time less LOC than equivalent written in C#.

    Here is a file I found with Google and it’s a piece of C++ code using Symbol’s ScanCAPI. It’s 88 lines long. (Note: the function with strange Java_BarcodeReader_read can be considered as a client function called as a button click handler triggering scan operation).

    And here we have *almost* the same but not yet completed code in C# using the same ScanCAPI:

    http://www.opennetcf.org/Forums/post.asp?method=ReplyQuote&REPLY_ID=479&TOPIC_ID=275&FORUM_ID=12

    Yes, this is only a P/Invoke wrapper but it’s 174 LOC so I can safely say that it will increase to about 200-210 LOC after you add lines using this wrapper.

    Here is a prototype code in C++ + WTL adding barcode buffer data to listbox:

    // … get barcode buffer to string

    int len = SCNBUF_GETLEN(lpScanBuffer);

    WTL::CString str((TCHAR*)SCNBUF_GETDATA(lpScanBuffer));

    // attach listbox on dialog box to client var

    myList.Attach(IDC_LISTBOX1);

    // add string to the listbox

    myList.AddString(str);

    By the way, talking about language comparison in action, here are results of small competition where you can find the same task implemented in 18 languages:

    http://wlodarek.com/articles/Mini-Wyzwanie.aspx

    Unfortunately, the overview is written in polish language only, but I think every programmer can understand source code 🙂

  8. mikehall says:

    Mateusz, so your solution is going to use about 200 or so lines of C++ code, right ?

    I’ve just walked through my C# code and counted the number of lines of code I actually needed to write, beyond the boilerplate code generated by Visual Studio 2005 I added a grande total of….

    EIGHT LINES OF CODE to read content from a USB Barcode Scanner…

    Very Cool ? – You Bet ! – wanna see the code ? ;O)

    – Mike

  9. Ronald says:

    I do it in 5 lines

    private Intermec.Datacollection.BarcodeReader bcr;

    bcr = new Intermec.Datacollection.BarcodeReader();

    bcr.BarcodeRead +=new BarcodeReadEventHandler(bcr_BarcodeRead);

    bcr.ThreadedRead(true);

    private void bcr_BarcodeRead(object sender, BarcodeReadEventArgs bre)

    {

    this.MyTextBox.Text = bre.strDataBuffer;

    }

  10. > Mateusz, so your solution is going to use

    > about 200 or so lines of C++ code, right ?

    Not C++ code but 200 LOC of C# code.

    It’s aobut 80 LOC of C++ code.

    So, I estimate C++ solution as 2 time shorter than C#.

  11. Ups! Sorry Mike, in my first post I’ve forgot to include the C++ code for reading barcode from scanner.

    Here it is:

    http://www.sics.se/accord/release/toolbox/src/accord/toolbox/editors/PDAeditor/scanner.cpp

    I think my point is more clear now.

  12. Ronald:

    > I do it in 5 lines

    I don’t agree.

    You omitted code of your Intermec.Datacollection.* stuff which I bet is huge amount of P/Invoke playing code.

    Check this:

    http://www.opennetcf.org/Forums/post.asp?method=ReplyQuote&REPLY_ID=479&TOPIC_ID=275&FORUM_ID=12

    You have to write .NET CF binding yourself or you can buy it.

    Here is C code:

    http://www.sics.se/accord/release/toolbox/src/accord/toolbox/editors/PDAeditor/scanner.cpp

    OK, I can C-like API pack into tiny class and the usage will fit in a few lines of code.

  13. mikehall says:

    ok, when I get to the office I will post my C# code.

    Hey, what about supporting a Mag Stripe Reader (MSR) ? (or a two line secondary LCD display) – guess what, each of these are about 8-10 lines of extra code…

    More on this in a short while…

    – Mike

  14. Ronald says:

    Mateusz:

    >I don’t agree.

    >You omitted code of your Intermec.Datacollection.* stuff which I bet is huge amount of P/Invoke playing code.

    Yes, and your point is?

    Why don’t you add the number of lines in CF.NET as well?

    Fact remains.. 5 lines..

  15. captain jack says:

    I know this is not a barcode, but since we are on the topic i figured i would add the code for reading RFID tags. This is for use with the TradeWind Technologies SDIO RFID reader used in WinCE devices…….

    /*************************************************************

    **** Read and Write to 15693 RFID Tag *****

    **** Using Managed Code and ContactlessONE *****

    **** library From http://www.tradewindtek.com *****

    *************************************************************/

    using C1Lib;

    string tag_id="";

    string tag_data="";

    string data_to_write="this is a test";

    read_tag(); //read the tag

    write_tag(); //write to the tag

    private int read_tag()

    {

    //open the port and activate the RFID card

    if(C1Lib.C1.NET_C1_open_comm()!=1) return 0;

    if(C1Lib.C1.NET_C1_enable()!=1) {

    C1Lib.C1.NET_C1_close_comm();

    return 0;

    }

    int j=0;

    while(C1Lib.ISO_15693.NET_get_15693(0)==0)

    {

    if(j++==30) {

    C1Lib.C1.NET_C1_disable();

    C1Lib.C1.NET_C1_close_comm();

    return 0;

    } //tried 30 times and didn’t find tag. (this takes a few seconds)

    }

    //got the tag id, now convert into proper display text

    for(int i=0;i<C1Lib.ISO_15693.tag.id_length;i++)

    tag_id=tag_id+C1Lib.util.hex_value(C1Lib.ISO_15693.tag.tag_id[i]);

    //now read the tag!

    if(C1Lib.ISO_15693.NET_read_multi_15693(0,C1Lib.ISO_15693.tag.blocks)!=1) {

    C1Lib.C1.NET_C1_disable();

    C1Lib.C1.NET_C1_close_comm();

    return 0;

    } //failed to read the tag

    //great, we just read the RFID tag, now convert into proper text to display to screen

    tag_data=C1Lib.util.to_str(C1Lib.ISO_15693.tag.read_buff,256);

    //disable the card and close the port.

    C1Lib.C1.NET_C1_disable();

    C1Lib.C1.NET_C1_close_comm();

    return 1;

    }

    private int write_tag()

    {

    //open the port and activate the RFID card

    if(C1Lib.C1.NET_C1_open_comm()!=1) return 0;

    if(C1Lib.C1.NET_C1_enable()!=1) {

    C1Lib.C1.NET_C1_close_comm();

    return 0;

    }

    int j=0;

    while(C1Lib.ISO_15693.NET_get_15693(0)==0)

    {

    if(j++==30) {

    C1Lib.C1.NET_C1_disable();

    C1Lib.C1.NET_C1_close_comm();

    return 0;

    } //tried 30 times and didn’t find tag. (this takes a few seconds)

    }

    byte[] bytes=new byte[256];

    bytes=C1Lib.util.to_bytes(data_to_write); //convert the string to write to bytes

    //now write to the tag!

    if(C1Lib.ISO_15693.NET_write_multi_15693(0,C1Lib.ISO_15693.tag.blocks,bytes)!=1) {

    C1Lib.C1.NET_C1_disable();

    C1Lib.C1.NET_C1_close_comm();

    return 0;

    } //failed to read the tag

    //disable the card and close the port.

    C1Lib.C1.NET_C1_disable();

    C1Lib.C1.NET_C1_close_comm();

    return 1;

    }

  16. Ronald says:

    Is this a conspiracy or something? 😉

    To Read and Write RFID Tags:

    private Intermec.DataCollection.RFID.Reader rfid;

    private Intermec.DataCollection.RFID.XMLTagReader; xmlTagReader;

    rfid.Open("readerName");

    string XMLresult = rfid.Command("Read *"); // read everything, using field def. file

    xmlTagReader = new XMLTagReader(XMLresult); // parse the Data

    if (xmlTagReader.TagCount>0)

    {

    xmlTagReader.Tags[0].Fields[0].Data ="newdata"; // field 0 on first Tag

    rfid.Write(xmlTagReader.CreateDeltaXML()); // write data back

    }

    rfid.Close();

    There! Can we count lines now? 🙂

  17. Dan McCarty says:

    We have a bar code module in our application (quality control for packaging) and I think we do our bar code stuff in just 4,000 lines of code.

    🙂

  18. Ronald:

    > Yes, and your point is?

    > Why don’t you add the number of lines in CF.NET

    > as well?

    I did. Read my second comment to Mike’s post.

    > Fact remains.. 5 lines..

    I don’t get it. Your code is ONLY a client code, but you omitted the code behind the scene…implementation of real job which is hidden in the Intermec.Datacollection.BarcodeReader stuff.

  19. Dan McCarty, what language you mention?

  20. Dan McCarty says:

    Mateusz Łoskot: Combination of C/C++ running on CE 4.2. The actual reading of the various bar codes and color codes (pharmacode) doesn’t take 4,000 lines of code, but the code that handles the whole process does.

  21. Dan McCarty, thanks I understand it.

    I still state this comparison (as I understand the goal is to compare solutions based on C/C++/.NET, isn’t it?) does not makes any sense. I don’t see any result.