我正在尝试使用摩托罗拉MC3090符号设备编写一个小应用程序来读取条形码。应用程序必须在PC上运行,而不是在设备上运行,并且必须用JAVA编码。
条形码在设备上扫描,使用DataWedge 3.3。
当我在设备上使用写字板时,条形码也会被扫描。
现在,问题是如何将这些扫描的条形码传输到PC上。
我已经测试了这段代码,以知道PC是否找到了设备的端口:
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 static void main(String[] args) {
boolean portFound = false;
String defaultPort = "COM1";
if (args.length > 0) {
defaultPort = args[0];
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
System.out.println("port existed");
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.");
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
}
}
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:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
System.out.print("The Read Bytes from SerialPort are");
System.out.write(readBuffer);
System.out.println();
}
System.out.print(new String(readBuffer));
} catch (IOException e) {
}
break;
}
}
}
port COM1 not found.
这是一个问题。
我试了另一种方法,一个直接的方法
public static void main(String[] args) {
String filePath = "??????????";
readFile(filePath);
}
public static void readFile(String filePath){
try {
Scanner scanner = new Scanner(new File(filePath));
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
}
但是这种方式的问题是我不知道文件路径,因为设备没有像存储设备一样连接到PC上。
那么如何检索扫描到PC上的条形码呢?
MC3000系列运行的是Windows CE 4.2。
没有串行连接,除非您将串行适配器(如果可用)连接到设备。
当设备插入到Windows PC (USB或串行)时,设备将启动ActiveSync,并且在PC上必须安装ActiveSync或WMDC以获得设备和PC之间的连接。这种连接可以通过C/c++ RAPI使用(参见在java中通过USB将文件从PDA复制到PC)或使用网络。ActiveSync创建一个PPP连接,设备获得一个IP。或者,对于运行Windows Mobile 6的新设备,您可以获得一个连接到设备的虚拟网络适配器(RNDIS)。
如果连接了ActiveSync,可以指定PC和设备之间要同步的目录等。或者使用PPP网络传输文件或数据。
如果你有一个正在运行的无线局域网,如果你把设备连接到你的无线局域网,一切都会变得更容易。