Pages: 1 ... 7 8 [9] 10 11   Go Down
Author Topic: NewSoftSerial Library: An AFSoftSerial update  (Read 17175 times)
0 Members and 1 Guest are viewing this topic.
Holland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 439
Arduino likes cookies too
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I located the culprit.
It was something related to timing (as I expected), though it was of a rather unexpected cause..
Found the problem thanks to this topic: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1268626974
Turns out that the read() method of NewSoftSerial also needs a tiny delay in order to get the actual value.

Still a bit annoyed that this isn't documented, but that'll pass.
Now the whole world of the arduino will open for me =)
Thank you to anybody who spent time trying to solve my conundrum.
Logged

Seattle
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is regarding the inability to get serial binary data at 4800N1 from a W800RF32 unit from an earlier post.
http://www.wgldesigns.com/w800.html
http://www.wgldesigns.com/protocols/w800rf32_protocol.txt

I'm able to see it's serial data when connected to a standard PUTTY console window.  The characters are 'garbage' as expected because it's binary data.

I'm also able to demonstrate that my circuit & sketch can read data at 4800N1 from a serial console window (connected to RX=2, TX=3).

But Serial.available() never returns > 0 when I connect the W800RF32A to the serial input.  If I don't use Serial.available() and just try a read, I only get 0xFF.

I'm starting to suspect that flow control is playing a role here.  The makers of the W800RF32 (wgldesigns) state that only TX/RX (and GND) are used by the unit.  However, is NewSoftSerial being thrown off by the binary serial data?  (By the way, I've tested with SoftwareSerial - same result).  

Is NSS using software flow control that is being tickled by the binary data? (XON/OFF).

Any working example for reading serial binary data?

-creatrope
« Last Edit: March 16, 2010, 05:55:49 pm by creatrope » Logged

Seattle
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Solved!

Nothing to do with flow control, it was the old serial culprit, forgetting to take into account DCE and DTE units.  With a NULL modem dongle installed between the Arduino's serial adapter and the W800RF32A, I started seeing valid serial data - from there getting the W800RF32A was a snap.

-creatrope
Logged

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

Hi,
i'm developing a university thesis and i need RTS/CTS control flow. my device talks at 38.4K bps.
from Mikalhart's (NewSoftSerial) posts i noticed at this rate TX is OK but RX is 99.9%.
2 questions:
- is it the ONLY library using RTS/CTS pins?
- what is the AFSoftSerial?

thank you,
mario
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mario,

AFSoftSerial ("AdaFruit Software Serial") was the first library that implemented software serial using interrupts.  This was an important improvement that allowed big projects to use Soft Serial.

NewSoftSerial is my attempt to tidy up AFSoftSerial and add a few new features.  Unfortuately for you, RTS/CTS is not (yet?) one of them.  I should mention, though, that supporting 38.4K baud is not necessarily a deal breaker.  There are a number of sketches out there that use that successfully.

Mikal
Logged

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

ok.
I've seen the code by Jin here http://public.me.com/jinschoi
Jin said:
The code is based on an earlier version of NewSoftSerial, version 2 or 3. It also contains code for RTS/CTS flow control, which I found I needed for my project.

@ Jin can you tell me if it works good, and maybe at what max speed?
thank you,
mario
Logged

Batangas, Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there something wrong with my implementation of nss? It doesn't seem to work. I'm trying to pass integers here. The led (which doesn't  light up on the other side) alone tells me something's wrong. On the serial monitor I get 10000++ numbers. thanks in advance...

here are my codes:
Code:
#include <NewSoftSerial.h>

const int led = 13;
const int rx = 2;
const int tx = 3;
NewSoftSerial mySerial(rx, tx);

int data = 0;
int serRecv[2];

void setup()
{
  mySerial.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  if( mySerial.available() > 0 )
  {
    serRecv[0] = mySerial.read();
    serRecv[1] = mySerial.read();
    
    data = (serRecv[0]<<8) + seRecv[1];
    Serial.println(data, DEC);
  }
  
  if(data > 1800)
    digitalWrite(led, HIGH);
  else
    digitalWrite(led, LOW);
}
    
Code:
#include <NewSoftSerial.h>
#include <Wire.h>
#include <CMPS03.h>

CMPS03 cmps;

const int led = 13;
const int rx = 2;
const int tx = 3;
NewSoftSerial mySerial(rx, tx);

int serData[2], cmpsReading;

void setup()
{
  mySerial.begin(9600);
  Wire.begin();
}

void loop()
{
  cmpsReading = cmps.read();
  mySerial.print((cmpsReading>>8));
  mySerial.print(cmpsReading);
  
  if(cmpsReading > 1800)
    digitalWrite(led, HIGH);
  else
    digitalWrite(led, LOW);
}

Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you are waiting for one character but trying to read two.
try:
  if( mySerial.available() > 1 )

also, binary data can be tricky because it can be hard to identify which byte is the first of the two bytes. It may be easier to send the values as ascii digits.
« Last Edit: March 31, 2010, 08:16:13 am by mem » Logged

Batangas, Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks mem!
I tried this: if(mySerial.available() > 1) but didn't work.

I tried using ascii but it still doesn't. Here are my codes:
Code:
#include <NewSoftSerial.h>

const int led = 13;
const int rx = 2;
const int tx = 3;
NewSoftSerial mySerial(rx, tx);

int data = 0;
int serRecv[4];

void setup()
{
  mySerial.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  for(int j =0;j<=3;j++)
    serRecv[j] = '\0';
  
  if( mySerial.available() > 3)
  {
    int i = 0;
    while( mySerial.available() )
    {
      serRecv[i] = mySerial.read();
      serRecv[i] = serRecv[i] - '0';
      i++;
    }
  
    int x = 1;
    for(int j=3;j>=0;j--)
    {
      if(serRecv[j] == '\0')
       break;
      else
      {
        serRecv[j] = serRecv[j]*x;
        x *= 10;
      }
    }

    for(int j=0;j<=3;j++)
      data += serRecv[j];
  }
  
  if(data > 1800)
    digitalWrite(led, HIGH);
  else
    digitalWrite(led, LOW);
}
    
Code:
#include <NewSoftSerial.h>
#include <Wire.h>
#include <CMPS03.h>

CMPS03 cmps;

const int led = 13;
const int rx = 2;
const int tx = 3;
NewSoftSerial mySerial(rx, tx);

int serData[2], cmpsReading;

void setup()
{
  mySerial.begin(9600);
  Wire.begin();
}

void loop()
{
  cmpsReading = cmps.read();
  mySerial.print(cmpsReading, DEC);
  
  if(cmpsReading > 1800)
    digitalWrite(led, HIGH);
  else
    digitalWrite(led, LOW);
}

« Last Edit: April 04, 2010, 11:09:19 pm by Homer_Manalo » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the fragment below is closer to what you want, but you still need to add code that determines where the start and end of the digits are in the serial data. the code below assumes that the data always consists of 3 digits but this is probably not correct, but i hope it gives you an idea of how you can convert incoming digit characters into a numric value.

Code:
void setup()
{
  mySerial.begin(9600);
  Serial.begin(9600);
}

 

const int NumberOfDigits = 3; // this assumes you want to receive 3 digits
int data = 0;

void loop()
{
  static int data = 0;
  if ( mySerial.available() >= NumberOfDigits )  // wait until three characters are available
  {
    for(int i=0; i < NumberOfDigits; i++)
    {
      char ch = mySeriall.read();

       if(ch >= '0' && ch <= '9')              // is ch a number?  
          data * 10 + ch - '0';           // yes, accumulate the value
        // note that non-digits will be discarded
    }    
  }
}
« Last Edit: April 05, 2010, 03:41:58 am by mem » Logged

Batangas, Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought that the lock-ups experienced by etracer was already solved but I am experiencing it with my code. I am using arduino 18 with gcc 4.3.2. My OS is Windows XP but it is just a parallel desktop with Mac OSX. I also have the recent v10c of NewSoftSerial. Any suggestions on where to start?
Logged

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

I just installed Arduino 0018 and NewSoftSerial10c on Fedora 12 (which uses avr-gcc 4.4.2). Simply including NewSoftSerial.h into an null sketch (ie void setup(){}/void loop(){}) causes the following error:

Code:
Error: register r24, r26, r28 or r30 required

Examining NewSoftSerial.cpp, I isolated the error to the following section (lines 174-186):

Code:
/* static */
inline void NewSoftSerial::tunedDelay(uint16_t delay) {
  uint8_t tmp=0;

  asm volatile("sbiw    %0, 0x01 \n\t"
    "ldi %1, 0xFF \n\t"
    "cpi %A0, 0xFF \n\t"
    "cpc %B0, %1 \n\t"
    "brne .-10 \n\t"
    : "+r" (delay), "+a" (tmp)
    : "0" (delay)
    );
}

Using well-known AVR assembler web references, I note that the SBIW command can only be used on special register pairs, namely r24, r26, r28, r30. Consequently, it is necessary to tell the compiler to use only these registers for this operation. Changing the constraint used for the "delay" parameter from "+r" to "+w" does this. Consequently, the following causes the error to disappear:

Code:
/* static */
inline void NewSoftSerial::tunedDelay(uint16_t delay) {
  uint8_t tmp=0;

  asm volatile("sbiw    %0, 0x01 \n\t"
    "ldi %1, 0xFF \n\t"
    "cpi %A0, 0xFF \n\t"
    "cpc %B0, %1 \n\t"
    "brne .-10 \n\t"
    : "+w" (delay), "+a" (tmp)
    : "0" (delay)
    );
}

Tests using this to communicate with a GPS module from a 328p Nano shows that it works great.

I am a newcomer to assembler and to Arduino, so I only raise this as a possible solution to this problem - I have no idea on the implications of this change for other processors.

Thanks for the great library.
Logged

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

Since this forum does not allow external references on the first posting (probably to control spam), the web reference I used for the previous posting was http://www.nongnu.org/avr-libc/user-manual/inline_asm.html.
Logged

Batangas, Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok I actually solved the problem by just rearranging my code. I occasionally encounter problems like this. For example this one works:
Code:
if( controlData[4] != lastIn[4] )
{
    if( (controlData[4] == 1) && (controlData[5] != 1) )        
      ...
    else
      ...
}
while this one do not:
Code:
if( (controlData[4] == 1) && (controlData[5] != 1) )
{
  if( controlData[4] != lastIn[4] )
    ...
  else
    ...
}
« Last Edit: April 15, 2010, 08:47:07 pm by Homer_Manalo » Logged

Batangas, Philippines
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

can someone suggest a more efficient code than this one:
Code:
void loop()
{
  if(mySerial.available() >= 6 && mySerial.read()=='q')
  {
    data = 0;
      while(inData != 'p')
      {      
        inData = mySerial.read();
        if(inData >= '0' && inData <= '9')
          data = data * 10 + inData - '0';
      }
  }
  
  
  if(data>=0 && data<=3600)
    Serial.println(data, DEC);
  inData = 0;
  
}

the output is very unstable (supposed to be 2108, 2109... in that range):
Code:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
6
6
6
6
6
6
6
6
0
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
2
2
2
2
2
210
210
210
2110
2111
11
0
0
0
0
0
0
0
0
0
20
20
20
20
20
108
0
217
217
2108
2108
218
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2108
208
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1698
1698
1698
907
2107
2107
2107
2108
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
928
2109
2109
0
2
0
0
0
0
0
0
0
0
0
0
0
0
111
1
1
1
1
1
1
1
1
1
2111
211
210
211
210
210
210
210
210
210
2108
8
218
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2109
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2111
211
211
2111
2111
2109
2109
2109
2109
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2108
2108
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2108
0
0
0
0
0
0
0
0
0
9
9
0
0
0
0
0
0
2109
210
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
219
219
210
110
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
210
211
2100
2100
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2111
211
211
211
211
808
218
2108
2110
210
2110
210
2110
2110
209
2109
219
2109
2108
2108
2108
2108
2108
2108
0
2108
2108
2108
2108
2108
2108
2109
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
508
0
2129
2129
2129
0
110
2110
2110
210
110
2110
2110
2110
211
2111
0
0
211
211
211
211
211
211
212
210
210
210
210
210
2
2
2
2112
110
2109
219
219
20
0
2110
2110
2110
2110
10
210
2110
2029
2
208
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2009
2108
2108
21
2109
0
91
91
91
91
91
91
91
91
91
2112
0
112
2110
211
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
219
2109
2109
2109
3464
3464
3464
3464
3464
3464
3464
3464
3464
3464
3464
3464
3464
3464
2108
2108
2108
2108
218
210
2108
2109
2118
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2108
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
2107
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
8
8
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
112
2212
2212
2212
2212
2212
2212
2212
2212
2212
2212
2212
2212
2212
2212
2212
2212
2212
0
0
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
108
7
210
99
0
0
0
0
0
0
2110
2110
110
2110
210
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2109
2109
2109
2109
2109
20
20
20
20
20
20
20
20
20
20
20
20
20
20
1901
1901
1901
1901
1901
1901
1901
1901
1901
1901
1901
1901
1901
1901
1901
1901
1901
1901
211
2111
212
212
212
0
0
0
0
2108
2108
210
2108
5
210
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
2020
2020
2020
2020
2020
2020
2020
2020
2020
2020
2020
2020
210
219
2109
0
0
0
0
0
2
211
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
2110
299
299
299
299
299
299
0
0
209
20
0
0
0
0
0
0
199
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
210
219
0
0
0
0
0
0
0
218
218
107
107
107
107
107
107
107
107
107
107
107
107
107
107
107
107
107
107
107
107
107
107
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
0
0
0
2
2
2
2
211
0
0
0
2110
2110
2110
2110
2110
111
1110
2110
2110
2110
211
211
2110
0
2108
2
210
210
210
210
210
210
210
210
210
210
210
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
2109
109
210
2109
219
2109
2109
2
2
0
0
0
0
2111
2111
2111
2111
2111
2111
2111
2111
2111
2111
2111
2111
2111
2111
2111
2110
0
211
21
21
21
118
110
110
110
110
110
110
110
110
110
110
110
110
110
110
110
110
110
8
2107
2207
2207
2207
2207
2207
217
2107
210
0
0
0
0
2100
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
211
2110
2110
2110
2110
2110
2110
2110
21
21
21
21
21
21
210
211
0
0
0
0
0
0
0
2
2
0
0
0
0
2109
2109
2109
2109
2109
2109
2109
2109
2109
2
0
0
0
0
0
0
0
0
0
0
0
7
7
7
7
7
7
7
22
2
208
20
9
0
0
0
0
0
0
0
0
2101
2101
2101
2101
2101
2101
2101
2101
2101
111
2111
2111
2111
2

Logged

Pages: 1 ... 7 8 [9] 10 11   Go Up
Jump to: