Bluetooth通信(Processing:メッセージ編)

<PC(Win・Mac)とNXTとでメッセージのやりとり>

動画:https://www.youtube.com/watch?v=hrbXXOIZroA

PC側

Receiveボタンで、テキストメッセージ受け取り表示

Send:TEST0ボタンで、テキストメッセージ"TEST0"送信

Send:TEST1ボタンで、テキストメッセージ”TEST1”送信

NXT側

テキストメッセージを受け取ると表示する

前と異なるテキストメッセージを受け取ると

Hello!またはAre You Happy? を送信

(NXT側でもプログラムを走らせておく必要があります)

(PC側のProcessingプログラム)

import processing.serial.*;
Serial port;

int Count = 0;
String NXTPort;
 
byte[] in_msg = new byte[100];
byte[] out_msg = new byte[100];
byte[] tmp_msg = new byte[100];

String writeString = "nothing";
 
int gpa;

int firstTime = 1;



void setup()
{  

  size(420,300);
  println(Serial.list());  //シリアルポートの一覧をコンソール表示

  NXTPort = Serial.list()[0];   //[0]の数値は接続ポートにより1, 2, 3などに変更する必要あり
  
  port=new Serial(this,NXTPort,9600);

}
  

void draw()
{

  String disp;
  background(100);

  fill(255);
  text(NXTPort, 100, 50);
  
  rect(60,100,100,100);
  rect(180,100,100,100);
  rect(300,100,100,100);
  
  disp = new String(in_msg);
  text("RECEIVE: " + disp, 50, 240);
  text("SEND: " + writeString, 50, 260);
  
  text(gpa, 300, 50);
  
  fill(0);
  text("Receive", 80, 150);
  text("Send:TEST0", 200, 150);
  text("Send:TEST1", 320, 150);
  
}


void mousePressed(){
 
  if (mouseX < 150){
  read();
  }
  
  if (mouseX > 150 && mouseX < 270) {
  writeString = "TEST0";
  write();
  }
  
  if (mouseX > 270) {
  writeString = "TEST1";
  write();
  }
}


void read()
{
  byte[] msg = new byte[7];
  int pa;
  
  pa = port.available();
  
  msg[0]=0x05; //Length of LSB
  msg[1]=0x00; //Length of MSB
  msg[2]=0x00; //Direct Command with Response
  msg[3]=0x13; //Command
  msg[4]=0x0F; //Remote Mail Box Number
  msg[5]=0x05; //NXT Mail Box Number
  msg[6]=0x01; //Remove
  
  port.write(msg);
  
  gpa = pa;
 
 if (pa < 66 && pa >= 11 && firstTime == 1) {
   firstTime = 0;
   
   for (Count = 0;  Count < 6; Count++ ) {

   tmp_msg[Count] = (byte)port.read();
      
   }
   pa -= 6;
 }
  
  if (pa < 66 && pa >= 11) {
     for (Count = 0;  Count < 11; Count++ ) {

   tmp_msg[Count] = (byte)port.read();
      
   }
   pa -= 11;
  }
  
  if (pa >= 78) {
     for (Count = 0;  Count < 66+11; Count++ ) {

   tmp_msg[Count] = (byte)port.read();
      
   }
   
   pa -= (66+11);
  }
  
  if (pa >=66 && pa < 78) {
    for (Count = 0;  Count < pa; Count++ ) {

   tmp_msg[Count] = (byte)port.read();
      
   }
   for (Count = 0;  Count < 100; Count++ ) {

   in_msg[Count] = 0;
      
  }
    return;
  }
  
   
  for (Count = 0;  Count < pa; Count++ ) {

   in_msg[Count] = (byte)port.read();
      
  }
  
   for (Count = pa;  Count < 100; Count++ ) {

   in_msg[Count] = 0;
      
  }
}

void write()
{
  byte[] msg = new byte[36];
  
  msg[0]=34; //Length of LSB
  msg[1]=0x00; //Length of MSB
  msg[2]=0x00; //Direct Command with Response
  msg[3]=0x09; //Command
  msg[4]=0x01; //NXT Mail Box Number
  msg[5]=12; //Message Size

  for( int i = 0; i < writeString.length() ; i++) {
    
  msg[6+i] = (byte)writeString.charAt(i);
  
  
  }
  
  port.write(msg);
 
  for (Count = 0;  Count < 66; Count++ ) {

   tmp_msg[Count] = (byte)port.read();
      
  }
}


(NXT側のプログラム)

#define INBX 1
#define OTBX 5

sub LNcheck(int ln) {
   if ( BluetoothStatus(ln)!=NO_ERR )
   {  TextOut(0, LCD_LINE1, "Line Error");
      Wait(1000); Stop(true);
   } else {
    TextOut(0, LCD_LINE1, "Line OK");
      Wait(1000);
   }
}

task main() {

   string stm, stm2, sts;
   LNcheck(0);
   
   sts = "Hello!";
             SendRemoteString(0, OTBX, sts);
             TextOut(0, LCD_LINE3, "                      ");
             TextOut(0, LCD_LINE3, sts);

   while (true) {

         ReceiveRemoteString(INBX, true, stm);
         if (stm != "") {
            TextOut(0, LCD_LINE2, "                      ");
            TextOut(0, LCD_LINE2, stm);
            Wait(1000);
         }


          if (stm2 != stm && stm != "") {
             if (sts == "Hello!") sts = "Are you Happy?";
             else sts = "Hello!";
             SendRemoteString(0, OTBX, sts);
             TextOut(0, LCD_LINE3, "                      ");
             TextOut(0, LCD_LINE3, sts);
             stm2 = stm;
          }

          Wait(1000);
   }
}


Last modified: 2014-09-01 Attached files total: 33MB