25. oktober 2004 - 11:37
#14
copy paster lige den kode jeg har, kan være det hjælper lidt:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.io.UnsupportedEncodingException;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
/**
*
* @author
* @version 0.1
*/
public class SimpleWrite {
static final String DEFAULT_PORT = "COM4";
static InputStream inputStream = null;
/** first of 3 init commands needed for reader to work*/
static final int initMessage1[] = {
0x01, 0x04, 0x06, 0x40, 0x00, 0x86, 0x81};
// other unknown init messages Matrics Tag Reader uses.
//01 04 08 27 02 02 00 AF 6B
//01 04 0D 0B 0B 00 00 00 00 00 9E 6A BA E9
//01 04 0D 0B 0B 00 00 00 00 00 9E 6A BA E9
//01 04 0B 0C 01 00 00 00 00 00 05 87
//01 04 27 0C 02 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 0E 2D
// set antenna filters
/** antenna filter command for antenna 1 */
static final int initMessage10[] = {
0x01, 0x04, 0x29, 0x23, 0x01, 0x00, 0x00, 0x00, 0xFF, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x11, 0xD5};
/** antenna filter command for antenna 2 - needed? */
static final int initMessage11[] = {
0x01, 0x04, 0x29, 0x23, 0x00, 0x01, 0x00, 0x00, 0xFF, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x67, 0x74};
// antenna 3+4 not used
//01 04 29 23 00 00 01 00 FF 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 83 28
//01 04 29 23 00 00 00 01 FF 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 3D 39
// read tags from antenna 1
/** command for full scan on antenna 1 */
static int readTagsMessageA[] = {
0x01, 0x04, 0x06, 0x10, 0xA0, 0x7B, 0xF7};
/**
* Test main, prototype only
*
* @param args - not used
*
*/
public static void main(String[] args) {
SerialPort serialPort;
OutputStream outputStream = null;
boolean portFound = false;
// get all ports
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId;
// iterate to find defaultport
while (portList.hasMoreElements() && !portFound) {
System.out.println("WHILE sætning!!");
portId = (CommPortIdentifier) portList.nextElement();
System.out.println(portId.getName() + " - " + portId.getPortType());
// look only at serial ports
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(DEFAULT_PORT)) {
System.out.println("Found port " + DEFAULT_PORT);
portFound = true;
// open a connection to specified serial port
try {
serialPort =
(SerialPort) portId.open("SimpleWrite", 2000);
}
catch (PortInUseException e) {
System.out.println("Port in use.");
continue;
}
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
}
catch (IOException e) {
}
// set transfer rates, and various bits
try {
serialPort.setSerialPortParams(
230400, // default for Matrics readers
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
}
try {
serialPort.notifyOnOutputEmpty(true);
}
catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit( -1);
}
System.out.println("Writing to " + serialPort.getName());
try {
String out;
// send init, this needs only be sent once
sendInitMessages(outputStream);
// read response from READ TAGS command
out = writeToReader(readTagsMessageA, outputStream);
// Her skriver den resultatet ud!
System.err.println(out);
}
catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(2000);
// Be sure data is xferred before reading
}
catch (Exception e) {
}
// clean up
serialPort.close();
}
}
}
if (!portFound) {
System.out.println("port " + DEFAULT_PORT + " not found.");
}
}
/**
* Sends initstatements to outputstream
*
* uses initmessages
*
* @param outputStream
* @throws IOException
*/
private static void sendInitMessages(OutputStream outputStream) throws
IOException {
writeToReader(initMessage1, outputStream);
writeToReader(initMessage10, outputStream);
writeToReader(initMessage11, outputStream);
}
private static String writeToReader(int[] message, OutputStream out) throws
IOException {
for (int i = 0; i < message.length; i++) {
out.write(message[i]);
}
out.flush();
try {
// if no wait, no response available, may need tuning
Thread.sleep(1000);
}
catch (InterruptedException e1) {
e1.printStackTrace();
}
String result = "No read available.";
try {
byte input[] = new byte[inputStream.available()];
System.out.println("Result length: " + inputStream.available());
// TODO does it make correct conversion from int->byte here? or does it need to be read one int at a time?
inputStream.read(input);
result = new String(input);
}
catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String decode(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '+':
sb.append(' ');
break;
case '%':
try {
sb.append( (char) Integer.parseInt(
s.substring(i + 1, i + 3), 16));
}
catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
i += 2;
break;
default:
sb.append(c);
break;
}
}
// Undo conversion to external encoding
String result = sb.toString();
try {
byte[] inputBytes = result.getBytes("8859_1");
result = new String(inputBytes);
}
catch (UnsupportedEncodingException e) {
// The system should always have 8859_1
}
return result;
}
}