SoftwareSerial and Ethernet don't work together

Hi, I have Arduino Uno and Ethernet Shield ENC28J60. I have code separated into two files, one using SoftwareSerial.h and other EtherShield.h Each piece works separately well, but when I combine them together, SoftwareSerial reads junk. I tried all possible digital pins to read serial data yet this is all unsuccessful. Do you think EtherShield.h may conflict with SoftwareSerial? if so, how can I read data from digital pins and run TCP/IP at the same time? Thanks, Vlad

This is a programming forum. We look at code, here. Where’s yours?

Here is sample code - I just put esentials in. When I read data from serial after few characters I start receiving junk. Then after few characters the message is OK again. I assume message gets out of sync due to EtherShield using the interrupts?? I am not sure how to solve this.

Additional Note: Problem seems to be with SoftwareSerial - I found in one of other posts about ns.read() - I apparently call this and no data is available.

Fix: Allright, found where the issue is. ns.available() must be called before each byte. I am just not sure why it worked without Ethernet module just fine.

Correct loop

void loop(){

  if (ns.available()) {
    char c = ns.read();
    
    int buflength;
    
    linebuffer[linebufferIx++] = c;
    
    if (c == '\n'){
       Serial.write(linebuffer); 
       Serial.println(); 
       linebufferIx = 0; 
    }
    
  }
}
#include <SoftwareSerial.h>
#include <EtherShield.h>

SoftwareSerial ns = SoftwareSerial(6, 7);
EtherShield es=EtherShield();

#define MAXBUFLEN 255
char linebuffer[MAXBUFLEN+1];

void setup(){
  Serial.begin(57600);
  ns.begin(57600); 
  networkSetup();
}

void loop(){

  if (ns.available()) {
    int buflength;
    buflength=nsreadline(linebuffer,MAXBUFLEN,10);
    if (buflength){
      Serial.write(linebuffer); // reads junk after 20-40 characters
      Serial.println();
    }
  }
}

// network section
#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];

static uint8_t gwip[4] = { 
  0,0,0,0};
static uint8_t dnsip[4] = { 
  0,0,0,0 };
static uint8_t dhcpsvrip[4] = { 
  0,0,0,0 };


static uint8_t mymac[6] = { 
  0x54,0x55,0x58,0x12,0x34,0x56 };

static uint8_t myip[4] = { 
  192,168,1,100 };
static uint8_t mynetmask[4] = { 
  255,255,255,0 };


void networkSetup(){
  Serial.println("Network Init");
  delay(1000);
  es.ES_enc28j60SpiInit();
  es.ES_enc28j60Init(mymac);


  if( es.allocateIPAddress(buf, BUFFER_SIZE, mymac, 80, myip, mynetmask, gwip, dhcpsvrip, dnsip ) > 0 ) {
    Serial.print( "My IP: " );
    printIP( myip );
    Serial.println();

    Serial.print( "Netmask: " );
    printIP( mynetmask );
    Serial.println();

    Serial.print( "DNS IP: " );
    printIP( dnsip );
    Serial.println();

    Serial.print( "GW IP: " );
    printIP( gwip );
    Serial.println();
  }
  Serial.println("Network Setup Completed");
}


// Output a ip address from buffer
void printIP( uint8_t *buf ) {
  for( int i = 0; i < 4; i++ ) {
    Serial.print( buf[i], DEC );
    if( i<3 )
      Serial.print( "." );
  }
}

// reads line into buffer
int nsreadline(char *buff, int maxlen, int timeout) {
  int idx;
  char c;

  // expecting message which starts with <msg>...
  if (ns.read() != '<' && ns.read() != 'm'){
    return 0;
  }

  for (idx=0; idx < maxlen; idx++) {
    buff[idx] = 0;

    c = ns.read();

    if (c == '\n') {
      return idx;
    } 
    else {
      buff[idx] = c;
    }
  } 

  return idx;
}

Hi,

i also assume the interrupts are causing your problem.

I have a problem with an IR-Library (using Interrupts) and SoftwareSerial as well.

Somehow one of the timers may be changed to another one, but i dont know how.