• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 43
  • Last Modified:

Interface Java app with barcode using ethernet cable

I am working on an automation project where i will be using couple of barcode readers to read the barcode printed on the books which would passing through the conveyor.  I develop my java application which continuously monitor the barcode reader and keep reading each barcode and gives me the value of that barcode. (manipulating with the received barcode data will be later).

I need to connect my barcode reader using ethernet to my laptop. Is there a sample code available how i interface my java code to the barcode reader, in such a way when it scan the barcode my java app will just displays the value respective to the barcode?
0
Kart Craze
Asked:
Kart Craze
  • 8
  • 8
1 Solution
 
CEHJCommented:
Really the interface is a matter of what the hardware manufacturer has made available. What does the documentation say?
0
 
Kart CrazeAuthor Commented:
Well, what i meant an interface is not relating to the physical interface but with my java code. Basically what i am trying to achieve is, write a java code which would connect to my bar code reader via Ethernet connection (ip address, port id). When reader scans any barcode (probably from a piece of paper where i have sample bar code) my java code just prints the respective value of it on the terminal.

Sample code below which i use, is there any other simple code available for use?

Socket bcSocket = null;
        try {
            bcSocket = new Socket("192.168.230.112",0); // port id unknown yet
            log.info("Port:" + bcSocket.getPort() + "\nLocal Port:" + bcSocket.getLocalPort() + "\nIP address:" + bcSocket.getInetAddress() + "\nLocal IP address" + bcSocket.getLocalAddress());
            DataInputStream dips = new DataInputStream(bcSocket.getInputStream());

            while(dips.available()>0){
                bcData = dips.readUTF();
            }
        } catch (IOException e) {
            log.error("Barcode reader encountered an error:", e);
        } finally {
            bcSocket.close();
        }
0
 
CEHJCommented:
Well, what i meant an interface is not relating to the physical interface but with my java code.
I wasn't referring to the physical interface either. Any kind of interface is dependent on what the device manufacturer has made available

write a java code which would connect to my bar code reader via Ethernet connection (ip address, port id).
Does the manufacturer allow that? If so, in what way?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Kart CrazeAuthor Commented:
Yes, the product keyence sr1000 series allows us to connect with PC or PLC via USB or Ethernet. Actually , today i was able to connect to my barcode reader from my java code, I.e i am able to ping, get the IP address , port details of my barcode reader.

My next step is to read the data from barcode reader when it scans.
0
 
CEHJCommented:
My next step is to read the data from barcode reader when it scans.
Yes, so you need to find out the interface for that. Often with barcode readers with a Java interface (don't know whether yours has one) that actually happens via reading a windowing text field
0
 
Kart CrazeAuthor Commented:
Well, i am not familiar with what you meant by 'windowing text field'. I read my barcode device document for the communication part. It can receive and send responses via TCP (as i use ethernet). For an example, to ON the barcode reader, we can send the command "LON[CR]". I use the below code, but not working fine as it timed out everytime.

        Socket bcSocket = null;

        try {
            bcSocket = new Socket("192.168.100.100", 9004);
            bcSocket.setSoTimeout(5000);

            BufferedReader br = new BufferedReader(new InputStreamReader(bcSocket.getInputStream()));
            DataOutputStream dos = new DataOutputStream(bcSocket.getOutputStream());

            String bcReadCMD = "LON[CR]";
            dos.writeUTF(bcReadCMD);

            bcSocket.setSoTimeout(5000);
            retour = br.readLine();
0
 
CEHJCommented:
"LON[CR]".

That should be "LON" plus a carriage return. You would be better off opening a PrintWriter on the socket output stream, then do

PrintWriter out = new PrintWriter(bcSocket.getOutputStream());
out.printLine("LON");

Open in new window

1
 
Kart CrazeAuthor Commented:
Tired with "LON" as well, no luck. Just tried with your solution, same error

java.net.SocketTimeoutException: Read timed out
0
 
CEHJCommented:
I would begin by reading everything from the server in a separate thread
0
 
Kart CrazeAuthor Commented:
Great help from you!

Finally i was able to speak to my barcode from the java code. I meant to say, i am able to switch the barcode to scan mode and is working. As a next step, need to get data read by the barcode reader to the app.

Code used:
 bcSocket = new Socket("192.168.100.100", 9004);
            bcSocket.setSoTimeout(5000);

            BufferedReader br = new BufferedReader(new InputStreamReader(bcSocket.getInputStream()));
           // DataOutputStream dos = new DataOutputStream(bcSocket.getOutputStream());

            OutputStreamWriter osw = new OutputStreamWriter(bcSocket.getOutputStream());
            PrintWriter out = new PrintWriter(osw);


            String bcReadCMD = "LON";
            out.println(bcReadCMD);
            osw.flush();
0
 
CEHJCommented:
If you want to use a Writer, then chain it. Also if you use this call, you don't need to call flush()

PrintWriter out = new PrintWriter(new OutputStreamWriter(bcSocket.getOutputStream), true);

Open in new window

0
 
Kart CrazeAuthor Commented:
Ok, that works so far to connect with my barcode. Yet another issue is that i am still unable to get the data read by the barcode reader. I am able to get the response from the barcode reader for every command i pass, but the expected barcode data is not able to receive.

Ex:

You send the cmd LON , the barcode responds OK,LON

The barcode enters into read mode when i send the command, but no response after that. I can verify the barcode data from the device itself.
0
 
CEHJCommented:
I've got to say that a 'pull' protocol seems like an odd way to communicate with a barcode reader. What would happen if your pulls were out of synch with its reads? Say you have a fast operator ...
0
 
Kart CrazeAuthor Commented:
Thanks for your help so far. I am moving to utilize apache Mina java api  for further use. However the basic communication methods remain same and i learned it with your help.
0
 
Kart CrazeAuthor Commented:
Havelearbt with the basic communication methods with the help provide here. There is an api  apache Mina available which might helps more.
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now