Go Down

Topic: Arduino Uno Reading Tx/Rx from Car Head Unit. (Read 366 times) previous topic - next topic

samrishi_24

#15
Sep 23, 2017, 09:54 am Last Edit: Sep 23, 2017, 02:38 pm by samrishi_24
Hi I tried this code first and can not seen any thing in the monitor...


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

SoftwareSerial mySerial(0, 1); // RX, TX
const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(57600);
/*while (!Serial) {
  ; // wait for serial port to connect. Needed for native USB port only
}*/


Serial.println("<Arduino is ready>");

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);

}

void loop() { // run over and over
static byte ndx = 0;
  char endMarker = '\n';
  char rc;
 
  while (mySerial.available() && newData == false) {
    rc = mySerial.read();
    
    if (rc != endMarker) {
          receivedChars[ndx] = rc;
          ndx++;
          if (ndx >= numChars) {
              ndx = numChars - 1;
          }
      }
      else {
          receivedChars[ndx] = '\0'; // terminate the string
          ndx = 0;
          newData = true;
      }
  }
  showNewData();

/*if (mySerial.available()) {
  Serial.write(mySerial.read());
}*/  

}

void showNewData() {
  if (newData == true) {
      Serial.write("This just in ... ");
      Serial.write(receivedChars);
      Serial.write("......");
      newData = false;
  }
}


Then I have uncommented the following:
Code: [Select]
/*if (mySerial.available()) {
  Serial.write(mySerial.read());
}*/  


and then the code become:

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

SoftwareSerial mySerial(0, 1); // RX, TX
const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(57600);
/*while (!Serial) {
  ; // wait for serial port to connect. Needed for native USB port only
}*/


Serial.println("<Arduino is ready>");

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);

}

void loop() { // run over and over
static byte ndx = 0;
  char endMarker = '\n';
  char rc;
 
  while (mySerial.available() && newData == false) {
    rc = mySerial.read();
    
    if (rc != endMarker) {
          receivedChars[ndx] = rc;
          ndx++;
          if (ndx >= numChars) {
              ndx = numChars - 1;
          }
      }
      else {
          receivedChars[ndx] = '\0'; // terminate the string
          ndx = 0;
          newData = true;
      }
  }
  showNewData();

if (mySerial.available()) {
  Serial.write(mySerial.read());
}  

}

void showNewData() {
  if (newData == true) {
      Serial.write("This just in ... ");
      Serial.write(receivedChars);
      Serial.write("......");
      newData = false;
  }
}



Then I can see the following output:
Image 1: when i dont press any button...
Image 2: when i press many buttons on my car...

Robin2

I just realized that I was wrong in what I said in Reply #14 "Now that you know how to use SoftwareSerial ".  Obviously you have not yet digested the advice from Reply #9  SoftwareSerial MUST NOT BE on pins 0 and 1. They are reserved for HardwareSerial for communication with the PC. When I wrote Reply #14 I had assumed (based on Reply #11) that you were using pins 10 and 11 for SoftwareSerial so I did not check that part of your program.

Fix that and try again, and report back.

...R

Please modify your post and use the code button </>
Code: [Select]
so your code looks like this and is easy to copy to a text editor. See How to use the Forum
Two or three hours spent thinking and reading documentation solves most programming problems.

samrishi_24

Yes I have tried with both.... 0,1 and 10,11.. so whatever I post here I try with both 0,1 and 10,11....

Robin2

Yes I have tried with both.... 0,1 and 10,11.. so whatever I post here I try with both 0,1 and 10,11....
The version using 0 and 1 for SoftwareSerial is WRONG. I am not going to waste time studying a wrong version. Post the correct version.

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

samrishi_24

#19
Sep 23, 2017, 02:39 pm Last Edit: Sep 23, 2017, 02:44 pm by samrishi_24
Now please tell me....

I tried this code first and can not seen any thing in the monitor...


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

SoftwareSerial mySerial(10, 11); // RX, TX
const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(57600);
/*while (!Serial) {
  ; // wait for serial port to connect. Needed for native USB port only
}*/


Serial.println("<Arduino is ready>");

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);

}

void loop() { // run over and over
static byte ndx = 0;
  char endMarker = '\n';
  char rc;
 
  while (mySerial.available() && newData == false) {
    rc = mySerial.read();
    
    if (rc != endMarker) {
          receivedChars[ndx] = rc;
          ndx++;
          if (ndx >= numChars) {
              ndx = numChars - 1;
          }
      }
      else {
          receivedChars[ndx] = '\0'; // terminate the string
          ndx = 0;
          newData = true;
      }
  }
  showNewData();

/*if (mySerial.available()) {
  Serial.write(mySerial.read());
}*/  

}

void showNewData() {
  if (newData == true) {
      Serial.write("This just in ... ");
      Serial.write(receivedChars);
      Serial.write("......");
      newData = false;
  }
}


Then I have uncommented the following:
Code: [Select]
/*if (mySerial.available()) {
  Serial.write(mySerial.read());
}*/  


and then the code become:

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

SoftwareSerial mySerial(10, 11); // RX, TX
const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(57600);
/*while (!Serial) {
  ; // wait for serial port to connect. Needed for native USB port only
}*/


Serial.println("<Arduino is ready>");

// set the data rate for the SoftwareSerial port
mySerial.begin(9600);

}

void loop() { // run over and over
static byte ndx = 0;
  char endMarker = '\n';
  char rc;
 
  while (mySerial.available() && newData == false) {
    rc = mySerial.read();
    
    if (rc != endMarker) {
          receivedChars[ndx] = rc;
          ndx++;
          if (ndx >= numChars) {
              ndx = numChars - 1;
          }
      }
      else {
          receivedChars[ndx] = '\0'; // terminate the string
          ndx = 0;
          newData = true;
      }
  }
  showNewData();

if (mySerial.available()) {
  Serial.write(mySerial.read());
}  

}

void showNewData() {
  if (newData == true) {
      Serial.write("This just in ... ");
      Serial.write(receivedChars);
      Serial.write("......");
      newData = false;
  }
}



Then I can see the following output:
Image 1: when i dont press any button...
Image 2: when i press many buttons on my car...

Robin2

Image from Reply #19 so we don't have to download it. See this  Image Guide





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

Robin2

#21
Sep 23, 2017, 05:12 pm Last Edit: Sep 23, 2017, 05:14 pm by Robin2
Please don't post pictures of text. Just copy and paste the text.

The first version of your code (like this)
Code: [Select]
/*if (mySerial.available()) {
  Serial.write(mySerial.read());
}*/  

is the more correct version.

But why aren't you using the example from Serial Input Basics the way I wrote it? Why have you moved the code out of the function recvWithEndMarker() and into loop()?

...R



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

samrishi_24

Okay I tried this... but no output...

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


SoftwareSerial mySerial(10, 11); // RX, TX


const byte numChars = 32;

char receivedChars[numChars];   // an array to store the received data


boolean newData = false;


void setup() {

    Serial.begin(57600);

    Serial.println("<Arduino is ready>");

    

    // set the data rate for the SoftwareSerial port

    mySerial.begin(9600);

}


void loop() {

    recvWithEndMarker();

    showNewData();

}


void recvWithEndMarker() {

    static byte ndx = 0;

    char endMarker = '\n';

    char rc;

    

    while (mySerial.available() > 0 && newData == false) {

        rc = mySerial.read();


        if (rc != endMarker) {

            receivedChars[ndx] = rc;

            ndx++;

            if (ndx >= numChars) {

                ndx = numChars - 1;

            }

        }

        else {

            receivedChars[ndx] = '\0'; // terminate the string

            ndx = 0;

            newData = true;

        }

        //flush(rc);

    }

}


void showNewData() {

    if (newData == true) {

        Serial.print("This just in ... ");

        Serial.println(receivedChars);

        newData = false;
}
    }

Robin2

#23
Today at 09:07 am Last Edit: Today at 09:08 am by Robin2
Okay I tried this... but no output...
It would be a huge help if you would write more than the absolute minimum and tell what are your thoughts about the problem.


Anyway, now we seem finally to have reached the stage where we have a known program and a basis for testing, so let's move on from there.


Do you KNOW that 9600 baud is the correct rate for your stereo AND that the stereo always sends a LineFeed at the end of its messages?

Is it necessary to send a message to the stereo before it will send a message back?

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

samrishi_24

#24
Today at 09:16 am Last Edit: Today at 09:25 am by samrishi_24
Then I replaced the endMarker to '\0' then got some output when idle and when pressed any button then a lot of same garbage output...



No, I dont know the exact baud rate of the control panel...
But I tried serial.begin with all the baud rates from 2400 to 115200...

No, its not required to send the message to the control panel/stereo.. I just want to receive the values from that control panel... so that I can validate that which button is pressed... thats all I want....

Robin2

#25
Today at 09:48 am Last Edit: Today at 09:49 am by Robin2
Then I replaced the endMarker to '\0' then got some output when idle .
I am not going to waste my time trying to help if you keep fiddling with the code. It took from Reply #1 to Reply #22 to get it right.

Also, I asked you NOT to post pictures of text.


On top of all that, you don't seem to be thinking logically about the problem at all ...

You say "I tried serial.begin with all the baud rates from 2400 to 115200..."   But how could changes to the baud rate for Serial have anything to do with it when Serial is not connected to the the stereo?

On the other hand you do seem to be receiving some characters which suggests that you have the correct baud rate - at least on that occasion. What baud rate was SoftwareSerial using when you received those characters?

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

samrishi_24

I am sorry, I am increasing your frustration level... I am completely unaware of all these things... I just gone through the internet to read the buttons of control panel via Aurdino to use it in my own car.

If am wasting your time... then i am sorry for that i will not post any thing from now..

but from the above post of mine..
i meant to say that mySerial.Begin i have tried with all....
but when i get the proper input is when these baud rates are set...

Serial.Begin(57600)
mySerial.Begin(9600)

Then i can see all these inputs in the monitor at 57600..

Yes you are right i am receiving the characters and when i press button i see a lot of characters... that means data is coming from the control panel... but the thing is they are looking like garbage for me to read the correct button....

So, I have two confusions...

1. Data is coming continuously even i dont press any button (in slow speed)..
2. When i press a button lot of data displays but in the garbage form as i have shown you in the images...

Go Up