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?
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?
Really the interface is a matter of what the hardware manufacturer has made available. What does the documentation say?
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.g etInputStr eam());
while(dips.available()>0){
bcData = dips.readUTF();
}
} catch (IOException e) {
log.error("Barcode reader encountered an error:", e);
} finally {
bcSocket.close();
}
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
log.info("Port:" + bcSocket.getPort() + "\nLocal Port:" + bcSocket.getLocalPort() + "\nIP address:" + bcSocket.getInetAddress() + "\nLocal IP address" + bcSocket.getLocalAddress()
DataInputStream dips = new DataInputStream(bcSocket.g
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?
ASKER
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.
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
ASKER
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 .getInputS tream()));
DataOutputStream dos = new DataOutputStream(bcSocket. getOutputS tream());
String bcReadCMD = "LON[CR]";
dos.writeUTF(bcReadCMD);
bcSocket.setSoTimeout(5000 );
retour = br.readLine();
Socket bcSocket = null;
try {
bcSocket = new Socket("192.168.100.100", 9004);
bcSocket.setSoTimeout(5000
BufferedReader br = new BufferedReader(new InputStreamReader(bcSocket
DataOutputStream dos = new DataOutputStream(bcSocket.
String bcReadCMD = "LON[CR]";
dos.writeUTF(bcReadCMD);
bcSocket.setSoTimeout(5000
retour = br.readLine();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tired with "LON" as well, no luck. Just tried with your solution, same error
java.net.SocketTimeoutExce ption: Read timed out
java.net.SocketTimeoutExce
I would begin by reading everything from the server in a separate thread
ASKER
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 .getInputS tream()));
// DataOutputStream dos = new DataOutputStream(bcSocket. getOutputS tream());
OutputStreamWriter osw = new OutputStreamWriter(bcSocke t.getOutpu tStream()) ;
PrintWriter out = new PrintWriter(osw);
String bcReadCMD = "LON";
out.println(bcReadCMD);
osw.flush();
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
// DataOutputStream dos = new DataOutputStream(bcSocket.
OutputStreamWriter osw = new OutputStreamWriter(bcSocke
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);
ASKER
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.
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 ...
ASKER
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.
ASKER
Havelearbt with the basic communication methods with the help provide here. There is an api apache Mina available which might helps more.
:)