Go Down

Topic: SoftwareSerialExample doesn't work well. (Read 1 time) previous topic - next topic

PeterH

The hardware serial is running at 57600 baud so the fact the serial monitor shows rubbish at 4800 is expected. The lower screen shot at 57600 should work, from my understanding of the sketch and your wiring. It correctly shows the output from the local (right) Arduino when you reset it. I assume that the corrupted characters resulted from the reset of the remote (left) Arduino. That should be sending "Hello, world?" but you received something like "C*±±½± °½É± 'ýjRü".

That's not exactly the right number of characters but is roughly the right length, and the "±±" where "ll" should appear suggests that you're seeing a mangled version of the original text.

If you have another USB cable I'd suggest connecting both Arduinos to the PC and open a second instance of the serial monitor to see what you're getting from the left Arduino's hardware serial. It almost looks as if the two boards are running at different speeds or different serial encoding settings, although I don't see any reason why that would happen.
I only provide help via the forum - please do not contact me for private consultancy.

UKHeliBob


Wiring looks fixed. 10 to 11, 11 to 10, GND to GND.

That would be the wiring for soft serial to soft serial though.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

GoForSmoke

That's what he should be doing between UNO's isn't it?
Just needs to change the code a wee bit?
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

BimBee

i'm new to arduino i can't figure out how to fix when i get the error like this

sketch_jan28b:0: error: 'SoftwareSerial' does not name a type
sketch_jan28b:0: error: 'Wire.h' does not name a type
sketch_jan28b:0: error: 'NewSoftwareSerial' does not name a type
there is no 'NewSoftwareSerial' on the file
there is no 'wire.h' on the file





PaulS

Quote
i'm new to arduino

That's no excuse for not telling us which version of the IDE you are using or posting your code.

Try again.

BimBee

I'm using ver 1.0

Code: [Select]
#include "SoftSerial.h"
#include <string.h>
#include <Wstring.h>

SoftSerial cell(2,3);

int bytesread = 0;
int  val = 0;
char s1[20]= "+CMTI: \"SM\",1";
char s2[20]= "OK";
char s3[20]= "OFF";
char s4[20]= "ON";
int ledpin = 13;   

void setup()
{
  Serial.begin(9600);
  cell.begin(9600);
  pinMode(ledpin, OUTPUT);

  Serial.println(" ");
  Serial.println("Starting Communication...");

  cell.println("at");
  delay(500);
  cell.println("ate0");
  delay(500);
  cell.println("AT+CMGD=1");    //delete sms at memlocation 2
  delay(1000);

  }
void loop()
{
  char incoming_ms[20]=" ";
  String buff = String(20);
  if(cell.available() >0)
  {
   if((val = cell.read()) == 10) {   // check for header
    bytesread = 0;
     while(bytesread 0) {
        val = cell.read();
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
           break;                       // stop reading
           }
         incoming_ms[bytesread] = val;         // add the digit
        bytesread++;                   // ready to read next digit
         }
       }
     }
   buff = incoming_ms ;
  // Serial.println(incoming_ms);
  }

if(buff.contains(s1)&&!buff.contains(s2))
   {
  calling();
}
}

void calling()
{
  int i=0;
  int j=0;
  char incoming_let=0;
  char incoming_char[200]=" ";
  String buff1 = String(200);

  cell.println("at+cmgr=1");
  delay(450);
if(cell.available() >0)
  {
   while(cell.available() >0)
    {
     incoming_let = cell.read();
     incoming_char[j]=incoming_let;
     j++;
     }
   buff1=incoming_char;
   Serial.println(incoming_char);
   change();
  }
if(buff1.contains(s3))
  {
   Serial.println("done!!!  OFF ");
   digitalWrite(ledpin, LOW);
  // send_off();
   }
else if(buff1.contains(s4))
  {
  Serial.println("done!!!   ON");
  digitalWrite(ledpin, HIGH);
// send_on();
  }
}

void change()
{
    //Serial.println("Deleting...");
    cell.println("at+cmgd=1");
    delay(500);
}

void send_on()
{
  cell.println("at+cmgf=1");
  delay(1500);
  cell.println("at+cmgs=+91........");
  delay(1500);
  cell.print("Your device is on");
  delay(500);
  Serial.print(0x1A,BYTE);
  delay(1000);
}

void send_off()
{
  cell.println("at+cmgf=1");
  delay(1500);
  cell.println("at+cmgs=+919.........");
  delay(1500);
  cell.print("Your device is off");
  delay(500);
  Serial.print(0x1A,BYTE);
  delay(1000);
}

GoForSmoke

You wrote all that before trying a compile?
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

PaulS

There are two classes use for software serial - SoftwareSerial for 1.0 and on and NewSoftSerial for 0023 and earlier.

There is no SoftSerial class.

Code: [Select]
  String buff = String(20);
After this, buff = "20". How useful is that?

michinyon

Your loop() function looks ridiculous.

BimBee

#24
Jan 28, 2013, 05:01 pm Last Edit: Jan 28, 2013, 05:22 pm by MrYu2316 Reason: 1
if the sketch is made in the libraries Newsoftserial ver 0023 etc,. the sketch will now work in SoftwareSerial?  :~

i have no idea if this program works. =( i just want to know how to fix if i get library errors. tnx


how about this one?

Code: [Select]
#include <SoftwareSerial.h>
#include <string.h>

char messagemo[] = "Hello!!! World!!!";

int rxPin = 0;
int txPin = 1;

// set up a new serial port
SoftwareSerial gsm = SoftwareSerial(rxPin,txPin);


void setup() {          
 gsm.begin(9600);  //begin serial communication

 pinMode(rxPin,INPUT);     //rx pin 0 for input
 pinMode(txPin,OUTPUT);  //tx pin 1 for output

 gsm.write("AT\r");
 delay(1000);
 gsm.write("ATE0\r");
 delay(1000);
 gsm.flush();
 gsm.write("AT+CFUN=1\r");
 delay(1000);
 gsm.flush();
 gsm.write("AT+CSDH=0\r");
 delay(1000);
 gsm.write("AT+CMGF=1\r");  //0 kapag PDU, 1 kapag text
 delay(1000);
 gsm.flush();

}



i get this error
prog.cpp:1:28: warning: SoftwareSerial.h: No such file or directory
prog:9: error: 'SoftwareSerial' does not name a type
prog.cpp: In function 'void setup()':
prog:13: error: 'gsm' was not declared in this scope
prog.cpp: In function 'void sendSMS(const char*, const char*)':
prog:44: error: 'gsm' was not declared in this scope
prog.cpp: In function 'void deleteSMS()':
prog:56: error: 'gsm' was not declared in this scope

PaulS

The NewSoftwareSerial class was developed because the old SoftwareSerial class had some serious issues. For 1.0, NewSoftSerial was adopted as an official library, and renamed to SoftwareSerial. So, yes, SoftwareSerial is a direct replacement for NewSoftSerial.

Quote
i just want to know how to fix if i get library errors.

It's better to just not make them.

GoForSmoke

This compiles in 1.03 with no errors. I used Sketch->Import Library to import SoftwareSerial.
You should be able to do the same with 1.0. Then add the rest of your code a piece at a time running Sketch->Verify/Compile at each stage. You set yourself up for trouble by throwing a mass of code up before verifying any of it.

Code: [Select]

#include <SoftwareSerial.h>

SoftwareSerial gsm( 2, 3 );

void setup( void )
{
  gsm.begin(9600);  //begin serial communication
}

void loop( void )
{
}


Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

tkawa


The hardware serial is running at 57600 baud so the fact the serial monitor shows rubbish at 4800 is expected. The lower screen shot at 57600 should work, from my understanding of the sketch and your wiring. It correctly shows the output from the local (right) Arduino when you reset it. I assume that the corrupted characters resulted from the reset of the remote (left) Arduino. That should be sending "Hello, world?" but you received something like "C*±±½± °½É± 'ýjRü".

That's not exactly the right number of characters but is roughly the right length, and the "±±" where "ll" should appear suggests that you're seeing a mangled version of the original text.

If you have another USB cable I'd suggest connecting both Arduinos to the PC and open a second instance of the serial monitor to see what you're getting from the left Arduino's hardware serial. It almost looks as if the two boards are running at different speeds or different serial encoding settings, although I don't see any reason why that would happen.



I connected USB cables to both Arduinos.
And I tried both "soft to soft" and "soft to hard" connections, and
both bauds as shown in soft2soft.jpg and soft2hard.jpg.
But, it can't work.
I put screenshots of serial monitors. When I took them, I pushed both
the reset buttons of the Arduinos once each.
In the screenshots, COM16 is the left Arduino in soft2soft.jpg and
soft2hard.jpg, and COM17 is the right.
I changed the Arduinos to new ones, but the result was same.

I can't upload all files for the maximum attachment size allowed, so I do only files of "soft to soft".
I'll upload files of "soft to hard" in my next comment.

tkawa

I upload the files of "soft to hard".

GoForSmoke

More troubleshooting then. Time to nail down when and where the failure takes place.

Maybe try a delay() before and after Serial.begin() in setup()?

Or hold up soft-soft serial until user-i/o from serial monitor transmits "GO" in response to prompt and led 13 ON on both UNO's to show each is initialized?
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up