Witam, mam do napisania program, który będzie odbierał dane z myszki podpiętej przez port com.
Program musi działać na linuksie, a ponieważ nie mam normalnego coma tylko używam przejściówki która pod linuksem w żaden sposób nie chce działać to zdecydowałem sie żeby pisać w javie. Używam biblioteki RXTXcomm informacje do tej pory czerpałem stąd:
http://www.everything2.com/index.pl?node_id=1413990
http://local.wasp.uwa.edu.au/~pbourke/dataformats/serialmouse/
klasa która ma odbierać dane od myszy wygląda tak:
package driver;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JTextArea;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
public class Mysza {
private Thread mouse = null;
public void Stop()
{
mouse.interrupt();
}
public void Sledz(String PortName, JTextArea txt)
{
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(PortName);
CommPort Port = portIdentifier.open(this.getClass().getName(), 2000);
if (Port instanceof SerialPort)
{
SerialPort serialPort = (SerialPort) Port;
serialPort.setSerialPortParams(1200, SerialPort.DATABITS_7,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setRTS(true);
try {
InputStream in = serialPort.getInputStream();
mouse = new Thread(new Watch(in, txt));
mouse.start();
} catch (IOException e) {
System.out.println("Błąd odczytu");
}
}
} catch (NoSuchPortException e) {
System.out.println("Nie ma takiego portu");
} catch (PortInUseException e) {
System.out.println("Port jest używany");
} catch (UnsupportedCommOperationException e) {
System.out.println("Niedozwolona operacja");
}
}
public class Watch implements Runnable
{
InputStream input;
JTextArea txt;
public Watch(InputStream in, JTextArea text)
{
txt = text;
input = in;
}
public void run() {
byte[] buffer = new byte[3];
int len = -1;
System.out.print(len);
try
{
while ( ( len = this.input.read(buffer)) > -1 )
{
for(int i=8; i>=0; i--)
{
if(((1L << i) & buffer[0])!=0)
{
txt.setText(txt.getText()+"1");
}else
{
txt.setText(txt.getText()+"0");
}
}
txt.setText(txt.getText()+"\n");
for(int i=8; i>=0; i--)
{
if(((1L << i) & buffer[1])!=0)
{
txt.setText(txt.getText()+"1");
}else
{
txt.setText(txt.getText()+"0");
}
}
for(int i=8; i>=0; i--)
{
if(((1L << i) & buffer[2])!=0)
{
txt.setText(txt.getText()+"1");
}else
{
txt.setText(txt.getText()+"0");
}
}
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
}
Może mi ktoś powiedzieć co tu jest nie tak?
Siedze nad tym już kilka dobrych dni i nie wiem o co może chodzić :/