9 years, 11 months ago.

Sending Decimals over Serial

I am trying to send decimals to the Processing IDE over serial

is there anyway of expanding on this idea. it does not comiple at the moment. thanks for having a look.

include the mbed library with this snippet

#include "mbed.h"

Serial pc(USBTX,USBRX);
int s= 200;
int main()
{ 
    while(1)
    {
        pc.putc(s,DEC);
        pc.printf("%d"DEC,s);
        }
 }   
 

1 Answer

9 years, 11 months ago.

I'm not quite sure what you mean by "send decimals", but this small variation of what you have will send the ASCII text representation of s to the serial port.

pc.printf("%d", s);

On the other hand, this will send the binary value:

pc.putc(s);

So, for the value 200, it will likely not be legible in a terminal program. If s = 0x41, you would see 'A' on a connected terminal.

Thanks for respoding. i am trying to send adc reading, from a custom code, raning from 0-1023 in a way that Processing can see them as int not their ASCII value. i've tried both printf, which receieves the ASCII but the application I am sending the data requires it to be in decimal or hex. Tried putc(s) the is no read on that all.

posted by Donald K 26 Apr 2014

this is the processing apllication that is receiving the data

include the mbed library with this snippet

/**
 * 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
float xVal, Yval;      // Data received from the serial port
int linefeed = 10;
float sensorValue;

void setup() 
{
  size(500, 500);
  // 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()[0];
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil(linefeed);
}

void draw()
{/*
  if ( myPort.available() > 0) {  // If data is available,
    xVal = myPort.read(); 
  }*/        // read it and store it in val
  fill(255);
  rect(0, 0, 500, 500);
  //stroke(255, 0, 0);
  line(0,250, 500, 250);
  //stroke(255, 0, 0);
  line(250, 0, 250, 500);
  ellipseMode(CENTER);
  fill(150, 0, 0);
  ellipse(250,sensorValue-10, 15, 15);
  
}


void serialEvent(Serial myPort) 
{
            // read the serial buffer:
            String myString = myPort.readStringUntil(linefeed);
            // if you got any bytes other than the linefeed:
            if (myString != null) 
              {
                // trim off the carriage return and convert the string to an integer:
                println(myString);
                sensorValue = int(trim(myString))*500;
                // print it:
                println(sensorValue);
              }
}



posted by Donald K 26 Apr 2014