Search code examples
arduino-unoserial-communicationsoftware-serial

Arduino SoftwareSerial library doesnt work


I have a very simple piece of code for Arduino Uno. Using SoftwareSerial lib.

#include <SoftwareSerial.h>
#define rxPin 6
#define txPin 7
SoftwareSerial mySerial(rxPin, txPin);

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  Serial.begin(9600);
  mySerial.begin(2400);
  Serial.println("Serial init");          
}

void loop() {
  delay(2000);
  Serial.println("Serial link started");

  mySerial.write(0x15);

  if(mySerial.available() ){
    int incomingByte = mySerial.read();
    Serial.print(incomingByte);
    Serial.print(" is here");   
  }
}

rxPin and txPin are connected directly to each other. Arduino is also connected to PC throught USB. I have no respone from mySerial.available() loop, mySerial.read() returns -1. Does anyone know, where could be the problem? SoftwareSerial doesnt work for me.


Solution

  • The method SoftwareSerial::write() disables interrupts while writing, at this line. So when you are writing out your byte the arduino is deaf to the incoming bits (except maybe for the first rising edge, that maybe will be delayed until when interrupts are enabled again - I don't actually remember).