Link to home
Start Free TrialLog in
Avatar of Kart Craze
Kart Craze

asked on

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?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Really the interface is a matter of what the hardware manufacturer has made available. What does the documentation say?
Avatar of Kart Craze
Kart Craze

ASKER

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();
        }
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?
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.
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
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();
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tired with "LON" as well, no luck. Just tried with your solution, same error

java.net.SocketTimeoutException: Read timed out
I would begin by reading everything from the server in a separate thread
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();
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

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.
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 ...
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.
Havelearbt with the basic communication methods with the help provide here. There is an api  apache Mina available which might helps more.
:)