Serial.Read() Variable

Hi, evrybody! I have been working with Arduino for a little while, now, and I have a project that requires information to go from the computer to the microcontroller. As an excersize, I tried just dropping the return value for Serial.Read() into the PORTD register, but that doesn't work. After looking a bit closer at the examples, it seems that the variable that holds the return value is initialized as an 'int', but handled like a 'char'. What's going on?

Thanks

Did you read the documentation? Why not?

I've read stuff. Specific link, please?

I've read stuff. Specific link, please?

http://arduino.cc/en/Serial/Read

Not very surprising to get this link, isn't it?

Korman

What's going on?

Without your code, you might try a psychic forum.

I'm sorry, let me start again.

I have a Perl script sending integers to the Arduino at half-second intervals:

use Device::SerialPort;

my $port = Device::SerialPort->new("/dev/ttyUSB0");
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);

while (1) {
@array = ( 4, 8, 16, 32, 64, 128 );
      for $n (0..$#array) {
            $port->write("$array[$n]");
            print "Sent: $array[$n] \n";
            select(undef, undef, undef, .5);
      }
}

Then I have the Arduino taking the serial input an assigning it to the PORTD register (with RX and TX masked, of course):

void setup()
{
  DDRD = DDRD | B11111100;
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available() > 0) {
          PORTD = Serial.read();
          }
}

...but this isn't working.

I'm still new with Arduino, and very new at serial communication. I was trying to get a clue, and several examples seem typical of the "Physical Pixel" example that comes with Arduino:

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    } 
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}

The variable that receives the return value of the Serial.read() function is initialized as an int...

int incomingByte;      // a variable to read incoming serial data into

...and seems to be handled like a char...

    if (incomingByte == 'H') {
    if (incomingByte == 'L') {

(I used the above Perl script to send a capital 'H' or 'L' to the Arduino, and it works fine.) Anyway, there appears to be a type mismatch in the code.

I looked at the documentation and a dozen other pages to try to get an answer before coming here, but I'm snagged on this point.

What is the paradigm with the Arduino serial?

The Serial.read function returns a byte from the serial buffer. All the values in the range of a byte are valid. So, in order to be able to return an invalid value (to indicate that an attempt was made to read a value when none was available to be read, the function needs to return a larger value - one that can hold all possible legal values and one or more non-legal values. So, Serial.read() returns -1 (which doesn't fit in a byte) or the actual value read.

There is no type-mismatch.

…the function needs to return a larger value - one that can hold all possible legal values and one or more non-legal values.

…so what’s needed is a simple type conversion from a 16-bit signed integer to an 8-bit unsigned byte. Thank you, Paul, I get that!

This change…

PORTD = byte(Serial.read()) << 2;

…causes the Arduino to react.

A new problem is that the other two bits are coming up on the wrong pins. ( the other four, actually ) Maybe the type conversion isn’t right? I’ll keep reading, but do you have any ideas?

Update: I’m sending 0 to 255 and the Arduino is counting to 10 over and over.
Update update: It’s on the Perl side. The Arduino monitor yields perfect results. Thanks for your help with Arduino, Paul!