找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 3469|回复: 1

NXT RoboTC蓝牙如何接受指令

[复制链接]
发表于 2015-9-26 23:48:24 | 显示全部楼层 |阅读模式
如题,NXT主机我用的robotc,我在PC段用lejos写了一段代码通过蓝牙发送指令出去给nxt,但是主机上面的robotc代码无论如何也获取不到指令信息,有哪位道友能知道小弟一二啊,感激不尽啊!这里很多代码都是nxt软件sample里面的代码

  1. 主机代码:

  2. task readDataMsg();

  3. task main() {
  4.        
  5.         StartTask(readDataMsg);
  6.         /*
  7.         string display;
  8.         while(true) {
  9.                
  10.           // Skip to the last message received
  11.           while (bQueuedMsgAvailable()) {
  12.                   StringFormat(display, "Value: %d", message);
  13.                   nxtDisplayCenteredTextLine(2, display);
  14.             ClearMessage();   // We’re ready to process the next messag
  15.                 }
  16.   
  17.         }*/
  18. }


  19. const int kMaxSizeOfMessage = 5;
  20. const TMailboxIDs kQueueID = mailbox1;

  21. long nRcvCnt = 0;
  22. long nReadCnt = 0;

  23. task readDataMsg()
  24. {

  25.         //const bool bWaitForReply = false;
  26.   TFileIOResult nBTCmdErrorStatus;
  27.         int nSizeOfMessage;
  28.         ubyte nRcvBuffer[kMaxSizeOfMessage * 5];
  29.         string sMsg;
  30.         string sTemp;

  31.         while (true)
  32.         {
  33.                 sMsg  = "Read Data ";
  34.                 sTemp = nRcvCnt;
  35.                 sMsg = sMsg + sTemp;
  36.                 nxtDisplayCenteredTextLine(6, sMsg);
  37.                 nSizeOfMessage = cCmdMessageGetSize(kQueueID);
  38.                 if (nSizeOfMessage <= 0)
  39.                 {
  40.                         wait1Msec(1);
  41.                 }
  42.                 if (nSizeOfMessage > kMaxSizeOfMessage)
  43.                   nSizeOfMessage = kMaxSizeOfMessage;
  44.           nBTCmdErrorStatus = cCmdMessageRead(nRcvBuffer, nSizeOfMessage, kQueueID);
  45.           if (nBTCmdErrorStatus == ioRsltSuccess)
  46.           {
  47.                         // Keep a running count of the number of messages successfully read
  48.                   ++nReadCnt;
  49.                         sMsg  = "Mag Read ";
  50.                         sTemp = nReadCnt;
  51.                         sMsg = sMsg + sTemp;
  52.                         nxtDisplayCenteredTextLine(7, sMsg);
  53.                 }
  54.         }
  55. }
复制代码
  1. PC段lejos代码
  2. package nxt;

  3. import java.io.DataOutputStream;
  4. import java.io.OutputStream;

  5. import lejos.pc.comm.NXTComm;
  6. import lejos.pc.comm.NXTCommFactory;
  7. import lejos.pc.comm.NXTInfo;

  8. /**
  9. * @author yang
  10. *
  11. */
  12. public class PCBluetooth {

  13.         public static void main(String[] args) {
  14.                 try {
  15.                         NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
  16.                         //NXTInfo[] nxtInfos = nxtComm.search("NXT");
  17.                         NXTInfo[] nxtInfos = nxtComm.search("Raymond");
  18.                        
  19.                         NXTInfo nxtInfo = nxtInfos[0];
  20.                         System.out.println("deviceAddress: " + nxtInfo.deviceAddress);
  21.                        
  22.                         nxtComm.open(nxtInfo);
  23.                         OutputStream os = nxtComm.getOutputStream();
  24.                         DataOutputStream dos = new DataOutputStream(os);
  25.                        
  26.                         try {
  27.                                 for (int i=1; i<10; i++) {
  28.                                        
  29.                                         System.out.println("Sending " + (i));
  30.                                         dos.writeInt((i*10));
  31.                                         dos.flush();
  32.                                        
  33.                                         Thread.sleep(1000 * 3);
  34.                                 }
  35.                         } catch (InterruptedException e) {
  36.                                 e.printStackTrace();
  37.                         }
  38.                        
  39.                 } catch (Exception e) {
  40.                         e.printStackTrace();
  41.                 }
  42.         }
  43. }
复制代码



如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
 楼主| 发表于 2015-9-26 23:49:06 | 显示全部楼层
麻烦大家帮忙看一下啊,感激不尽感激不尽!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 07:54 , Processed in 1.306129 second(s), 22 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

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