Pages: [1]   Go Down
Author Topic: Comunication PC-BLUETOOTH  (Read 496 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys, I have a module bluetooth with connect with my PC and for that I use the terminal PuttY and everything is fine. My question is to known if is possible and how I can create a program in C, C#, java,.. that receive the data from the bluetooth and I can use them in one expression or make a register of data without use Putty.
Thanks!
Logged

Wahiawa, Hawaii
Offline Offline
God Member
*****
Karma: 27
Posts: 582
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can try using Processing. http://www.processing.org/

I uses a similar IDE but the code runs on your PC.

A simple serial read example:
Code:
/**
 * Simple Read
 *
 * Read data from the serial port and change the color of a rectangle
 * when a switch connected to a Wiring or Arduino board is pressed and released.
 * This example works with the Wiring / Arduino program that follows below.
 */
import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
    println(val);
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  }
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}

Sample code for the Arduino to send to Processing:
Code:
// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
    Serial.write(1);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

Logged

0
Offline Offline
Sr. Member
****
Karma: 6
Posts: 383
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My question is to known if is possible and how I can create a program in C, C#, java,.. that receive the data from the bluetooth

If puttY is working than any language should work that supports a serial library.  Just open the serial port comNN as in PuttY.  On your PC Bluetooth just looks like a serial port.
Which language?  C, C++, C#, Java, Python, Basic, ....

Joe
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If puttY is working than any language should work that supports a serial library.  Just open the serial port comNN as in PuttY.  On your PC Bluetooth just looks like a serial port.
Which language?  C, C++, C#, Java, Python, Basic, ....

Joe

Then what you say is if I create one program in C for example only I´ve to do is add a library in my program and run at the same time the programe and the PuttY to read from bluetooth? Any example code of how can I do that?
Normally I use C but I can make in C++, C# or Java too

You can try using Processing. http://www.processing.org/

I uses a similar IDE but the code runs on your PC.


Im will try that too smiley-wink
« Last Edit: June 07, 2013, 06:02:46 am by pridewrath » Logged

0
Offline Offline
Sr. Member
****
Karma: 6
Posts: 383
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Then what you say is if I create one program in C for example only I´ve to do is add a library in my program and run at the same time the programe and the PuttY to read from bluetooth? Any example code of how can I do that?
Normally I use C but I can make in C++, C# or Java too

C will work great.  All you need to do is open the same serial port that putty was using.
Note that you do NOT run putty at the same time.  The first program to open the port also locks it.  As far a C is concerned the Bluetooth port looks like a sequential file.  Remember that putty is written in C.

A quick search gives many examples.  Here are 2.

http://forums.codeguru.com/showthread.php?500130-Open-Serial-Port-using-C
http://www.codeproject.com/Articles/2682/Serial-Communication-in-Windows


Joe
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for all the help, I solved my problem using processing, the program is really awesome!
Thanks guys!
Logged

Pages: [1]   Go Up
Jump to: