send integer to serial port

I am trying to send an integer value to an Arduino via the serial port. The Serial.write function takes only characters. Do I convert to 2 bytes first then send each byte as a character? if so how do I converts? other suggestions?

On the arduino side, I think I can get the integer back by: Serial.parseint(); but have not yet tried.

Help please!

Use this :

Serial1.Write(str(your_integer_value)) + chr(13) +chr(10)

chr(13) +chr(10) - this is optional if you need CR and LF

Thanks for your suggestion.

I can send a string of characters, but it’s difficult to turn back into an integer on the Arduino side. I guess what I want is a way to convert an int16 or int32 into its individual bytes, then send those bytes as ChrB(), which I think will e easier to convert back to numbers on the other end…

Thoughts?

Doesn’t Str() handle multibyte values? Or use a MemoryBlock.

stuff everything into a memory block ( which you can do as integers etc) then write the memoryblock

dim myMemoryBlock as new MemoryBlock( 4 ) /// integers are 32 bits - 4 bytes
myMemoryblock.Integer(0) = value
Serial1.Write( myMemoryBlock )

there’s several ways you can set up the memoryblock so its easy to create and put varied data into it
my personal favourite is to use a binary stream back by a memoryblock then write to the stream and then send the memory block

Hi … I do not know how to Aurdoino development environment, in Bascom are simply converted received string back to integer with the val(string) function. Without all the problems.

This thread shows some useful info on sending bytes over serial to an MCU.

How to decode 24-bit values into Chr to send over serial?

[quote=17206:@matt Herron]I am trying to send an integer value to an Arduino via the serial port. The Serial.write function takes only characters. Do I convert to 2 bytes first then send each byte as a character? if so how do I converts? other suggestions?

On the arduino side, I think I can get the integer back by: Serial.parseint(); but have not yet tried.

Help please![/quote]

You can use Serial1.Print() function to change from hex, binrary or decimal values over serial on the arduino side.
Arduino coverts using the int(value of any datatype) or using other functions. It’s very well documented on the arduiono reference.

I had the same question yesterday and found a link to this:

println()

Description

Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or ‘\r’) and a newline character (ASCII 10, or ’
'). This command takes the same forms as Serial.print().

Syntax

Serial.println(val)
Serial.println(val, format)

Parameters

val: the value to print - any data type

format: specifies the number base (for integral data types) or number of decimal places (for floating point types)

Returns

size_t (long): println() returns the number of bytes written, though reading that number is optional

Example:

/*
Analog input

reads an analog input on analog in 0, prints the value out.

created 24 March 2006
by Tom Igoe
*/

int analogValue = 0; // variable to hold the analog value

void setup() {
// open the serial port at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog input on pin 0:
analogValue = analogRead(0);

// print it out in many formats:
Serial.println(analogValue); // print as an ASCII-encoded decimal
Serial.println(analogValue, DEC); // print as an ASCII-encoded decimal
Serial.println(analogValue, HEX); // print as an ASCII-encoded hexadecimal
Serial.println(analogValue, OCT); // print as an ASCII-encoded octal
Serial.println(analogValue, BIN); // print as an ASCII-encoded binary

// delay 10 milliseconds before the next reading:
delay(10);
}
[Get Code]

I couldn’t believe it was that easy so I tried it.
I created and int on one Arduino, sent it to the other over I2C, then printed it to the serial monitor on the receiving
UNO.
Note that it says (ANY DATA TYPE).

If you want to print a floating point number I found this:
void setup()

{
Serial.begin(9600);
Serial.println(PI, 4); // should print 3.1416 (4 means four decimals)
float x = 2.12345;
Serial.println(x);
}

void loop() {}

Haven’t test that yet but I will tonight.
Hope that helps.