Serial Input Char - String

char inChar = Serial.read();

Did you forget a (char) cast there?

AWOL:

char inChar = Serial.read();

Did you forget a (char) cast there?

Technically yes, but it works just the same, why not try out on a real Uno or similar?

It doesn't matter what the platform is; the read method returns an int.

AWOL:
It doesn't matter what the platform is; the read method returns an int.

Really, a single byte read returns a 2 byte int.

Surely the compiler knows what I mean, and adjusts accordingly.

Compile the code and run it on Uno or similar, I'm not making an issue of this, the source code works period:)

Int, Char whatever, a byte (human conception) is 8 bits of whatever you like to call it.

Really, a single byte read returns a 2 byte int

Except when there's nothing to read, and the read () method returns -1.

It's all documented.

indev2:
Not byting :slight_smile:

Why not?

...R

indev2:
Really, a single byte read returns a 2 byte int.

Surely the compiler knows what I mean, and adjusts accordingly.

my compiler doesn't know crap about my intentions, I need one like yours.

indev2:
Compile the code and run it on Uno or similar, I'm not making an issue of this, the source code works period:)

Int, Char whatever, a byte (human conception) is 8 bits of whatever you like to call it.

As AWOL points out, the function returns an int.

You relied on an implicit cast, which here in Arduino land you can safely bury your head in the sand like that.

A rather touché moment for AWOL (karma++)

BulldogLowell:
You relied on an implicit cast, which here in Arduino land you can safely bury your head in the sand like that.

Are we not in Arduino land right here?

Back off guys, there's no argument here :slight_smile:

BulldogLowell:
my compiler doesn't know crap about my intentions, I need one like yours.

Mine comes free with the Arduino IDE, try it sometime :slight_smile:

Returns 'c' which is unsigned char (8 bits), or -1 if no read.

Please explain...

int HardwareSerial::read(void)
{
  // if the head isn't ahead of the tail, we don't have any characters
  if (_rx_buffer_head == _rx_buffer_tail) {
    return -1;
  } else {
    unsigned char c = _rx_buffer[_rx_buffer_tail];
    _rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
    return c;
  }
}

What is there to explain?
What is it about unsigned char you don't understand?

AWOL:
What is there to explain?
What is it about unsigned char you don't understand?

What is it that you have not read in earlier posts? Arduino 'int' is a 16 bit word, Serial.read() does not appear to return a 16 bit word. It returns unsigned char (8 bits).
Hence one can do...

Serial.write(Serial.read());

Casting...

char inChar = (char)Serial.read();

is not strictly necessary

8 bits in = 8 bits out. (data bits that is)

indev2:
8 bits in = 8 bits out. (data bits that is)

no, look at the function... it returns an int of a 16 bit signed flavor

lookie here:

void setup() 
{
  Serial.begin(9600);
  Serial.println(proof(1));
  Serial.println(sizeof(proof(1)));
  Serial.println(proof(0));
  Serial.println(sizeof(proof(0)));
  Serial.println(uint8_t(proof(0)));
  Serial.println(sizeof(uint8_t(proof(0))));
}

void loop() {}

int proof(int val)
{
  
  if(val == 1)
    return -1;
  unsigned char c = 0xFF;
  return c;
}

outputs:

-1
2
255
2
255
1

Serial.read() does not appear to return a 16 bit word

Read it again

Hint: try to figure out how to represent -1 in an unsigned value.

AWOL:
Read it again

Hint: try to figure out how to represent -1 in an unsigned value.

Cool stuff, for sure you can't do that. What I don't get is how Serial.write() does a byte for word transcription on 16 bit words from Serial.read(). Obviously I need to enlighten myself with the .write() source, thanks for the heads up. Another learning day for sure.
Thanks Guys.

You're welcome

DCloud:
I can't use an end line character because I'm receiving data from another board which does not send end

character, and I cannot change this.

At this point I would make a sketch that reads data from that board and for each char print the hex value read, some spaces and then the millis time it was read just to find out what that board really does send and when.

Throw some light on what so far is d_cking around in the dark. What you learn determines how to deal with it.