Pages: [1]   Go Down
Author Topic: processing not reading serial data correctly??  (Read 1206 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

my arduino sends data over serial(usb) port.but there is a problem.for example,when i read it in arduino serial monitor,it is 244,however,when i read it in processing, it is 52
13
-1
i can't understand what is the problem,and how to solve it.

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
my arduino sends data over serial(usb) port.but there is a problem.
Sure is. You posted the code in such a tiny font that it is completely invisible.

Quote
for example,when i read it in arduino serial monitor,it is 244,however,when i read it in processing, it is 52
What is?

Quote
i can't understand what is the problem
Your understanding of how data is sent from one device to another via the serial port is incorrect.

Quote
and how to solve it.
Read up on how serial data is sent. Look at the source code for both sides.

Or post the Arduino and Processing code, and we'll help you.
Logged

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

the problem is in data type conversion.it converts 244 into 52,13,-1
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the problem is in data type conversion.it converts 244 into 52,13,-1
Well, then, you should perform the correct data type conversion. Or post some code.
Logged

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

arduino code:
Code:
// 0.1 by pmalmsten http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176098434
// 0.2 by farkinga
// 0.3 by farkinga - adds cool behaviors

#define IR_BIT_LENGTH 12    // number of bits sent by IR remote
#define BIT_1 1000          // Binary 1 threshold (Microseconds)
#define BIT_0 400           // Binary 0 threshold (Microseconds)
#define BIT_START 2000      // Start bit threshold (Microseconds)

#define IR_PIN 7            // Sensor pin 1 wired through a 220 ohm resistor
#define LED_PIN 9           // first LED output
#define POWER_PIN 11        // second LED output, corresponds to power button

#define DEBUG 0             // Serial connection must be started to debug

int runtime_debug = 0;      // flag to output raw IR pulse data
int output_key = 0;         // flag to print decoded key integers
int power_button = 0;       // flag to indicate if power LED is on
int power_level = 128;      // value (0-255) for power LED intensity

void setup() {
  pinMode(LED_PIN, OUTPUT); //This shows when we're ready to recieve
  pinMode(POWER_PIN, OUTPUT); //This is the "power on" indicator
  pinMode(IR_PIN, INPUT);
  digitalWrite(LED_PIN, LOW);
  Serial.begin(9600);
  Serial.println("ready to decode!!");
}

void loop() {
  int key = get_ir_key();
  Serial.println(key);
  delay(200);
 pinMode(8,OUTPUT);
  digitalWrite(8,LOW);
  pinMode(9,OUTPUT);
  digitalWrite(9,HIGH);  // short delay to cancel duplicate keypresses
}

/*
  wait for a keypress from the IR remote, and return the
  integer mapping of that key (e.g. power button on remote returns
  the integer 1429)
*/

int get_ir_key()
{
  int pulse[IR_BIT_LENGTH];
  int bits[IR_BIT_LENGTH]; 

  do {} //Wait for a start bit
  while(pulseIn(IR_PIN, LOW) < BIT_START);

  read_pulse(pulse, IR_BIT_LENGTH);
  pulse_to_bits(pulse, bits, IR_BIT_LENGTH);
  return bits_to_int(bits, IR_BIT_LENGTH);
}

/*
  respond to specific remote-control keys with different behaviors
*/


/*
  use pulseIn to receive IR pulses from the remote.
  Record the length of these pulses (in ms) in an array
*/

void read_pulse(int pulse[], int num_bits)
{
  for (int i = 0; i < num_bits; i++)
  {
    pulse[i] = pulseIn(IR_PIN, LOW);
  }
}

/*
  IR pulses encode binary "0" as a short pulse, and binary "1"
  as a long pulse.  Given an array containing pulse lengths,
  convert this to an array containing binary values
*/

void pulse_to_bits(int pulse[], int bits[], int num_bits)
{
  if (DEBUG || runtime_debug) { Serial.println("-----"); }
 
  for(int i = 0; i < num_bits ; i++)
  {
    if (DEBUG || runtime_debug) { Serial.println(pulse[i]); }
   
    if(pulse[i] > BIT_1) //is it a 1?
    {
      bits[i] = 1;
    } 
    else if(pulse[i] > BIT_0) //is it a 0?
    {
      bits[i] = 0;
    }
    else //data is invalid...
    {
      Serial.println("Error");
    }
  }
}

/*
  convert an array of binary values to a single base-10 integer
*/

int bits_to_int(int bits[], int num_bits)
{
  int result = 0;
  int seed = 1;
 
  //Convert bits to integer
  for(int i = 0 ; i < num_bits ; i++)
  {  
    if(bits[i] == 1)
    {
result += seed;
    }
   
    seed *= 2;
  }
 
  return result;
}


processing code:
Code:
import processing.serial.*;
Serial serial;
void setup(){
  serial = new Serial(this, "COM8", 9600);
}
void draw(){
 if ( serial.read() > 0) {
    println(serial.read());       
  }
}



actually,what i dont know is what conversion to perform.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if ( serial.read() > 0) {

This line is throwing away a byte. Perhaps you mean serial.available().
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial.println() takes your int, and converts it to a string. It sends the string to the serial port. It is sending '2', '4', '4', <cr>, <lf>. The Processing application is then reading the '2' (or 50 is the ASCII value). Since that is greater than 0, it then reads the '4' (52) and shows that. Then, it reads the '4', which is greater than 0, so it reads and shows the <cr> (13 in the ASCII table). Then, it reads the <lf> (10 in the ASCII table). Since that is greater then 0, it reads and shows the next value. Oops, there are not more values (so you get the -1).
Logged

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

so,what should i do?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so,what should i do?
Did you see dxw00d's question? Did you answer it? Isn't that enough of a clue?

Yea, yeah, I know more questions than answers, but sometimes that's necessary.
Logged

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

yeah,dxw00d is right.serial.available should be used.also,in my arduino code,i should have used serial.write. i did this and problem solved!!thanks!!!!
Logged

Pages: [1]   Go Up
Jump to: