Help needed with Serial issue between two arduinos

Hi all,

I am trying to upload sketches from one Arduino (a mega2560) to another (a pro mini) through Serial, and I have trouble getting the two boards in sync over Serial.

The code works perfectly between the mega and a UNO, however with the pro mini I get loads of 0x0 from Serial.read().

I never read unless Serial.available() has returned true; I don't see 0x00 described as a special value in the Serial doc?
Here is a sample output: after 7-8 attempts I get a valid response from the pro mini, but the next command (get signature) fails when I again read a 0x0.

Stk500 started at 38400
STK: Created -> Begun
Resetting target board... done.
Synching at 38400 bps...SND 0x30 0x20
RCV 0x0
SND 0x30 0x20
RCV 0x0
SND 0x30 0x20
RCV 0x0
SND 0x30 0x20
RCV 0x0
SND 0x30 0x20
RCV 0x0
SND 0x30 0x20
RCV 0x0
SND 0x30 0x20
RCV 0x0
SND 0x30 0x20
RCV 0x0
SND 0x30 0x20
RCV 0x14
RCV 0x10
 done.
AVR device initialized and ready to accept instructions.
SND 0x75 0x20
RCV 0x0
Could not read device signature: 0x15

The code I use to read Serial is below; the same code with no timeout detection yields the same results:

uint32_t start = millis();
    while (!_line.available()) {
        if (millis() - start > STK_TIMEOUT) {
            return (uint8_t) Stk::NOSYNC;
        }
    };
    uint8_t b = (uint8_t) _line.read();
#ifdef DEBUG
    _ui.print(F("RCV"));
    printByte(_ui, b);
    _ui.println();
#endif
    ...

I have also tried to save _line.read() into an int to check for -1, and that isn't the problem, it's indeed 0.

Any hint??

Thanks!
Franck

Any hint??

Provide complete code. That micro-excerpt doesn't show any relevant code.

I have posted the complete code in this other thread:

Franck

franck102:
I have posted the complete code in this other thread:

Please don't start two Threads on the same subject. Double posting is not appreciated as it just wastes everyone's time.

...R

Robin2:
Please don't start two Threads on the same subject. Double posting is not appreciated as it just wastes everyone's time.

...R

It's in some way an unrelated topic.

franck102:
I never read unless Serial.available() has returned true

Not solving the problem but Serial.available() does not return true and possibly nit picking; it returns the number of characters available in the software buffer.

That is not the same subject.
Let me rephrase the question I have in this thread if that helps: do you know of any situation where Serial.read() would return 0, when there is not a device connected at the other end that wrote a 0 on the line?

That being said I have been looking at the minicore source (that is the bootloader on the pro mini) and it does write 0x00 in a couple places, although not during sync nor during get signature

I also checked the avrdude code and I don’t see any handling of 0’s...

Franck

This may we’ll be a hardware issue, I’ll test this out tomorrow:

Franck

This was indeed noise on the Serial line around reset, enabling the internal pullup on the Rx & Tx pins solved it right away:

void Stk500Client::begin(unsigned long baudRate)
{
    if (_line) {
        _line.end();
        delay(5);
    }
    _line.begin(baudRate);
#ifdef SERIAL_RX
    pinMode(SERIAL_RX, INPUT_PULLUP);
#endif
#ifdef SERIAL_TX
    pinMode(SERIAL_TX, INPUT_PULLUP);
#endif
    while (!_line) {}
    // Drain noise
    while (_line.available() > 0) {
        _line.read();
    }

Thanks!