Software Serial & hardware serial at the same time

Hi ! I see the Arduino Software Serial Interface , and I make it work . but when I try to use "hardware serial" and "Software Serial" at the same time , only "Software Serial" work , is there somebody can make them work at the same time ? or it is impossibility?

my code:

#include <ctype.h>

#define bit9600Delay 84  
#define halfBit9600Delay 42
#define bit4800Delay 188 
#define halfBit4800Delay 94 

byte rx = 6;
byte tx = 7;
byte SWval;

void setup() {
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  digitalWrite(tx,HIGH);
  digitalWrite(13,HIGH); //turn on debugging LED
  SWprint('h');  //debugging hello
  SWprint('i');
  SWprint(10); //carriage return
  beginSerial(9600);
}

void SWprint(int data)
{
  byte mask;
  //startbit
  digitalWrite(tx,LOW);
  delayMicroseconds(bit9600Delay);
  for (mask = 0x01; mask>0; mask <<= 1) {
    if (data & mask){ // choose bit
     digitalWrite(tx,HIGH); // send 1
    }
    else{
     digitalWrite(tx,LOW); // send 0
    }
    delayMicroseconds(bit9600Delay);
  }
  //stop bit
  digitalWrite(tx, HIGH);
  delayMicroseconds(bit9600Delay);
}

int SWread()
{
  byte val = 0;
  while (digitalRead(rx));
  //wait for start bit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(halfBit9600Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay); 
    delayMicroseconds(bit9600Delay);
    return val;
  }
}

void loop()
{
    SWval = SWread(); 
    SWprint(toupper(SWval));
    delay(100);
    printString("hello!");
}

Hi! I make them work at the same time now! :smiley: the "SWread()" should add a overtime detect.

Cool. Can you post the revised version of the code?

I only read 10 times in Software Serial , if on data come, try next loop, no still wait . these code just fit my purpose .
hope it is useful.

#include <ctype.h>

#define bit9600Delay 84  
#define halfBit9600Delay 42
#define bit4800Delay 188 
#define halfBit4800Delay 94 

int flag,i;

byte rx = 6;
byte tx = 7;
byte SWval;

void setup() {
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  digitalWrite(tx,HIGH);
  digitalWrite(13,HIGH); //turn on debugging LED
  SWprint('h');  //debugging hello
  SWprint('i');
  SWprint(10); //carriage return
  beginSerial(9600);
  flag=0;
}

void SWprint(int data)
{
  byte mask;
  //startbit
  digitalWrite(tx,LOW);
  delayMicroseconds(bit9600Delay);
  for (mask = 0x01; mask>0; mask <<= 1) {
    if (data & mask){ // choose bit
      digitalWrite(tx,HIGH); // send 1
    }
    else{
      digitalWrite(tx,LOW); // send 0
    }
    delayMicroseconds(bit9600Delay);
  }
  //stop bit
  digitalWrite(tx, HIGH);
  delayMicroseconds(bit9600Delay);
}

int SWread()
{
  byte val = 0;
  for(i=0;i<10;i++){

    if (digitalRead(rx) == LOW && flag==0) {
      delayMicroseconds(halfBit9600Delay);
      for (int offset = 0; offset < 8; offset++) {
        delayMicroseconds(bit9600Delay);
        val |= digitalRead(rx) << offset;
      }
      //wait for stop bit + extra
      delayMicroseconds(bit9600Delay); 
      delayMicroseconds(bit9600Delay);
      flag=1;
    }
    return val;
  }
}

void loop()
{
  SWval = SWread(); 
  SWprint(toupper(SWval));
  delay(100);
  printString("hello!");
}