Pages: [1]   Go Down
Author Topic: printing BYTE (8bit) to serial  (Read 1343 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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.
Logged

London, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 20
If you can't open it, you don't own it.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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
Logged

London, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 20
If you can't open it, you don't own it.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How about "unsigned int" ? (as a test only).
« Last Edit: January 11, 2007, 07:55:55 am by llemarie » Logged

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

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

(testing  :-?)

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

I get 63  :'(  smiley-wink
Logged

London, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 20
If you can't open it, you don't own it.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 11
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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));
  }
}

Logged

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

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:
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.
Logged

Pages: [1]   Go Up
Jump to: