bluetoothing an arduino – data to PC

this little sketch along with the vb .Net script below builds on the previous examples, but this time having completed the sketch the arduino sends a message: “[Arduino;Bluesmirf;Baltaz]”. This is then read by the windows .Net application using the SerialPort.DataReceived event in .Net.

In short, it appears to my untrained eyes that the windows application is waiting for any data received via the serial port. So here when it gets the left bracket “[“, it is programmed to collect each of the characters that follow until it gets the right bracket “]” – which tells the .Net programme that the message stream has finished. This will allow the arduino to communicate with the windows app and tell that programme when the arduino has completed the commands.

So, the wiring, is as in the last post.

And the vb .net code is as follows:

First, the following public variable is declared (the WithEvents description means that the windows app is aware that there are events – like datareceived – which are associated with the serial port.

Public WithEvents BGCserialPort As IO.Ports.SerialPort

Then, the following is the code for the event itself. This reads each character of data at a time and concatenates it to form a single string.

    ''''''''''''''''''''''''''''''''''''''''''''''''''    'SerialDataReceived Event                        '    'This event runs when the serial port receives   '     'data.                                            '    ''''''''''''''''''''''''''''''''''''''''''''''''''

    Private Sub Receiver(ByVal sender As Object, _           ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _           Handles BGCserialPort.DataReceived

        'Declare the variables for the code        Dim RxChar As Char        Dim RxMessage As String

        'Empty the variables        RxChar = ""        RxMessage = ""

        'While there is data to read...        While BGCserialPort.BytesToRead > 0

            'Store each character received and concatenate             'to form a single message            Do                RxChar = ChrW(BGCserialPort.ReadByte)                RxMessage = RxMessage & RxChar            Loop Until RxChar = "]"        End While

        'Show the message        MsgBox(RxMessage)

    End Sub

The arduino sketch is as follows (v similar to the last two but now has square brackets around the incoming data):

/* AFSoft Serial Mode - Multi-Bits * --------------------------------------- * * Turns on and off an LED on Pin 13 according to the number * transmitted to the arduino via the bluesmirf.  Can now * receive a number larger than 1-bit. * Sending [5;10;15] will: flash Pin 11 x 5, Pin 12 x 10, Pin 13 x 15. *  * Then this sends out a single message "[Arduino;Bluetooth;Baltaz]" *  * Acknowledgements  * The difficult serial parsing bit was came from Nition * see forum post at: * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1203538464/2#2 * * The AFSoftwareSerial library came from ladyada.  It adds the * serial.available functionality. * download it here: http://www.ladyada.net/make/eshield/download.html * * created 25 June 2008 * by Turquand */

// include the AFSoftSerial library#include <AFSoftSerial.h>

// define the pins on the arduino: 7 to rx, 6 to tx, 13 to flash#define rxPin 7#define txPin 6#define flashxPin 11#define flashyPin 12#define flashrPin 13

//define the variables for parsing serial data #define tagLength 6    // each tag ID contains 6 bytes#define semicolon 0x3b   // ";" semicolon#define dataRate 9600    // 9600kbps#define leftbracket 0x5b // "[" left squarebracket#define rightbracket 0x5d // "]" right squarebracket

int tagIndex = 0;             // counter for number of bytes readint allComplete = false;        // whether all tags have been readchar xVal[tagLength], yVal[tagLength], rVal[tagLength];int xComplete = false;int yComplete = false;int rComplete = false;

// set up a new serial connection using pins 7 and 6AFSoftSerial mySerial =  AFSoftSerial(rxPin, txPin);

// the setup part of the scriptvoid setup()  {

  // define pin modes for tx, rx:  pinMode(rxPin, INPUT);  pinMode(txPin, OUTPUT);  pinMode(flashxPin, OUTPUT);  pinMode(flashyPin, OUTPUT);  pinMode(flashrPin, OUTPUT);

  // set the data rate for the serial port  mySerial.begin(dataRate);}

// the main part of the scriptvoid loop() {

  // read in and parse serial data:  if (mySerial.available() > 0) {

  //just put in a loop here    delay(1500); // wait for all data to be in. 1500 works fine.    readData(); // read the data that's been received

  // flash the Pin 11 [xVal] number of times    for (int a=0; a<atoi(xVal);a++){    digitalWrite(flashxPin,HIGH);    delay(100);    digitalWrite(flashxPin,LOW);    delay(100);    }

  // flash the Pin 12 [yVal] number of times    for (int a=0; a<atoi(yVal);a++){    digitalWrite(flashyPin,HIGH);    delay(100);    digitalWrite(flashyPin,LOW);    delay(100);    }

  // flash the Pin 13 [rVal] number of times    for (int a=0; a<atoi(rVal);a++){    digitalWrite(flashrPin,HIGH);    delay(100);    digitalWrite(flashrPin,LOW);    delay(100);    }

  // run the DataComplete procedure     DataComplete();

//End: If Serial.Available > 0  }//End: loop}

void readData() {

// Reset all the variables    xComplete = false;    yComplete = false;    rComplete = false;

    for (int i=0;i<5;i++){    digitalWrite(5, HIGH);    xVal[i] = 0x00;    yVal[i] = 0x00;    rVal[i] = 0x00;    }    

// Read the serial data  char thisChar = mySerial.read();

 if (thisChar == leftbracket){    // read all x  while (xComplete == false && mySerial.available() > 0){        char thisChar = mySerial.read();   //   Serial.println("x-loop");    if (thisChar == semicolon){        xComplete = true;      break;    }    else {        xVal[tagIndex] = thisChar;      tagIndex++;    }    }  tagIndex = 0;

 // read all y  while (yComplete == false && mySerial.available() > 0){    char thisChar = mySerial.read();   //   Serial.println("y-loop");

    if (thisChar == semicolon){      yComplete = true;      break;      }    else {      yVal[tagIndex] = thisChar;      tagIndex++;    }    }  tagIndex = 0;

 // read all
r  while (rComplete == false && mySerial.available() > 0){    char thisChar = mySerial.read();   //   Serial.println("r-loop");

    if (thisChar == rightbracket){      rComplete = true;      break;    }    else {      rVal[tagIndex] = thisChar;      tagIndex++;    }    }  tagIndex = 0;

  if (xComplete == true && yComplete == true && rComplete == true){    // Can put in debugging here  } }}

void DataComplete(){  // after all the data has been received and the LEDs flashed...  mySerial.print("[Arduino;Bluesmirf;Baltaz]");  }

T. EXTRA EXTRA

FORMER POST:
this little sketch along with the vb .Net script below builds on the previous examples, but this time having completed the sketch the arduino sends a message: “[Arduino;Bluesmirf;Baltaz]”. This is then read by the windows .Net application using the SerialPort.DataReceived event in .Net.

In short, it appears to my untrained eyes that the windows application is waiting for any data received via the serial port. So here when it gets the left bracket “[“, it is programmed to collect each of the characters that follow until it gets the right bracket “]” – which tells the .Net programme that the message stream has finished. This will allow the arduino to communicate with the windows app and tell that programme when the arduino has completed the commands.

So, the wiring, is as in the last post.

And the vb .net code is as follows:

First, the following public variable is declared (the WithEvents description means that the windows app is aware that there are events – like datareceived – which are associated with the serial port.

<code>Public WithEvents BGCserialPort As IO.Ports.SerialPort

</code>

Then, the following is the code for the event itself. This reads each character of data at a time and concatenates it to form a single string.

<code>    ''''''''''''''''''''''''''''''''''''''''''''''''''    'SerialDataReceived Event                        '    'This event runs when the serial port receives   '     'data.                                            '    ''''''''''''''''''''''''''''''''''''''''''''''''''

    Private Sub Receiver(ByVal sender As Object, _           ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _           Handles BGCserialPort.DataReceived

        'Declare the variables for the code        Dim RxChar As Char        Dim RxMessage As String

        'Empty the variables        RxChar = ""        RxMessage = ""

        'While there is data to read...        While BGCserialPort.BytesToRead > 0

            'Store each character received and concatenate             'to form a single message            Do                RxChar = ChrW(BGCserialPort.ReadByte)                RxMessage = RxMessage & RxChar            Loop Until RxChar = "]"        End While

        'Show the message        MsgBox(RxMessage)

    End Sub

</code>

The arduino sketch is as follows (v similar to the last two but now has square brackets around the incoming data):

<code>/* AFSoft Serial Mode - Multi-Bits * --------------------------------------- * * Turns on and off an LED on Pin 13 according to the number * transmitted to the arduino via the bluesmirf.  Can now * receive a number larger than 1-bit. * Sending [5;10;15] will: flash Pin 11 x 5, Pin 12 x 10, Pin 13 x 15. *  * Then this sends out a single message "[Arduino;Bluetooth;Baltaz]" *  * Acknowledgements  * The difficult serial parsing bit was came from Nition * see forum post at: * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1203538464/2#2 * * The AFSoftwareSerial library came from ladyada.  It adds the * serial.available functionality. * download it here: http://www.ladyada.net/make/eshield/download.html * * created 25 June 2008 * by Turquand */

// include the AFSoftSerial library#include <AFSoftSerial.h>

// define the pins on the arduino: 7 to rx, 6 to tx, 13 to flash
#define rxPin 7
#define txPin 6
#define flashxPin 11
#define flashyPin 12
#define flashrPin 13

//define the variables for parsing serial data 
#define tagLength 6  // each tag ID contains 6 bytes
#define semicolon 0x3b   // ";" semicolon
#define dataRate 9600    // 9600kbps
#define leftbracket 0x5b // "[" left squarebracket
#define rightbracket 0x5d // "]" right squarebracket

int tagIndex = 0;             // counter for number of bytes read
int allComplete = false;        // whether all tags have been readchar xVal[tagLength], yVal[tagLength], rVal[tagLength];
int xComplete = false;
int yComplete = false;
int rComplete = false;

// set up a new serial connection using pins 7 and 6
AFSoftSerial mySerial =  AFSoftSerial(rxPin, txPin);

// the setup part of the script
void setup()  {

  // define pin modes for tx, rx:  
pinMode(rxPin, INPUT);  
pinMode(txPin, OUTPUT);  
pinMode(flashxPin, OUTPUT);  
pinMode(flashyPin, OUTPUT);  
pinMode(flashrPin, OUTPUT);

// set the data rate for the serial port  
mySerial.begin(dataRate);}

// the main part of the script
void loop() {

  // read in and parse serial data:
  if (mySerial.available() > 0) {

  //just put in a loop here    
delay(1500); // wait for all data to be in. 1500 works fine.    
readData(); // read the data that's been received

  // flash the Pin 11 [xVal] number of times    
for (int a=0; a<atoi(xVal);a++){    
digitalWrite(flashxPin,HIGH);    
delay(100);    
digitalWrite(flashxPin,LOW);    
delay(100);    
}

  // flash the Pin 12 [yVal] number of times    
for (int a=0; a<atoi(yVal);a++){    
digitalWrite(flashyPin,HIGH);    
delay(100);    
digitalWrite(flashyPin,LOW);    
delay(100);    
}

  // flash the Pin 13 [rVal] number of times    
for (int a=0; a<atoi(rVal);a++){    
digitalWrite(flashrPin,HIGH);    
delay(100);    
digitalWrite(flashrPin,LOW);    
delay(100);    
}

  // run the DataComplete procedure     
DataComplete();

}//End: If Serial.Available > 0  
}//End: loop

void readData() {

// Reset all the variables    
xComplete = false;    
yComplete = false;    
rComplete = false;

    for (int i=0;i<5;i++){    
digitalWrite(5, HIGH);    
xVal[i] = 0x00;    
yVal[i] = 0x00;    
rVal[i] = 0x00;    
}    

// Read the serial data  char 
thisChar = mySerial.read();

 if (thisChar == leftbracket){    // read all x  
while (xComplete == false && mySerial.available() > 0){        
char thisChar = mySerial.read();   
//   Serial.println("x-loop");    
if (thisChar == semicolon){        
xComplete = true;      
break;    
}    
else {        
xVal[tagIndex] = thisChar;      
tagIndex++;    
}    
}  
tagIndex = 0;

 // read all y  
while (yComplete == false && mySerial.available() > 0){    
char thisChar = mySerial.read();   
//   Serial.println("y-loop");

    if (thisChar == semicolon){      
yComplete = true;      
break;      
}    
else 
{      
yVal[tagIndex] = thisChar;      
tagIndex++;    
}    
}  
tagIndex = 0;

 // read all r  
while (rComplete == false && mySerial.available() > 0){    
char thisChar = mySerial.read();   
//   Serial.println("r-loop");

    if (thisChar == rightbracket){      
rComplete = true;      
break;    
}    
else 
{      
rVal[tagIndex] = thisChar;      
tagIndex++;    
}    
}  
tagIndex = 0;

  if (xComplete == true && yComplete == true && rComplete == true){    // Can put in debugging here  } }}

void DataComplete(){  // after all the data has been received and the LEDs flashed...  mySerial.print("[Arduino;Bluesmirf;Baltaz]");  }

</code>

T.

post a comment...

you must be logged in to post a comment.