how to simulate barcode scanner for my java application? - java

I created a Java application and want to use barcode scanner in my Java application.
but don't have a device Barcode Scanner
How can I simulate a Barcode Scanner for testing my Java Application?

It really depends on how you want the scanner to connect to the system later on.
There are scanners that just use keyboard emulation. In that case you don't need to do anything (just make sure the right input box is active when expecting barcode input).
Other scanners connect to the system through a serial port emulation (for example, there's an USB to serial driver for Symbol/Motorola and Datalogic gun scanners). In that case, you open the serial port in Java and get scanner input as serial data. To simulate this, you'd have to connect your PC to another PC using a cross-over RS232 cable and could then use Hyperterminal/Putty/[whatever there is on linux or other OSs] to send data to your PC over the serial cable.

If you are running your application from the console,
Scanner scan = new Scanner(System.in);
String barcode = scan.nextLine();
Otherwise just pass your barcode to main method args.

I had a similar need and found the barcode-simulator project on GitHub. It covers a portion of the concerns raised in the comments, at least for the initial testing.
Still, there is nothing like the real deal. Expect that the mix of real users and real scanners are going to find unexpected holes in your application.
Provide really clear input to your clients:
When the users cursor is in the correct field have it change color and
show a message like "Waiting for Scan",
Clearly and quickly show if a scan is valid or invalid,
Make it trivial to rescan.
If your users are clear about what the path of success looks like then it should be good.

Related

Java concurrent console IO

I wrote multithreaded java server-client sockets app with messaging functionality but I encountered a problem with simultaneous console IO.Main server console is listening for keyboard input and simultaneously printing out messages from the clients. On client side there is a separate thread for printout.
Here is simplified code representation:
public class ServerThread{
....
BufferedReader in = ... (sock.getInputStream);
while(true){
System.out.println(in.readline());
....
public class ServerMain{
.....
BufferedReader keyb = ... (System.in);
while(true){
in = keyb.readLine();
....
The problem occurs while I'm typing something in the main server console and at the same time a message arrives from one of the clients.
That message is then concated to what I was typing on screen and cursor moves to the beginning of the next line waiting for input.
What was typed in previously is stuck in the keyboard buffer, and I cant edit it anymore. Same problem happens on client side.
The question is how can I print messages on screen without disrupting ongoing input?
(inputted text also needs to stay printed on screen as in readLine() default behavior)
I already tried some of the solutions suggested for other similar problems:
In Lanterna and JCurses libraries there's no support for native System.IO streams. I would have to reinvent the wheel and implement it all by myself manually from memory to screen, one char at a time plus build whole console GUI layer.
The other thing was using ANSI codes but I couldn't figure out how to do what I need with them. I could read one input char at a time instead of a whole line, then if message arrives clear the line, move cursor to the beginning and printout, but afterwards in nextline I don't know how to print previously buffered text and still be able to delete chars with backspace.
edit:
GUI is not an option as I want my code to be able to run on a headless server.(also assume that there will be only one terminal, console, shell, and app running per side)
A distinct non-answer, based on: there is only one console.
And that console is an artefact from times when multiple threads weren't a real problem. "Works nicely with multiple threads" was never a requirement for that low level console.
Thus: if you really want a sophisticated solution (that isn't a hack of some sort) simply consider: not using the stdin/stdout console(s).
Instead: write a simple Swing GUI application. Have one text entry field where input is collected, and one or maybe multiple text fields where your output goes. If you want to be fancy, make it a webapp. I am sure that using some framework, you could put together a working thing within a few hours. You will learn more valuable skills by doing that, instead of spending these hours "working around" the fact that you picked the wrong technology for your problem.
Update, given the comment by the OP: then the best I can think of: don't write to the console. Write to different files. Open multiple terminals, and use tools like "tail" to show you what is happening with your output file(s).
Ok, I found the ideal solution myself:
JLine library works in conjunction with default System.IO, also there is no need to create new Terminal objects (you can) or anything else. Simply instead of BufferedReader you use LineReader
String readLine(String prompt, Character mask, String buffer)
prompt (can be null) is the unexpanded prompt pattern that will be displayed to the user on the left of the input line
mask (can also be null) is the character used to hide user input (when reading a password for example)
buffer is the initial content of the input line
Edit: In JLine's docs i found an even better solution:
printAbove
void printAbove(AttributedString str)
Prints a string before the prompt and redraw everything. If the LineReader is not actually reading a line, the string will simply be
printed to the terminal.
Parameters:
str - the string to print*

How to take standard input from user and use that across Appium Test Case?

I have a scenario where I want to take User Standard Input from the console (Using Scanner(System.in) utility in Java).
Like, When a particular text field is being appeared then User need to type the input text as standard input rather than a device itself.
I am trying below code:
System.out.println("Enter the User Input: ");
Scanner sc = new Scanner(System.in);
String inputForTextField = sc.next();
getTextField().sendKeys(inputForTextField);
But the execution is getting stuck on String inputForTextField = sc.next(); User is not able to enter the text on the console and after 60 seconds Appium session is getting timed out.
Any help/suggestion would really be appreciated!
I am also open to hearing suggestion if I can take input from User using some Java AWT prompt, I just wanted to take user input from the computer rather than the device itself.
Thanks
You can supply the values using dataprovider(Excel/Json files) or even using config.properties file. In automation, it is machine which does all the inputs, human intervention defeats the automation purpose.

How do I correctly capture data from a Symbol LS2208 barcode scanner with java

I have been asked to develop a java desktop swing application that reads barcodes and processes data based on products with the relevant barcode.
The barcode scanner we're using is a Symbol LS2208 usb scanner and when it came there was no instructions or cd in the box. We plugged it in, it beeped at us and were able to scan barcode values into notepad as a test.
In my application, how do I ensure that the scanner populates data into the relevant textbox and how do I know when the entire barcode has been scanned?
Or how to get barcode Scannar input in my application?
James already answered most of your questions. This question is already a little bit old, but this might serve people with the same question in the future.
When you connect your scanner through USB, in particular the Zebra scanner (Symbol is part of Zebra Technologies nowadays), it will typically be recognized as a USB HID device by default. See the link Fildor shared for more information on HID Emulation.
So each time you scan the barcode scanner will translate the encoded information as keystrokes since it's emulating a keyboard.
If there is a text field in your application in which you can type, it should also be possible for you to scan into it.
For more information, please see this portal for the PRG (product reference guide).
For Zebra's configuration utility, 123ScanĀ². With the latter you can set the scanner to send a suffix or a prefix, such as Enter or Tab.
You can work with your scanner via System.in if you connect it to USB. Also you can use USB-COM emulator and work with it as you work with com-port. More information here
http://rxtx.qbang.org/wiki/index.php/Writing_%22Hello_World%22_to_a_USB_to_serial_converter
This type of scanner is a "Human Interface Device," also known as an HID. This type of scanner can provide input to software anywhere a keyboard can be used to provide input.
In my application, how do I ensure that the scanner populates data
into the relevant textbox?
Your application would not normally be responsible for that. Normally, the user has the responsibility of using the scanner when the relevant textbox has the focus.
How do I know when the entire barcode has been scanned?
The scanner should take care of that. It should transmit its data only when it senses a complete barcode has been scanned.
How to get barcode Scannar input in my application?
The same way you would get data typed from a keyboard in your application. You can use stdin (System.in in Java). If you are using a GUI, you can use a text field, text box, text edit pane, etc.
So, the short answer is this: Write the application as if you didn't have a barcode scanner.
This applies regardless of the language you use to code your application.

Java continuous console while running

Recently I have been making a little java game where you build up a city and have to defend it. Now I want to make it ready for proper testing. I was thinking of having the server running on an old computer of mine, than I would have to make it so that, while the program is running, I can type in commands that will be executed. Like the minecraft or craftbukkit servers have. There you can type in text and press enter in the same console as where you started the program.
Anybody got any ideas?
Sounds like you just need a Scanner object.
For example, to continuously wait for input and then print that input, you would write:
Scanner s = new Scanner(System.in);
String foo = s.nextLine();
System.out.println(foo);
System.in as the constructor parameter specifies that you are reading input from the keyboard (as opposed to a file, for example).

convert serial port sounds into microphone input

Assume that there are two computers, two headsets with microphone.One PC (A) is having a user and one PC (B) does not have a user, but it is having a software which makes automated calls. Now B making a call to A and Playing something like "Please say 'DONE' if you already in your seat." Then A response for it by saying "DONE". Now here what I want to do is, When that person response it comes to the speaker as incoming audio.I want to make that same incoming voice as the input for microphone. because my voice recognition software only convert microphone inputs to words.
If someone understand my problem please reply/advice me. Upto now I used a windows function in my sound card (Listen to the device), but as the solution its not worthy since that voice is not clear and voice volume not enough for voice recognition.
Thank you.

Categories

Resources