serial on different pins than 0 or 1

Finally, Fabrice and I got something quite interesting. The previous code for software Rx didn't work for us. So we changed some things (don't ask us why it works now, we don't really know ...).


| | __________ | | | |
| PC1 |-------| MAX233 |--------| Arduino |-----------| PC2 |
|| || || |__|

Our PC1 serial Tx is connected on our new software Rx (pin 6) through a MAX233 (to switch from RS232 to TTL). Arduino is connected to PC2 with the classical hardware serial link.
When I type a text in a hyperterminal on PC1, it is received by Arduino and sent back on PC2 hyperterminal.

It works for a typed text, but not yet by sending a text file.

Here is the modified software Rx code:

#define BAUDRATE 9600

// the width of a single bit of serial data in microseconds
// in each second (1000000 microseconds) there are a number of bits equal
// to the baud rate (e.g. 9600), so each bit lasts for 1000000 / baudrate
// microseconds
int width = 1000000 / BAUDRATE;
int fudge = -25;
unsigned int val = 0;
int count = 0;
int state = 0;

int rx = 6;

void setup() {
  pinMode(rx,INPUT);
  digitalWrite(13,HIGH);
  beginSerial(BAUDRATE);
}

void loop() {
  int offset;
  boolean error = true;

  // one byte of serial data (LSB first)
  // ...--\    /--\/--\/--\/--\/--\/--\/--\/--\/--...
  //       \--/\--/\--/\--/\--/\--/\--/\--/\--/
  //      start  0   1   2   3   4   5   6   7 stop

  while (digitalRead(rx));

  // frame start indicated by a falling edge and low start bit
  // jump to middle of start bit
  //delayMicroseconds(width / 2 + fudge);

  // confirm that this is a real start bit, not line noise
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(width/2);
    
    for (offset = -1; offset < 8; offset++) {
      // jump to middle of next bit
        val |= (digitalRead(rx) << offset);
      delayMicroseconds(width + fudge);
    }

    // check stop bit is high
    delayMicroseconds(width + fudge);
    if (digitalRead(rx) == HIGH) {
      error = false;
    }

    Serial.print(val,BYTE);
    val = 0;

  }
}

It seems to be very sensitive to time delay. As soon as you add some code, the pin value can be sampled at a wrong instant.

Antonio