0

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());
}
3
  • the sketch is OK. something else is wrong. do you have 9600 baud set in Serial Monitor? and both line ends? is the wiring good?
    – Juraj
    Jul 6, 2019 at 5:09
  • What pins are you using for tx and rx? Jul 6, 2019 at 10:15
  • @A. L. K., I think we are all making assumptions here. Please clear some things up by answering if it is the Arduino which is sending the "AT" or if it is the Arduino which is receiving the "AT" and sending back incorrect characters.
    – st2000
    Jul 6, 2019 at 12:02

2 Answers 2

0

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.

2
  • 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.
    Jul 6, 2019 at 17:10
  • Teensy boards are probably the best supported.
    – Majenko
    Jul 6, 2019 at 17:29
0

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.

2
  • 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 Question
    – Juraj
    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
    Jul 6, 2019 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.