|
没教程,还真不容易啊 ~!全都是JAVA
NXT端:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.UltrasonicSensor;
import lejos.nxt.comm.USB;
import lejos.nxt.comm.USBConnection;
public class NxtToPc {
public static USBConnection USBLink;
public static DataOutputStream dataOut;
public static DataInputStream dataIn;
public static boolean b;
public static int ReceivedData; //接收PC传入的数
public static int speed=60; //马达速度
public static int turnspeed=100; //转弯速度
public static int movespeed; //移动速度
static byte[] OutData={0,0} ; //输出数组,用于保存几个数值
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final UltrasonicSensor us=new UltrasonicSensor(SensorPort.S4);
b=false;
//监听ESCAPE按钮
Button.ESCAPE.addButtonListener(new ButtonListener()
{
@Override
public void buttonReleased(Button arg0) {}
@Override
public void buttonPressed(Button arg0) {
System.exit(0);
}
});
connect();
Runnable runnable1 = new Runnable(){
@Override
public void run() {
while(true)
{
try
{
LCD.clearDisplay();
LCD.drawString("Ultra: "+us.getDistance(), 0, 4);
OutData[0]=(byte) us.getDistance();
OutData[1]=(byte) Motor.A.getSpeed();
dataOut.write(OutData,0,2); //写数据方式
dataOut.flush(); //刷出数据到PC
}
catch (IOException e) {e.printStackTrace();}
}
}};
Runnable runnable2 = new Runnable(){
@Override
public void run() {
while(true)
{
try {
ReceivedData=dataIn.readInt();
Motor.C.setSpeed(speed);
Motor.A.setSpeed(speed);
if(ReceivedData==1) //向前
{
Motor.C.forward();
Motor.A.forward();
}
if(ReceivedData==5) //停止
{
Motor.A.stop();
Motor.C.stop();
}
if(ReceivedData==6) //加速
{
if(speed>720)
speed=720;
else
speed+=60;
}
if(ReceivedData==7) //减速
{
if(speed<60)
speed=60;
else
speed-=60;
}
if(ReceivedData==2) //向后
{
Motor.A.backward();
Motor.C.backward();
}
if(ReceivedData==3) //转右
{
Motor.A.setSpeed(turnspeed);
Motor.C.setSpeed(turnspeed);
Motor.A.backward();
Motor.C.forward();
}
if(ReceivedData==4) //转左
{
Motor.A.setSpeed(turnspeed);
Motor.C.setSpeed(turnspeed);
Motor.A.forward();
Motor.C.backward();
}
} catch (IOException e) {e.printStackTrace();}
}
}};
Thread thread1 = new Thread(runnable1);
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
}
private static void connect() {
//BTLink = Bluetooth.waitForConnection();
//dataOut = BTLink.openDataOutputStream();
//dataIn = BTLink.openDataInputStream();
System.out.println("Listening");
USBLink = USB.waitForConnection();
dataOut = USBLink.openDataOutputStream();
dataIn = USBLink.openDataInputStream();
}
}
PC端:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import lejos.pc.comm.*;
import java.awt.Font;
public class PcToNxt {
//自定义
static boolean connected; //连接
static DataInputStream dis; //接收数据
static DataOutputStream dos; //发送数据
public static int getdata; //用于接收dis
static NXTConnector conn; //连接
static byte[] InData={0,0};
private static JFrame f;
private static JTextField text;
private static JButton btnW;
private static JTextField txta;
public static void main(String[] args) throws InterruptedException
{
f = new JFrame("控制NXT");
f.getContentPane().setLayout(null);
text = new JTextField();
text.setBounds(135, 272, 193, 21);
f.getContentPane().add(text);
f.setSize(379,335);
f.setVisible(true);
text.setText("\u8D85\u58F0\u6CE2\u4F20\u611F\u5668");
JButton btnConnect = new JButton("连接");
btnConnect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
conn = new NXTConnector(); //创建连接
connected = conn.connectTo("usb://"); //连接方式使用USB
Runnable runnable = new Runnable() //实现Runnable()用于获取传感器数据线程
{
@Override
public void run()
{
while(true) //循环用于检测是否能连接上
{
if (!connected)
{
System.err.println("连接NXT失败"); //判断是否连接
System.exit(1); //直接退出程序
}
dis = conn.getDataIn(); //取值数据类型为INT
while(true) //循环接收数据
{
try{
dis.read(InData, 0,2);//接收数组变量
text.setText( "超声波传感器距离: "+InData[0]); //接收回来的值赋给text
txta.setText( "马达A的速度: "+InData[1]);
}
catch (IOException ioe) {break;}//出错的时候终止循环
}
}
}
};
Thread thread = new Thread(runnable); //创建线程
thread.start(); //开启线程
}
});
btnConnect.setBounds(10, 240, 93, 23);
f.getContentPane().add(btnConnect);
btnW = new JButton("\u524DW");
btnW.setFont(new Font("宋体", Font.PLAIN, 10));
btnW.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
dos=conn.getDataOut(); //输出数据
try
{
dos.writeInt(1); //写数据类型
dos.flush(); //刷出数据到NXT
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
@Override
public void mouseReleased(MouseEvent e)
{
dos=conn.getDataOut();
try
{
dos.writeInt(5);
dos.flush();
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
});
btnW.setBounds(64, 10, 55, 55);
f.getContentPane().add(btnW);
JButton btnS = new JButton("后S");
btnS.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
dos=conn.getDataOut(); //输出数据
try
{
dos.writeInt(2); //写数据类型
dos.flush(); //刷出数据到NXT
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
@Override
public void mouseReleased(MouseEvent e) {
dos=conn.getDataOut(); //输出数据
try
{
dos.writeInt(5); //写数据类型
dos.flush(); //刷出数据到NXT
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
});
btnS.setFont(new Font("宋体", Font.PLAIN, 10));
btnS.setBounds(64, 64, 55, 55);
f.getContentPane().add(btnS);
JButton btnA = new JButton("左A");
btnA.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
dos=conn.getDataOut(); //输出数据
try
{
dos.writeInt(4); //写数据类型
dos.flush(); //刷出数据到NXT
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
@Override
public void mouseReleased(MouseEvent e) {
dos=conn.getDataOut(); //输出数据
try
{
dos.writeInt(5); //写数据类型
dos.flush(); //刷出数据到NXT
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
});
btnA.setFont(new Font("宋体", Font.PLAIN, 10));
btnA.setBounds(10, 64, 55, 55);
f.getContentPane().add(btnA);
JButton btnD = new JButton("右D");
btnD.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
dos=conn.getDataOut(); //输出数据
try
{
dos.writeInt(3); //写数据类型
dos.flush(); //刷出数据到NXT
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
@Override
public void mouseReleased(MouseEvent e) {
dos=conn.getDataOut(); //输出数据
try
{
dos.writeInt(5); //写数据类型
dos.flush(); //刷出数据到NXT
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
});
btnD.setFont(new Font("宋体", Font.PLAIN, 10));
btnD.setBounds(118, 64, 55, 55);
f.getContentPane().add(btnD);
JButton btnJ = new JButton("\u52A0J");
btnJ.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dos=conn.getDataOut(); //输出数据
try
{
dos.writeInt(6); //写数据类型
dos.flush(); //刷出数据到NXT
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
});
btnJ.setFont(new Font("宋体", Font.PLAIN, 10));
btnJ.setBounds(218, 10, 55, 55);
f.getContentPane().add(btnJ);
JButton btnK = new JButton("\u51CFK");
btnK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dos=conn.getDataOut(); //输出数据
try
{
dos.writeInt(7); //写数据类型
dos.flush(); //刷出数据到NXT
}
catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
}
});
btnK.setFont(new Font("宋体", Font.PLAIN, 10));
btnK.setBounds(218, 64, 55, 55);
f.getContentPane().add(btnK);
txta = new JTextField();
txta.setText("\u9A6C\u8FBEA\u901F\u5EA6");
txta.setBounds(135, 241, 193, 21);
f.getContentPane().add(txta);
txta.setColumns(10);
JButton btnClose = new JButton("\u9000\u51FA");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
btnClose.setBounds(10, 271, 93, 23);
f.getContentPane().add(btnClose);
}
}
|
|