Pages: 1 [2] 3   Go Down
Author Topic: Reading from Serial  (Read 3884 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok i think i am with you so far, but NSS doesnt pick up anything i send out through the arduino serial monitor. nor does it see anything i echo from the router. Could baud rates be a problem? I would think i would at lease see gibberish pop up in the serial monitor. When i say 'console' i mean monitor, btw. How do i set up NSS to make sure it is seeing pin 8? (should have been 8 to begin with, i have fixed that since.) what would the verbiage be? If i type in #define rxPin 8, will the arduino know enough to associate that with NSS?
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8530
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do i set up NSS to make sure it is seeing pin 8?
Have a look at the NSS page

http://arduiniana.org/libraries/NewSoftSerial/

There must be some documentation there. I've never used NSS but in the constructor

NewSoftSerial mySerial(8,9);

Presumably the 8 and 9 are the Tx and Rx pins (or vice versa).

Quote
but NSS doesnt pick up anything i send out through the arduino serial monitor.
Of course, it's not connected to the PC unless you're running a wire into your laptop smiley, it's connected to the router.

Quote
nor does it see anything i echo from the router.
You should see something from that if it's transmitting even if the baud rate is wrong (BTW you had 4800 in the original post).

How do you know the router is transmitting anything?

Quote
If i type in #define rxPin 8, will the arduino know enough to associate that with NSS?
Nope, that line at present is doing nothing at all, just defining a more human-readable bit of text. For example

NewSoftSerial mySerial(rxPin,9);

is easier to read than

NewSoftSerial mySerial(8,9);

______
Rob
 

Logged

Rob Gray aka the GRAYnomad www.robgray.com

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


Quote
Of course, it's not connected to the PC unless you're running a wire into your laptop , it's connected to the router.

But since NSS is handling the receiving end of things, shouldnt it println whatever comes in to it as well as flicker the light? relaying the info taken from the router to the arduino and then printing it out through the hard tx port into the computer? Or is that wrong?

Quote
You should see something from that if it's transmitting even if the baud rate is wrong (BTW you had 4800 in the original post).

How do you know the router is transmitting anything?

I am running:

echo "test' > /dev/tts/1 through the router.

yesterday, i opened up two SSH sessions to the router and ran cat < /dev/tts/1 in one and the echo in the other and it echo'd out to itself through the tx pin and into the rx pin just fine. Its the only test i've been able to find, hence the posting here looking for help.

I should note that i have tried this on 2 different routers with the exact same effect, so it's not my wiring.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8530
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
flicker the light?
What light?

Quote
shouldnt it println whatever comes in
AFIAK the NSS Tx pin is not physically connected to your PC or indeed anything else, how can it transmit to it?

Quote
relaying the info taken from the router to the arduino and then printing it out through the hard tx port into the computer? Or is that wrong?
No that's right, but isn't that what the code is doing? Reading from NSS and writing to Serial.

Quote
and the echo in the other and it echo'd out to itself through the tx pin and into the rx pin just fine.
So you did a physical loopback from a TX pin to an Rx pin, you actually connected an external wire. Is that the case?

If so (and assuming that today the same test still works) then it's reasonable to say there are characters emanating from the router. I'd still like to see it on an instrument though.

Did you figure out the pin 8/9 business. Are you sure you've got the right pin connected?

______
Rob 
Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

Update.

I am sitting here watching the most beautiful thing ever. a flickering green led!. I got it to work with the following code. I think it all came down to having sending on one com port and receiving on another. once i set NSS up to read and standard serial to transmit, it seems to be working... the led part at least. I am going to go through my code now and change things out so that everything reads on NSS and transmits on hardware serial. I'll let you all know how it goes.

Code:
#include <NewSoftSerial.h>
#define LEDpin 13
#define txPin 9

NewSoftSerial mySerial(8,9);
void setup () {

Serial.begin(115200);
mySerial.begin(9600);

pinMode(LEDpin, OUTPUT);

}

void loop () {
static byte c;

if (mySerial.available() > 0) {
c = mySerial.read();
Serial.println (c, HEX);
digitalWrite (LEDpin, HIGH);
delay (100);
digitalWrite (LEDpin, LOW);
delay (100);
}

}

Logged

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

YES!

Serial communication is a go!

Now on to the actual problem. I modified my code with elements from that test sketch so that i can be sure that data is coming through and when i run the carserver client, my light flickers constantly, almost in time with the light on my router. I set this up to println back to the txpin when i have debug turned on and then i used cat < /dev/tts/1 to listen to what was coming across. So basically, the way i see it is that the router is pumping something out, the arduino looks at it and turns around and sends it right back. That data i can see raw in my terminal window... here's the thing. It's just putting out a 0 and a carrige return.... now at some point i had vb6 application that controls the car working to an extent and it put out two 0's and any time i hit a key, it would put out a different number. I cant figure out what has changed. Can you run through my new code and see if i have it set up and reading incorrectly?

Code:
// include the SoftwareSerial library so you can use its functions:
#include <NewSoftSerial.h>
#include <ctype.h>

#define DEBUG 0

#define txPin 9
#define rxPin 8
#define LEDpin 13

NewSoftSerial mySerial(8,9);
int inByte;         // incoming serial byte

#define bit9600Delay 84
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94

int serialChar; // stores character of serial Input as integer
 
unsigned long decay; // counter from last command
unsigned long decaylimit=100000; // 100,000 is about 5 seconds

 
void setup() {
    // define pin modes for tx, rx, led pins:

  pinMode(LEDpin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  Serial.begin(9600);
  mySerial.begin(115200);
  Serial.println("Hello"); // For debugging purposes
 
 
  //left/right motors
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  //end debugging
}
 
int SWread()
{
  byte val = 0;
  while (digitalRead(rxPin));
  //wait for start bit
  if (digitalRead(rxPin) == LOW) {
    delayMicroseconds(halfBit9600Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rxPin) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay);
    delayMicroseconds(bit9600Delay);
    return val;
  }
}
void loop() {
  //check for serial communications
 {

  if (mySerial.available()) {
      serialChar = mySerial.read();
      mySerial.println((char)inByte);
                digitalWrite (LEDpin, HIGH);
delay (100);
digitalWrite (LEDpin, LOW);
delay (100);
   
    // decay counts the age of the last command
    // at the limit (100,000 = ~5 seconds, all signals are shut off
    if (decay > decaylimit) {
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      // stop counting at limit, shut off all pins
    }
    else {
      decay++;
    }
   
   {
 
 #if DEBUG     // echo for debugging only:
      Serial.println(serialChar);
 #endif
        // serialChar contains the ASCII code of the *first* character sent over serial when defined as int
        switch (serialChar) {
 
         // logical signals w s a d style     
         case '119':
           digitalWrite(4, LOW); 
           digitalWrite(7, LOW);
           digitalWrite(5, HIGH);
           digitalWrite(6, HIGH);           
            decay=0;           
            break;
         case '115':
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(4, HIGH);
            digitalWrite(7, HIGH);           
            decay=0;           
            break;
         case '97': 
            digitalWrite(5, HIGH);
            digitalWrite(6, LOW);
            digitalWrite(4, LOW);
            digitalWrite(7, LOW);           
            decay=0;           
            break;
         case '100':
            digitalWrite(4, LOW);
            digitalWrite(6, HIGH);
            digitalWrite(5, LOW);
            digitalWrite(7, LOW);           
            decay=0;           
            break;           
         case '113':
            digitalWrite(4, LOW);
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(7, LOW);
            decay = decaylimit;           

        }
      }
    }
  }
 }




Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
         case '119':
This is almost certainly wrong. Either lose the single quotes, or replace the number in the quotes with the correct letter (preferred).
Logged

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

No go. It doesnt move. I think the code is now set up correctly, but maybe the carserver is sending over characters other than wasd? or is the arduino looking for characters in one form and the carserver is sending them over in another?

Code:
// include the SoftwareSerial library so you can use its functions:
#include <NewSoftSerial.h>
#include <ctype.h>

#define DEBUG 0

#define txPin 9
#define rxPin 8
#define LEDpin 13

NewSoftSerial mySerial(8,9);
int inByte;         // incoming serial byte

#define bit9600Delay 84
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94

int serialChar; // stores character of serial Input as integer
 
unsigned long decay; // counter from last command
unsigned long decaylimit=100000; // 100,000 is about 5 seconds

 
void setup() {
    // define pin modes for tx, rx, led pins:

  pinMode(LEDpin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  Serial.begin(9600);
  mySerial.begin(115200);
  Serial.println("Hello"); // For debugging purposes
 
 
  //left/right motors
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  //end debugging
}
 
int SWread()
{
  byte val = 0;
  while (digitalRead(rxPin));
  //wait for start bit
  if (digitalRead(rxPin) == LOW) {
    delayMicroseconds(halfBit9600Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rxPin) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay);
    delayMicroseconds(bit9600Delay);
    return val;
  }
}
void loop() {
  //check for serial communications
 {

  if (mySerial.available()) {
      serialChar = mySerial.read();
      mySerial.println((char)inByte);
                digitalWrite (LEDpin, HIGH);
delay (100);
digitalWrite (LEDpin, LOW);
delay (100);
   
    // decay counts the age of the last command
    // at the limit (100,000 = ~5 seconds, all signals are shut off
    if (decay > decaylimit) {
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      // stop counting at limit, shut off all pins
    }
    else {
      decay++;
    }
   
   {
 
 #if DEBUG     // echo for debugging only:
      Serial.println(serialChar);
 #endif
        // serialChar contains the ASCII code of the *first* character sent over serial when defined as int
        switch (serialChar) {
 
         // logical signals w s a d style     
         case 'w': // 119
           digitalWrite(4, LOW); 
           digitalWrite(7, LOW);
           digitalWrite(5, HIGH);
           digitalWrite(6, HIGH);           
            decay=0;           
            break;
         case 's': // 115
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(4, HIGH);
            digitalWrite(7, HIGH);           
            decay=0;           
            break;
         case 'a': // 97 
            digitalWrite(5, HIGH);
            digitalWrite(6, LOW);
            digitalWrite(4, LOW);
            digitalWrite(7, LOW);           
            decay=0;           
            break;
         case 'd': // 100
            digitalWrite(4, LOW);
            digitalWrite(6, HIGH);
            digitalWrite(5, LOW);
            digitalWrite(7, LOW);           
            decay=0;           
            break;           
         case 'q': // 113
            digitalWrite(4, LOW);
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            digitalWrite(7, LOW);
            decay = decaylimit;           

        }
      }
    }
  }
 }




Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8530
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
maybe the carserver is sending over characters other than wasd?
You tell us, you have a debug print there. But change it to

Serial.println(serialChar, HEX);

in case you're getting non-printable values.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

Basically, something like this....

0
0
0
0
0
0
0

and so on. nothing else.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8530
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So serialChar is screwed at the point you print it, was it received correctly in the first place. Move you print() around to see where thing start going wrong.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8530
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you really using NSS at 115200? If so what are all the bit9600Delay type constants?

I thought you were using 9600 to the router.

______
Rob
« Last Edit: December 01, 2011, 01:35:49 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

What's a type constant? See how ignorant i am?

I was told by someone (dont know at this point) that the router runs at 115200....
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8530
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I was told by someone (dont know at this point) that the router runs at 115200
Hardly confidence boosting smiley You need to know that.

Quote
What's a type constant?
All these

Code:
#define bit9600Delay 84
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94

But I now see there aren't being used because SWread() never gets called.

No matter, you have to identify at what point serialChar is bad. As I said a couple of posts ago

Quote
Move you print() around to see where thing start going wrong.

I see you are echoing the character back to the router, does that work?

______
Rob

Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

echoing back to the router does work. It returns 0's as well.

should i remove swread entirely? as well as the type constants? Just to clean things up?

I will try playing with my print debug.
Logged

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