找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 5716|回复: 1

請問一下藍芽一對多??

[复制链接]
发表于 2011-12-10 21:50:09 | 显示全部楼层 |阅读模式
大家好...
想請大家幫忙{:soso_e101:}
這是我們的專題作業..
import lejos.pc.comm.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class MouseAction implements MouseListener //滑鼠事件
{
        DataOutputStream dataOut;
        int cmd;
       
        public MouseAction(DataOutputStream _dataOut, int _cmd)
        {
                dataOut = _dataOut;
                cmd = _cmd;
        }//建構子
       
        public void mousePressed(MouseEvent e) //滑鼠壓下事件
        {
                try{
                dataOut.writeInt(cmd); //送出命令
                dataOut.flush();
                } catch(IOException i) {
                        JOptionPane.showMessageDialog(null, "Communication disconnected",                                                 "Error", JOptionPane.ERROR_MESSAGE); //斷線錯誤警告
                        System.exit(1);
                }
        }//mousePressed
       
        public void mouseReleased(MouseEvent e) //滑鼠放開事件
        {
                try{
                dataOut.writeInt(0); //送出停止命令
                dataOut.flush();
                } catch(IOException i) {
                        JOptionPane.showMessageDialog(null, "Communication disconnected",                                                 "Error", JOptionPane.ERROR_MESSAGE); //斷線錯誤警告
                        System.exit(1);
                }
        }//mouseReleased
       
        public void mouseClicked(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
}//MouseAction

class Sample14_2 extends JFrame
{
        int STOP = 0, FWD = 1, BACK = 2, LEFT = 3, RIGHT = 4; //宣告命令碼
        JButton btForward;
        JButton btBackward;
        JButton btLeft;
        JButton btRight;
        NXTConnector conn;
        DataOutputStream dataOut;
       
        public Sample14_2()
        {
                conn = new NXTConnector();
                if(!conn.connectTo("", "", NXTCommFactory.BLUETOOTH)) //連線到NXT
                {
                        JOptionPane.showMessageDialog(null, "Cannot connect to NXT",
                                                "Error", JOptionPane.ERROR_MESSAGE); //連線失敗訊息
                        System.exit(1);
                }//if
                dataOut = conn.getDataOut(); //取得輸出串流
               
                //設定視窗
                this.setTitle("COM"); //視窗標題為「COM」
                this.setSize(320, 240); //視窗大小為320×240
                this.setResizable(false); //固定視窗大小
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //視窗關閉時結束程式
                this.getContentPane().setLayout(null); //視窗的Layout Manager為null
                this.setLocationRelativeTo(null); //視窗在螢幕置中
               
                //前進按鈕
                btForward = new JButton("↑"); //宣告JButton
                btForward.setBounds(100, 0, 100, 100); //設定大小位置
                btForward.addMouseListener(new MouseAction(dataOut, FWD));//加入滑鼠事件

                this.getContentPane().add(btForward); //將按鈕加入視窗
               
                //後退按鈕
                btBackward = new JButton("↓");
                btBackward.setBounds(100, 100, 100, 100);
                btBackward.addMouseListener(new MouseAction(dataOut, BACK));

                this.getContentPane().add(btBackward);
               
                //左轉按鈕
                btLeft = new JButton("←");
                btLeft.setBounds(0, 100, 100, 100);
                btLeft.addMouseListener(new MouseAction(dataOut, LEFT));

                this.getContentPane().add(btLeft);
               
                //右轉按鈕
                btRight = new JButton("→");
                btRight.setBounds(200, 100, 100, 100);
                btRight.addMouseListener(new MouseAction(dataOut, RIGHT));

                this.getContentPane().add(btRight);
               
                this.setVisible(true); //顯示視窗
        }//建構子
       
        public static void main(String args[])
        {
                Sample14_2 com = new Sample14_2();
        }//main
}//Sample14_2

以上程式碼為1對1的nxt機器人操控,
目前要修改成能1對2同時操控兩台機器人 不知道要改哪些地方呢??
麻煩指教一下了謝謝

如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
发表于 2011-12-11 18:45:53 | 显示全部楼层
你應該是從書中的範例拿出來的吧?重點在58-ˊ65行:
58 建立名為conn的NXTConnector物件
59 用connectTo方法,經藍芽連線到不限定名稱、位置的NXT,connectTo的詳細用法請參閱書上的8-20頁,或是leJOS官網上的API可以找(0.85版)
60-64 若連線失敗則顯示錯誤訊息
65 取得DataOutputStream物件
這樣就連結上一個NXT,如果你要連結第二台NXT,再建立一個NXTConnector,照58-65依樣畫葫蘆即可,以下提供範例
  1. //連結第一台NXT
  2. conn1 = new NXTConnector();
  3. if(!conn1.connectTo("NXT1", "", NXTCommFactory.BLUETOOTH))
  4. {
  5.         JOptionPane.showMessageDialog(null, "Cannot connect to NXT1", "Error", JOptionPane.ERROR_MESSAGE);
  6.         System.exit(1);
  7. }
  8. dataOut1 = conn1.getDataOut();

  9. //連結第二台NXT
  10. conn2 = new NXTConnector();
  11. if(!conn2.connectTo("NXT2", "", NXTCommFactory.BLUETOOTH))
  12. {
  13.         JOptionPane.showMessageDialog(null, "Cannot connect to NXT1", "Error", JOptionPane.ERROR_MESSAGE);
  14.         System.exit(1);
  15. }
  16. dataOut2 = conn2.getDataOut();
复制代码
如此就可以連上2台NXT,別忘了修改一下視窗介面,祝你專題成功。
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2024-4-30 18:36 , Processed in 0.120653 second(s), 19 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表