Go Down

Topic: printing BYTE (8bit) to serial (Read 1 time) previous topic - next topic

lenox

When I use print.serial(data, BYTE), data has to be smaller than 127 (7 bits)
Can I send a 8bits data using serial port?

Thanks in advance

CosineKitty

As far as I can tell (without my hardware right now, but looking at runtime code), I see no reason why Serial.print(x,BYTE) should limit you to 7 bits... 8 bits should work just fine.

lenox

Quote
As far as I can tell (without my hardware right now, but looking at runtime code), I see no reason why Serial.print(x,BYTE) should limit you to 7 bits... 8 bits should work just fine.


I tried to use Serial.print(x, BYTE) with x>127 and I got 63 in the PC port (?)  :'(
By code, it's possible to convert a 8bit value in two 7bit values but It would easier send a 8bit value.

byte x=150, x0, x1;
x0 = x & 127;
x1 = (x >>7) & 1;
Serial.print(x0, BYTE);
Serial.print(x1, BYTE);

I would like to send a byte without making this conversion.

Lionel Lemarie

Is byte signed ? Can you try "unsigned byte x" ?

lenox

Quote
Is byte signed ? Can you try "unsigned byte x" ?

ops it doesn't work  :-/. I can't define a unsigned byte but:

unsigned char x = 200;
Serial.print(x, BYTE);
prints 63

Lionel Lemarie

#5
Jan 11, 2007, 01:55 pm Last Edit: Jan 11, 2007, 01:55 pm by llemarie Reason: 1
How about "unsigned int" ? (as a test only).

lenox

Quote
How about "unsigned int" ? (as a test only).


(testing  :-?)

   unsigned int v = 200;
   Serial.print(v, BYTE);

I get 63  :'(  ;)

Lionel Lemarie

Ok, next suspect : the PC software.
What application do you use to read from the serial port ?

mellis

Sending 8-bit data works for me, so I don't think it's broken in general.  What's your Arduino code look like?  How are you reading it on the PC?

Here's the Processing code I'm using to dump the data I get from the serial port:

Code: [Select]

import processing.serial.*;

Serial port;

void setup()
{
 port = new Serial(this, Serial.list()[0], 9600);
}

void draw()
{
 while (port.available() > 0) {
   int val = port.read();
   println(((char) val) + " " + str(val));
 }
}


lenox

I have found the problem. In VB.net I was using SerialPort.ReadExisting(). It returns a string converting any value greater than 127 to 63.
       
Code: [Select]
Dim encoding As New System.Text.ASCIIEncoding, data() as Byte
       data = encoding.GetBytes(SerialPort.ReadExisting())


Using SerialPort.ReadBuffer with a byte array is a possible solution.

Thanks.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy