Go Down

Topic: Basic SoftwareSerial read not successful (Read 524 times) previous topic - next topic

mstomasik

Hello,
I have been trying to get a GSM modem to work for days now.  The problem seems to be with the SoftwareSerial connection.  So I backed up and tried to master SoftwareSerial.  First by trying the example in Arduino, did not return the expected output.

So I wrote the following simple program in an attempt to power through it.  Still no success.

I am using a genuine ARDUINO Uno R3 with version 1.6.13 of the programming environment.

This sketch opens the hardware and software serial ports at 9600 baud.  It then writes a singe ASCII character (in decimal) to the Software Serial port, then reads it to the hardware serial port and displays in on the Serial Monitor.

I have tried numerous approaches and consistently get a -1 output, interspersed with a couple of 0's which leads me to believe it is a timing problem.

Code is as follows: (output below that)



/* Simple test program to learn about SoftwareSerial basics
 *  It is clear that I have misunderstood something basic
 */

#include <SoftwareSerial.h>
int rxPin = 10;
int txPin = 11;

//Initialize variable to hold single ASCII character value
int c = 0;

//Initialize counter variable
int i =0;

//Initialize instance of SoftwareSerial
SoftwareSerial MySerial(rxPin,txPin);

void setup() {

pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);

//Start hardware serial
Serial.begin(9600);
delay(100);
Serial.println("Serial at 9600");

//Start software serial
MySerial.begin(9600);
delay(100);
Serial.println("MySerial at 9600");
delay(100);
}

void loop() {

while (i < 20) {
//Write ASCII 84 ("T") to software serial
MySerial.write(84);
delay(100);

//Read value from SoftwareSerial buffer to variable c
c = MySerial.read();
delay(100);

//Print value of variable c to serial monitor
Serial.println(c, DEC);
MySerial.flush();
i=++i;
}
}

OUTPUT


Serial at 9600
MySerial at 9600
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
0
-1
-1
-1
-1
-1
-1
-1


Any help will be gratefully appreciated.  Clearly I have some fundamental misunderstanding about SoftwareSerial taht I just cannot see.

Michael

UKHeliBob

What is connected to pins 10 and 11 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mstomasik

Nothing....somewhat following the Arduino example which has nothing connected to the digital pins.

mstomasik

HKH.....nothing connected to 10 & 11, or any other pins for that matter, per example Arduino sketch.

UKHeliBob

Nothing....somewhat following the Arduino example which has nothing connected to the digital pins.
What input are you expecting with nothing connected ?

-1 is what the read() function returns when there is nothing available to read.

If you look at the comments at the top of the SS example in the IDE you will see that it is intended to be run on 2 Arduinos cross connected between pins 10 and 11
Quote
* RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

mstomasik

Oh man,I totally missed that.  Perhaps there was another test sketch which showed no wiring.  At any rate, I expected that my sketch would write to a virtual SerialSoftware buffer then the "regular" serial functions would read from that buffer and write it to the SerialMonitor.  All on the same board.

mstomasik

UKHeliBob,

Based upon your comment, I will set up another Uno to transmit a text message once per second, then try to alter this sketch to receive and send it to the Serial Monitor.

Thanks for you help.  Will post a reply when it is done.

Michael

Robin2

You cannot do a loop-back test with SoftwareSerial. In other words it cannot send stuff to itself because it cannot listen and talk at the same time (whereas HardwareSerial can).

If you want to test SoftwareSerial then you need to send the data to another Arduino or use a USB-TTL cable to connect it to another USB port on your PC.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

-dev

Like Robin2 said, SoftwareSerial cannot RX and TX at the same time.  However, AltSoftSerial (pins 8 & 9 only) and NeoSWSerial (any pins) can.

Cheers,
/dev
Really, I used to be /dev.  :(

mstomasik

UKHeliBob,
Thanks again.  I set up a second Uno and had some success transmitting via SoftwareSerial.  Still some gltiches but I am confident I can work them out.

Go Up