I'm testing a code a colleague had written. She used SoftwareSerial to define the serial ports i.e SoftwareSerial(2,3) for RX,TX respectively. The code defines the baud rate for the SoftwareSerial ports and the HardwareSerial ports separately(as shown below). But,for some reason,I am able to receive an output on the serial monitor only when I plug my TX of transmitter into the default Arduino serial pin 0 which is a RX port. This is hard to comprehend,as my colleague defined the digital pin 2 as the RX port in the code.
I get gibberish when I connect my TX of transmitter to pin 2 of Arduino.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);///rx,tx
int ledPin=13;
void setup()
{
// Open serial communications and wait for port to open:
pinMode(2, INPUT);
pinMode(3, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // sets the LED off
mySerial.begin(9600);//pins 0 and 1
Serial.begin(19200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Goodnight moon!");
}
void loop() // run over and over
{
// debug();
//return;
int i=0;
int a;//a is the identifier, b,c are used to calculate the parity
//Serial.flush();//wait for sent buffer to empty
//delay(2);
for(i=1;i<=1;i++)//ID for the multifunction steering wheel is 1
{
mySerial.end();
mySerial.begin(9600);
mySerial.write((byte)0x0);
//delay(1);//if Serial port is used, this dalay is needed
mySerial.end();//close serial
mySerial.begin(19200);
//sync field
mySerial.write(0x55);
//delay(1);//if Serial port is used, this dalay is needed
/////rest of the code
That is right, only the hardware aerial pins are connected to the monitor, you can not see the software serial port in the computer unless you fit a TTL to USB lead to those pins and attach that to the computer.
This is how I connected a chip with its own RX,TX to the Arduino.
Chip RX -> Arduino TX pin 3
Chip TX -> Arduino RX pin 2
...since I define pin 2 and 3 as RX,TX respectively using Software Serial.For such a connection,I get gibberish in my Serial Monitor.
Chip RX -> Arduino TX pin 3
Chip TX -> Arduino RX pin 0
Now,I get meaningful output on my Serial Monitor. This is despite the fact that I connected to pin 3. Why is that?
Hi. What speed are you running Serial Monitor at, when you change the connections and see good data? And is the good data the "goodnight moon" message?
It looks like there are two different speeds being used at different places in your program for the external device.
Your code is very mixed up.
- Only call serial begin in the setup function it needs to be done only once.
- The code you posted does not write to the hardware serial port so we can have no idea of why you see anything on it.
- Comments and code do not match. If the comment is at error then this is just confusing but it does illustrate a lack of care.
mySerial.begin(9600);//pins 0 and 1
Serial.begin(19200);
Should be:-
mySerial.begin(9600);
Serial.begin(19200);//pins 0 and 1
I have no idea why I see the output when I connect to RX 0 either. I think there's one instance where the colleague defines the SoftwareSerial port with the same baud rate as the HardwareSerial i.e 19200 baud rate,and this might have confused the ARDUINO??
mySerial.begin(19200); // This line right here redefines the baud rate to 19200,whereas it was initially defined at 9600 in the setup() function. I guess she does it to create some sort of a delay.
//sync field
mySerial.write(0x55);
//delay(1);//if Serial port is used, this dalay is needed
/////rest of the code
there's one instance where the colleague defines the SoftwareSerial port with the same baud rate as the HardwareSerial i.e 19200 baud rate,and this might have confused the ARDUINO?
No you can have both running at the same speed no problem.
But you should not make repeated calls to Serial.begin, it should be called once in the setup and not called again.
Another question.Does SoftwareSerial even work beyond a baud rate of 9600? My output seems okay with the above code which uses a baud 19200 for SoftwareSerial in one instance.
But,now when I am trying to pair a JY-MCU bluetooth,I keep wondering if SoftwareSerial can even be used beyond a baud rate of 9600.
marellasunny:
Another question.Does SoftwareSerial even work beyond a baud rate of 9600?
Yes:-
It is possible to have multiple software serial ports with speeds up to 115200 bps.
From:-
http://arduino.cc/en/Reference/SoftwareSerial
MAX232 TTL-RS232 chip. (see attached)
TTL to USB converter
TUTORIAL: (TTL to USB)
max232.pdf (988 KB)