I have a problem which I think is very wiers please help me out on this one.
the program I want to write is very simple, but somehow I cant get my head around.
here is my code:
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,10,11,12,13);
char test='0';
char test2='3';
SoftwareSerial mySerial(3, 2); // RX, TX
void setup() {
lcd.begin(16,2);
lcd.print("Hello World!");
delay(1000);
lcd.clear();
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
test='0';
}
if (Serial.available()) {
mySerial.write(Serial.read());
test2=mySerial.read();
test='1';
}
delay(1000);
lcd.print(test);
delay(1000);
lcd.print(test2);
delay(1000);
lcd.clear();
}
The result is I get "0" and "3" at first which is what i want
then
when i enter anything from serial monitor
it becomes "1" which is expected however for the second character I expect what i wrote on Serial Monitor. But thats not the case instead I get full square on my lcd, and if I make it write on hardware Serial i get "ÿ"
Please help because i can not figure out why am I not getting correct characters.
if (Serial.available()) { //<<<<<<<<<<<<<<<<< using Serial
mySerial.write(Serial.read());
test2=mySerial.read(); //<< using MySerial but nothing is likely to be there
test='1';
}
if (Serial.available()) { //<<<<<<<<<<<<<<<<< using Serial
mySerial.write(Serial.read());
test2=mySerial.read(); //<< using MySerial but nothing is likely to be there
test='1';
}
I am trying to write on mySerial and read it at the same time. Rx and Tx pins are connected so there will be direct data transfer right?
Robin2
Thank you it is extremely helpful I read it all, it gives very useful info about Serial port however, there is nothing about software serial.
I made the code very simple this time to try and realised something
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5,6);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial){
;
}
mySerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available()>0){
mySerial.write(Serial.read());
Serial.write(mySerial.read());
}
}
and what ever I write to the serial monitor I get "ÿ" this but its interesting that the number of characters I wrote matches with the number of "ÿ" I get. Do you have any suggestions? I still cant figure this out.
you should indent your code (hit ctrl-T in the IDE) that will make it more readable.
You need to understand that Serial is not working by magic synchronously. When you ask mySerial to write something you are not guaranteed it's instantly available on the other side. actually at 9600 bauds, it will take roughly 1ms for the character to get there... in 1ms your arduino does A LOT!
Ok I think I could not expain myself clear enough.
First of all J-M-L it has a practical use actually, first of all I tried to use an LCD to monitor the data recieved from Software Serial, i only used the serial to give command. Then ı made the code simpler so that I can explain myself further. The thing I am trying to do is to read the data coming from Software Serial. Then I'll connect an RS232 device though a shield to my arduino but that a hole different story. But the main problem is I can not read Software serial data, the onlything I get instead is "ÿ" this.
My simple logic is that if it can not read the data which software Serial itself sent it also wont be able to read the data recieved form another device too.
last Robin2 I sent you every piece of code which is giving me the headaches please dont think that I am not sending the code. I think, it is very simple thats why you think like this. I am just trying to read the Serial data which SoftwareSerial send to itself.
Manifoldwarper:
last Robin2 I sent you every piece of code which is giving me the headaches please dont think that I am not sending the code. I think, it is very simple thats why you think like this. I am just trying to read the Serial data which SoftwareSerial send to itself.
You have not posted any code since your Reply #8 in which you said that you could not get my system to work with SoftwareSerial. And I have not seen my examples included in the code in any of your earlier posts.
I understand what you say, but there is probably a challenge in the way Software Serial is handling this if you have the same instance writing and reading.
I took that small piece of code and wired the same way as you
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5, 6);
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
int v = Serial.read();
Serial.write((byte) v); // local echo
mySerial.write((byte) v); // send to the other Arduino
}
if (mySerial.available() > 0) {
Serial.write(mySerial.read()); // Echo what we got
}
}
and got the same issue as you highlight.
Now I took two Arduino UNO, joined the grounds and wired from 1st arduino to the second one: 5 <--> 6 and 6 <--> 5 for having the Software Serial Tx on Rx and vice-versa and I uploaded the very same code to both Arduino.
As the IDE cannot open two Serial Console on two different port, I opened 2 Serial Terminal windows and connected one to Arduino #1 and the other to Arduino #2 (note that I Set the hardware Serial to 115200 bauds, so that's what my Terminal is set to while I'm using Software Serial at a slower baud rate, 9600, between the 2 arduinos)
Now whatever I type in the first terminal appears on the second one and whatever I type on the second one appears in the first one (I type "HELLO" and return on the one on the left, and "ALL IS FINE" on the one on the right and you can see the text on both sides)
So I'd be tempted to say that Software Serial is getting confused somehow in the first case when the communication is local whereas everything works fine across two different CPUs... So if you had a real Serial device working at 9600 Bauds attached to 5 and 6 (watch out which one is Tx and which one is Rx, when you do SoftwareSerial(rxPin, txPin) in your code rxPin is the pin on which to receive serial data so it's the one connected to the Tx of the other device and txPin is the pin on which to transmit serial data so it's connected to the Rx of the Serial device), there should be no problem.
Hi everyone again I coulndt write because i was busy trying this. but as J-M-L said it worked with the device. I can easily recieve and transmit data. Aparently arduino has no ability to communicate with its own Software Serial, it acts as if its not common ground.
Thank you for your interest in my little experiment. Especially J-M-L and Robin2
would need to delve in the SoftwareSerial code to understand exactly what's happening. I suspect that this would work fine with Hardware Serial if you have a Mega you can try it out
Manifoldwarper:
I can easily recieve and transmit data. Aparently arduino has no ability to communicate with its own Software Serial,
Which is stated clearly in the documentation:
If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library. AltSoftSerial overcomes a number of other issues with the core SoftwareSerial, but has it's own limitations. Refer to the AltSoftSerial site for more information.
Whandall - I thought that was in the context of "If using multiple software serial ports" (because only one can receive data at a time hence the relevance with "simultaneous data flows")
I maintain this is not a bad issue though, there is no reason on earth I can see why you would send data you have to yourself as you already have the data in the first place anyway... So to me the use case is moot...