wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes help using javax.comm api Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "help using javax.comm api" Watch "help using javax.comm api" New topic

help using javax.comm api

blackcarlos Dibia

Joined: Mar 30, 2005
Posts: 21
pls I'm trying to read fro a USB metrologic scanner the code I'm using works but it reads the barcode in two parts
here is the code can anyone help I need it to read the barcode at once

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public SimpleRead()
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}

try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}

try {
} catch (TooManyListenersException e) {}


try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
} catch (UnsupportedCommOperationException e) {}

readThread = new Thread(this);


public static void main(String[] args)
boolean portFound = false;
String defaultPort = "COM5";

if (args.length > 0) {
defaultPort = args[0];

portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port: "+defaultPort);
portFound = true;
SimpleRead reader = new SimpleRead();
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");


* Method declaration
* @see
public void run() {
try {
} catch (InterruptedException e) {}

* Method declaration
* @param event
* @see
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {

case SerialPortEvent.BI:

case SerialPortEvent.OE:

case SerialPortEvent.FE:

case SerialPortEvent.PE:

case SerialPortEvent.CD:

case SerialPortEvent.CTS:

case SerialPortEvent.DSR:

case SerialPortEvent.RI:

case SerialPortEvent.OUTPUT_BUFFER_EMPTY:

case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[30];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);

System.out.print(new String(readBuffer));
} catch (IOException e) {}


Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60752

"blackcarlos Dibia", you have previously been warned on multiple occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional, and this is your final warning. Adjust your display name to comply with the required standards prior to your next post.

Failure to comply will result in the removal of your account.

JavaRanch Sheriff

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
I agree. Here's the link: http://aspose.com/file-tools
subject: help using javax.comm api
Similar Threads
Send SMS by using Serial Port
Java Comm API
unable to read the data from serial port in rfid
how can I send the output of a servlet to a jsp page?
Comm API