Pages: [1] 2 3 4   Go Down
Author Topic: Softwareserial  (Read 9290 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Every time I try to bring softwareserial into my code I get this error:

In function 'void setup()':
error: expected unqualified-id before '.' token

I've searched the forum and references and cann't figure out what I'm doing wrong.  thanks


my code looks like this:
#include <SoftwareSerial.h>



int val = 0;
char code [10];
int byteread = 0;
int EnPin = 2;

void setup()
{
  Serial.begin(2400);
  pinMode(2,OUTPUT);
  SoftwareSerial(7,8);
  SoftwareSerial.begin(9600);
  
}

void loop()
{
  
  digitalWrite(EnPin,LOW);
  if (Serial.available()>0)
  {
    if((val = Serial.read()) == 10)
    {
      byteread=0;
      while (byteread < 10)
      {
        if (Serial.available()>0)
        {
          val = Serial.read();
          if ((val == 10) || (val == 13))
          {
            break;
          }
          code[byteread]=val;
          byteread++;
        }
      }
      if (byteread==10)
      {
        Serial.print("Tag code is ");
        Serial.println(code);
      }
        digitalWrite(EnPin,HIGH);
        delay(500);
    }
  }
}
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 11
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

SoftwareSerial doesn't work quite the same way as the regular Serial.  You need to declare an instance of it, then call begin(), etc. on that instance not on SoftwareSerial.  See the example at: http://www.arduino.cc/en/Reference/SoftwareSerialExample
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm still having problems with this.  Everything works until I try to begin the software serial.  The program compiles but does not run.  If I comment it out everything works perfectly.

#include <SoftwareSerial.h>

int val=0;
char card[10];
int byteread=0;
#define RFIDEN 2
#define rxPin 5
#define txPin 6

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()
{
pinMode(RFIDEN,OUTPUT);
Serial.begin(2400);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
// mySerial.begin(9600);
}
  
  
void loop()
{
  GetRFID();
  mySerial.println(card);
}
Logged

Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The program compiles but does not run.  

hi

Do you mean that it compiles and you can upload it, but it doesn't appear to work?

D
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SoftwareSerial has some timing issues. It doesn't push the tx pin high when started, that can ruin the first character, but you can work around that by setting it high when you set the pinmode for it.

Worse, the rather expensive math operation to compute the bit delay gets shuffled into an unfortunate code path and makes the start bit be about 1.5 bit times long ensuring that your bits are sampled on the edges instead of the centers

Still, I wouldn't expect you to get nothing from that. I'd expect you to get a few percent of your characters garbled, or all of them, depending on the timing of your receiver's uart.

(Patches are in the works for 0008 software serial to stabilize it.)

SoftwareSerial::begin() just stores your bit rate in an instance variable. It seems unlikely that could cause a problem directly unless you have run out of SRAM. If you don't do a ::begin() you will get a divide by zero error in print. I don't know what that does.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino 1 - Programmer 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting to see that someone came across a similar problem.

I am currently using the Arduino Mini to read data from an RFID reader and send it through the serial. To communicate with the RFID reader (which also talks through serial, 9600 baud rate) I need to use a SoftwareSerial.

Whenever a call to SoftwareeSerial.read() is made I get some character printed out on the USB Serial (usually 'b') *even* if I don'call any printing function; and then the Mini resets  :-?

This is exactly the same behaviour that I get if I don't have anything plugged onto my designated software serial ports.

I have previously connected the RFID reader directly to the Arduino USB and I am sure that it is working. I also have tried programming the Mini so that the RFID reader is connected to the hardware serial pors and the USB to the software ones. The result is the same, the Mini keeps on resetting.

Code follows:

#include <SoftwareSerial.h>

#define rxPin 3
#define txPin 2
#define ledPin 13

// set up a soft serial port
SoftwareSerial mySerial(rxPin, txPin);

void setup()  {

  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
  // set txPin high (as recommended in forum)
  digitalWrite(txPin, HIGH);
  
  // set the data rate for the serial ports
  mySerial.begin(9600);
  Serial.begin(9600);
  
  // say something
  Serial.println("Hello World!");

}

void loop() {  

  char someChar = '0';
  someChar = mySerial.read();

  // print out the character:
  Serial.print(someChar, DEC);

  // toggle an LED just so you see the thing's alive.  
  // this LED will go on with every OTHER character received:
  toggle(13);

}



So does SoftwareSerial implement the whole serial protocol or should I be sending some character to start the serial communication? Altough the reset thing is quite strange....

Cheers!
TMSM
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it's interesting i'm having trouble implementing softwareserial also. i am just trying to use the basic example at the arduino.cc site to see how it works  - http://www.arduino.cc/en/Reference/SoftwareSerialExample - but it does not run properly. i've also tried some of the suggestions here (setting tx high in setup) but that has had no effect. has anybody run this successfully?
Logged

Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

hi

in order for use to give you a good answer you need to give us more detail.. code, pictures, videos, text description. Unfortunately most of us failed our courses on mind-reading at school. smiley

D
« Last Edit: April 23, 2007, 02:54:28 pm by Daniel » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
hi

in order for use to give you a good answer you need to give us more detail.. code, pictures, videos, text description. Unfortunately most of us failed our courses on mind-reading at school. smiley

D

i'm not sure what additional information you would need. i am using the exact same code used in the softwareserial example - http://www.arduino.cc/en/Reference/SoftwareSerialExample - with no modifications or changes. i wanted to see first if the example would actually work. i have an arduino NG i am running the latest version (0007). in the example serial data is sent through pins 2 and 3 - again as described in the example which i have not modified.

pins 2, 3 are connected to a FTDI rs232 usb-serial adapter. using the regular serial commands it works just fine. when i switch the code over to use softwareserial it does not work.

the only thing changed in this setup is the addition of the softwareserial declaration and changing the instances of send.serial(), read.serial(), etc. to the softwareserial command detailed in the example.

so i'm still wondering what the problem might be.

just wondering, has anybody implemented the example successfully?
« Last Edit: April 23, 2007, 03:34:12 pm by myusername » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino 1 - Programmer 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it keep on resetting like mine? This is the thing I find most weird...  :o
Logged

Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

hi

Software serial is going to be a lot more finicky than the regular hardware serial. This is because hardware serial on pins 0 and 1 have a dedicated UART that sits there all the time, dealing with the data coming in and out. If the processor is busy, the hardware serial port can still receive data, until its buffer is full.  

Software serial, by contrast,  is only going to work if the processor is executing commands related to software serial. This means it is  possible to miss incoming data, as you might be off blinking an LED or something else when it arrives.   I have used sws for non-critical applications like sending data to an LCD, but I wouldn't use it for Arduino-host communication for the above reasons.

D
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
hi
I have used sws for non-critical applications like sending data to an LCD, but I wouldn't use it for Arduino-host communication for the above reasons.
D

well i have been trying to test softwareserial by sending the data to hyperterm from pins 2 and 3 connected to an FTDI re232 serial/usb converter. i also tried modifying the sample code so that the arduino sends data continuously figuring that something would get through - but to no avail. it does not work at all.

any suggestions? i eventually want to send data to a serial controlled mp3 player. i do not need complicated call and response - in fact i don't even really need serial.read() - but i seem to have trouble getting even a basic system working.
Logged

Daniel
Guest
 Bigger Bigger  Smaller Smaller  Reset Reset

hi

sending to hyperterm is actually not the best way to test it... there may be some handshaking or similar between the RS232-USB chip and the computer.

Now that you mention it, one other problem may be the RS232 itself. What kind of converter amodule are you using?

Any time I have used SWS it has been to send data to a TTL serial connection  (0/5V), rather than an RS-232 connection (+/-  3 to 15V).

If your 232-USB converter needs 'real' RS-232 levels, you will have to provide a real RS232 voltage swing, and possibly also inverted data form the TTL pin. If you look at the original Arduino schematic they show you how to achieve this with a couple of transistors.    

D
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have been using this module - http://www.sparkfun.com/commerce/product_info.php?products_id=718 - the FT232RL USB to Serial breakout board, but set for 5v instead of 3.3v.

it's funny because the mp3 player i have is TTL serial. i've been using the FT232RL USB to Serial breakout board to communicate with the player through hyperterm, which works perfectly so i know it can take TTL level signals.

i have also tried interfacing directly to the mp3 player, but that has not yet worked. i was hoping to test anyway with something else since the mp3 player is the only one i currently have.

any suggestions?
« Last Edit: April 23, 2007, 06:46:27 pm by myusername » Logged

Pages: [1] 2 3 4   Go Up
Jump to: