Seriel kommunikation
HejJeg forsøger ved selvstudie at lære Java. I den forbindelse har jeg brug for at kommunikere over RS232 Com1 porten.
Jeg har bøffet flg. kode (se nedenfor) som jeg forsøger at få til at køre, men jeg får en "kryptisk" fejlmeddelese:
Error loading SolarisSerial: java.lang.UnsatisfiedLinkError: no SolarisSerialParallel in java.library.path
Caught java.lang.UnsatisfiedLinkError: readRegistrySerial while loading driver com.sun.comm.SolarisDriver
Jeg bruger JBuilder 2005 Foundation på en Win-XP maskine. Javax pakken har jeg hentet hos Sun (men det er måske ikke Windows versionen jeg har fået fat i ??)
package klasse2;
// *************************************************************************
// File name : SerialPort1.java
// Start date : 12-01-2003.
// Programmer : Hans So.
// Java : Java 1.3.1_05
// Description: Serial port example
//
// Revision history:
// date - init - comment
//
// *************************************************************************
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import javax.comm.SerialPort;
import javax.comm.CommPortIdentifier;
class SerialPortImpl
{
private SerialPort serialPort;
private CommPortIdentifier commPortId;
private InputStream inStream; // serial input stream
private OutputStream outStream; // serial output stream
public void initializeSerialPort()
{
try
{
commPortId = CommPortIdentifier.getPortIdentifier("com1");
// Open the serial port. The name is unimportant,
// The second (timeout) parameter is unimplemented
serialPort = (SerialPort)commPortId.open("com1",0);
// Set up the serial port:
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// Get output and input streams
outStream = serialPort.getOutputStream();
inStream = serialPort.getInputStream();
}
catch (Exception e)
{
System.out.println("Error: Exception bla bla " + e);
System.exit(1);
}
}
public void sendByte (byte b)
{
try
{
outStream.write (b);
}
catch (IOException e)
{
System.out.println("IOException" + e);
}
}
public byte receiveByte()
{
try
{
return (byte)inStream.read ();
}
catch (IOException e)
{
System.out.println("IOException" + e);
return 0;
}
}
public void sendBytes (byte[] bytes)
{
try
{
outStream.write (bytes, 0, bytes.length);
}
catch (IOException e)
{
System.out.println("IOException" + e);
}
}
public byte[] receiveBytes(int number) // not used in this example
{
byte[] bytes = null;
try
{
bytes = new byte[number];
inStream.read (bytes, 0, number);
}
catch (IOException e)
{
System.out.println("IOException" + e);
}
return bytes;
}
}
public class klasse2
{
public static void main (String [] args)
{
System.out.println("*** main start ***");
SerialPortImpl serialP = new SerialPortImpl();
serialP.initializeSerialPort();
String startStr = "\r\nEnter characters. End with x: ";
String endStr = "\r\nReceived x. The session ends now.";
byte endByte = 'x';
byte b;
serialP.sendBytes(startStr.getBytes());
do
{
b = serialP.receiveByte();
System.out.println("** Received byte: " + b + ". As char: " + (char)b);
serialP.sendByte (b); // echo the character back
} while (b != endByte);
serialP.sendBytes(endStr.getBytes());
}
}
