I uploaded regular pass through code for Serial and SoftwareSerial to communicate with my AT command based module.
Writing to SoftwareSerial seems to be working but reading not. As I write "AT", the module returns me "-5" which should be "OK".
As I write "asdad" the module returns something similar ot "invalid syntax" but very messy.
#include <SoftwareSerial.h>
SoftwareSerial ss(rx, tx);
void setup() {
Serial.begin(9600);
ss.begin(9600);
}
void loop() {
while (Serial.available()) ss.write(Serial.read());
while (ss.available()) Serial.write(ss.read());
}
2 Answers 2
With SoftwareSerial 9600 baud is about the maximum rate you can reliably run at when using a 5V 16MHz Arduino. You are using an 8MHz one at 3.3V, so you can expect that speed cap to be halved.
Don't use SoftwareSerial. Ever. Full stop. There is no call to ever use it. If you don't have enough real UART ports on your board then you are using the wrong board. Don't just use a board because it's the one that everyone uses. Look at what you want to do and then pick a suitable board that fits your needs. There are many many 3.3V boards available with multiple (or even one single spare) UARTs - for example the Fubarino Mini, the Teensy 3.x, Any number of ESP32 based boards - all with a similar footprint to the Pro Mini, and with a huge amount more power to boot.
-
Thank you. Can you recommend me some board like pro mini 3v3 but with more hw serial ports including sleep with external interrupt wake up and timed wake up.A. L. K.– A. L. K.2019年07月06日 17:10:18 +00:00Commented Jul 6, 2019 at 17:10
-
Teensy boards are probably the best supported.Majenko– Majenko2019年07月06日 17:29:52 +00:00Commented Jul 6, 2019 at 17:29
You may get better results if you do not nest the read and write function calls as you did. That is, read everything then write everything.
The reason for this is that softwareserial.read() returns 1 character. If you immediately turn around and pass that character to softwareserial.write() you will devote the resources of the processor to sending that character. In the meantime if another character is sent to the processor the processor will not see it or will only see part of it.
That said, you will likely get better results if you use a better embedded processor with dedicated serial port hardware for each port your project requires. Such as the Mega 2560 processor on the Arduino Mega.
Moving away from a cheap processor to one with enough hardware is a valid solution. Because, with another hardware serial port, the processor is only needed to read & write a character to & from the hardware using one command for each of these operations, not waste time reading & waiting each bit of a charater. A hardware serial port or UART can cut the effort of receiving & sending a character by the processor by more than an order of magnitude.
-
with AT firmware you send a command and then you get a response. so
while
is ok. the rest of the Answer is not related to the Question2019年07月06日 05:11:06 +00:00Commented Jul 6, 2019 at 5:11 -
@Juraj, I have improved my answer by explainning why a serial port implemented in software finds it difficult to run fast. I also explained why a HW UART is better for faster communications.st2000– st20002019年07月06日 11:54:00 +00:00Commented Jul 6, 2019 at 11:54
Explore related questions
See similar questions with these tags.
tx
andrx
?