What is the best delay to use between characters sent to the serial portReading multiple bytes from *software* serialMultiple SoftwareSerial ports - how to handle the limitationsWhat is the relationship between the gain on the receiver and the delay timeSoftserial - max speed - loosing charactersAltering SoftwareSerial to transmit more than 1 byte at a timewhy are there odd spikes in the USB serial data transfer rates?Serial Comm. timing issue between Arduino and Pyserialmyserial.available() returns zero byteSIM800L not registering to networkWhy is Serial.Write that slower when writing x+1 characters than when it is writing x characters?
How is it possible for tall trees to pull water to heights more than 10m?
Do electrons really perform instantaneous quantum leaps?
How do I tell my girlfriend she's been buying me books by the wrong author for the last nine months?
A* pathfinding algorithm too slow
Why doesn't SpaceX land boosters in Africa?
Is it possible to alias a column based on the result of a select+where?
English idiomatic equivalents of 能骗就骗 (if you can cheat, then cheat)
ATMEGA328P-U vs ATMEGA328-PU
What verb goes with "coup"?
Is there a word for the act of simultaneously pulling and twisting an object?
Why are symbols not written in words?
Why was Pan Am Flight 103 flying over Lockerbie?
What does that Pokemon Trainer mean by saying I am a SHELLOS?
Find the closest three-digit hex colour
How to track mail undetectably?
Angle Between Two Vectors Facing A Point
Robots in a spaceship
How do I present a future free of gender stereotypes without being jarring or overpowering the narrative?
Are you required to spend hit dice to take a short rest?
What's the lunar calendar of two moons
Rear derailleur got caught in the spokes, what could be a root cause
Simplify the code
Why should I allow multiple IP addresses on a website for a single session?
Having to constantly redo everything because I don't know how to do it
What is the best delay to use between characters sent to the serial port
Reading multiple bytes from *software* serialMultiple SoftwareSerial ports - how to handle the limitationsWhat is the relationship between the gain on the receiver and the delay timeSoftserial - max speed - loosing charactersAltering SoftwareSerial to transmit more than 1 byte at a timewhy are there odd spikes in the USB serial data transfer rates?Serial Comm. timing issue between Arduino and Pyserialmyserial.available() returns zero byteSIM800L not registering to networkWhy is Serial.Write that slower when writing x+1 characters than when it is writing x characters?
I have an Arduino Uno. It's role is to echo back what it receives.
But if I send it a stream of ascii characters it starts to error after the first one. Occasionally it gets the second character correct but often not.
If I delay each character then it works ok. I couldn't see this limitation mentioned in the SoftwareSerial library: https://www.arduino.cc/en/Reference/SoftwareSerial
The Arduino is running the following code:
#define BT_RX_PIN 16 // A2
#define BT_TX_PIN 17 // A3
#include <SoftwareSerial.h>
SoftwareSerial OtherSerial(BT_RX_PIN, BT_TX_PIN);
void setup()
Serial.begin(9600); // not used
OtherSerial.begin(9600);
void loop()
if (OtherSerial.available())
char value = OtherSerial.read();
String stri = "Received: ";
stri += value;
OtherSerial.println(stri);
The limitations do say "If using multiple software serial ports, only one can receive data at a time.". I don't have multiple software serial ports though I do have an instance of the "normal" serial port but I'm not sending any data to it... but perhaps even just initialising it is enough to disrupt seamless communication with the SoftwareSerial port?
Using the following test code for serial communication from a Raspberry Pi (using logic level conversion to go from 5V -> 3.3V and visa versa):
import time
def write(msg, delay):
for char in msg:
with open("/dev/ttyAMA0", "w") as f:
f.write(char)
time.sleep(delay)
write("hello", 0.01) # repeated 10 times at each delay time
I got the following results:
Delay (ms) | Success rate
10 | 100 %
9 | 100 %
8 | 0 %
5 | 0 %
1 | 0 %
A delay of 9 ms between each character means the communication rate falls from ~ 960 characters per second (9600 baud / 10 bits per 7 bit character) to ~ 100 characters per second (1 / (1 / 960 characters per second + 9ms)). Is this right? Perhaps I'm doing something else wrong / set it up wrong in software / hardware?
serial softwareserial
|
show 4 more comments
I have an Arduino Uno. It's role is to echo back what it receives.
But if I send it a stream of ascii characters it starts to error after the first one. Occasionally it gets the second character correct but often not.
If I delay each character then it works ok. I couldn't see this limitation mentioned in the SoftwareSerial library: https://www.arduino.cc/en/Reference/SoftwareSerial
The Arduino is running the following code:
#define BT_RX_PIN 16 // A2
#define BT_TX_PIN 17 // A3
#include <SoftwareSerial.h>
SoftwareSerial OtherSerial(BT_RX_PIN, BT_TX_PIN);
void setup()
Serial.begin(9600); // not used
OtherSerial.begin(9600);
void loop()
if (OtherSerial.available())
char value = OtherSerial.read();
String stri = "Received: ";
stri += value;
OtherSerial.println(stri);
The limitations do say "If using multiple software serial ports, only one can receive data at a time.". I don't have multiple software serial ports though I do have an instance of the "normal" serial port but I'm not sending any data to it... but perhaps even just initialising it is enough to disrupt seamless communication with the SoftwareSerial port?
Using the following test code for serial communication from a Raspberry Pi (using logic level conversion to go from 5V -> 3.3V and visa versa):
import time
def write(msg, delay):
for char in msg:
with open("/dev/ttyAMA0", "w") as f:
f.write(char)
time.sleep(delay)
write("hello", 0.01) # repeated 10 times at each delay time
I got the following results:
Delay (ms) | Success rate
10 | 100 %
9 | 100 %
8 | 0 %
5 | 0 %
1 | 0 %
A delay of 9 ms between each character means the communication rate falls from ~ 960 characters per second (9600 baud / 10 bits per 7 bit character) to ~ 100 characters per second (1 / (1 / 960 characters per second + 9ms)). Is this right? Perhaps I'm doing something else wrong / set it up wrong in software / hardware?
serial softwareserial
Really, there is no "best delay" as it appears the delay needed depends on what the RPi sends the Arduino. The more the RPi sends, the longer the delay in order for the Arduino to not let any data go missing.
– st2000
Jun 23 at 0:49
Are you opening the serial port for every single character? Why? Should you not put thewith
line before thefor
line?
– Nick Gammon♦
Jun 23 at 4:39
@NickGammon it was a quick hack to flush the characters to the file.
– AJP
Jun 23 at 9:38
1
Fairly obviously, you will get much better throughput if you don't send the word "Received:" for each character you receive. That's like putting 13 litres of water into a bucket every second and taking 1 litre out, and then wondering why it overflows.
– Nick Gammon♦
Jun 23 at 10:54
1
The documentation should indeed make it clear that SoftwareSerial cannot receive at the same time as it is transmitting, so it is not a good choice for attempting to do that.
– Nick Gammon♦
Jun 23 at 21:47
|
show 4 more comments
I have an Arduino Uno. It's role is to echo back what it receives.
But if I send it a stream of ascii characters it starts to error after the first one. Occasionally it gets the second character correct but often not.
If I delay each character then it works ok. I couldn't see this limitation mentioned in the SoftwareSerial library: https://www.arduino.cc/en/Reference/SoftwareSerial
The Arduino is running the following code:
#define BT_RX_PIN 16 // A2
#define BT_TX_PIN 17 // A3
#include <SoftwareSerial.h>
SoftwareSerial OtherSerial(BT_RX_PIN, BT_TX_PIN);
void setup()
Serial.begin(9600); // not used
OtherSerial.begin(9600);
void loop()
if (OtherSerial.available())
char value = OtherSerial.read();
String stri = "Received: ";
stri += value;
OtherSerial.println(stri);
The limitations do say "If using multiple software serial ports, only one can receive data at a time.". I don't have multiple software serial ports though I do have an instance of the "normal" serial port but I'm not sending any data to it... but perhaps even just initialising it is enough to disrupt seamless communication with the SoftwareSerial port?
Using the following test code for serial communication from a Raspberry Pi (using logic level conversion to go from 5V -> 3.3V and visa versa):
import time
def write(msg, delay):
for char in msg:
with open("/dev/ttyAMA0", "w") as f:
f.write(char)
time.sleep(delay)
write("hello", 0.01) # repeated 10 times at each delay time
I got the following results:
Delay (ms) | Success rate
10 | 100 %
9 | 100 %
8 | 0 %
5 | 0 %
1 | 0 %
A delay of 9 ms between each character means the communication rate falls from ~ 960 characters per second (9600 baud / 10 bits per 7 bit character) to ~ 100 characters per second (1 / (1 / 960 characters per second + 9ms)). Is this right? Perhaps I'm doing something else wrong / set it up wrong in software / hardware?
serial softwareserial
I have an Arduino Uno. It's role is to echo back what it receives.
But if I send it a stream of ascii characters it starts to error after the first one. Occasionally it gets the second character correct but often not.
If I delay each character then it works ok. I couldn't see this limitation mentioned in the SoftwareSerial library: https://www.arduino.cc/en/Reference/SoftwareSerial
The Arduino is running the following code:
#define BT_RX_PIN 16 // A2
#define BT_TX_PIN 17 // A3
#include <SoftwareSerial.h>
SoftwareSerial OtherSerial(BT_RX_PIN, BT_TX_PIN);
void setup()
Serial.begin(9600); // not used
OtherSerial.begin(9600);
void loop()
if (OtherSerial.available())
char value = OtherSerial.read();
String stri = "Received: ";
stri += value;
OtherSerial.println(stri);
The limitations do say "If using multiple software serial ports, only one can receive data at a time.". I don't have multiple software serial ports though I do have an instance of the "normal" serial port but I'm not sending any data to it... but perhaps even just initialising it is enough to disrupt seamless communication with the SoftwareSerial port?
Using the following test code for serial communication from a Raspberry Pi (using logic level conversion to go from 5V -> 3.3V and visa versa):
import time
def write(msg, delay):
for char in msg:
with open("/dev/ttyAMA0", "w") as f:
f.write(char)
time.sleep(delay)
write("hello", 0.01) # repeated 10 times at each delay time
I got the following results:
Delay (ms) | Success rate
10 | 100 %
9 | 100 %
8 | 0 %
5 | 0 %
1 | 0 %
A delay of 9 ms between each character means the communication rate falls from ~ 960 characters per second (9600 baud / 10 bits per 7 bit character) to ~ 100 characters per second (1 / (1 / 960 characters per second + 9ms)). Is this right? Perhaps I'm doing something else wrong / set it up wrong in software / hardware?
serial softwareserial
serial softwareserial
edited Jun 23 at 1:28
VE7JRO♦
1,8365 gold badges14 silver badges24 bronze badges
1,8365 gold badges14 silver badges24 bronze badges
asked Jun 22 at 23:59
AJPAJP
1256 bronze badges
1256 bronze badges
Really, there is no "best delay" as it appears the delay needed depends on what the RPi sends the Arduino. The more the RPi sends, the longer the delay in order for the Arduino to not let any data go missing.
– st2000
Jun 23 at 0:49
Are you opening the serial port for every single character? Why? Should you not put thewith
line before thefor
line?
– Nick Gammon♦
Jun 23 at 4:39
@NickGammon it was a quick hack to flush the characters to the file.
– AJP
Jun 23 at 9:38
1
Fairly obviously, you will get much better throughput if you don't send the word "Received:" for each character you receive. That's like putting 13 litres of water into a bucket every second and taking 1 litre out, and then wondering why it overflows.
– Nick Gammon♦
Jun 23 at 10:54
1
The documentation should indeed make it clear that SoftwareSerial cannot receive at the same time as it is transmitting, so it is not a good choice for attempting to do that.
– Nick Gammon♦
Jun 23 at 21:47
|
show 4 more comments
Really, there is no "best delay" as it appears the delay needed depends on what the RPi sends the Arduino. The more the RPi sends, the longer the delay in order for the Arduino to not let any data go missing.
– st2000
Jun 23 at 0:49
Are you opening the serial port for every single character? Why? Should you not put thewith
line before thefor
line?
– Nick Gammon♦
Jun 23 at 4:39
@NickGammon it was a quick hack to flush the characters to the file.
– AJP
Jun 23 at 9:38
1
Fairly obviously, you will get much better throughput if you don't send the word "Received:" for each character you receive. That's like putting 13 litres of water into a bucket every second and taking 1 litre out, and then wondering why it overflows.
– Nick Gammon♦
Jun 23 at 10:54
1
The documentation should indeed make it clear that SoftwareSerial cannot receive at the same time as it is transmitting, so it is not a good choice for attempting to do that.
– Nick Gammon♦
Jun 23 at 21:47
Really, there is no "best delay" as it appears the delay needed depends on what the RPi sends the Arduino. The more the RPi sends, the longer the delay in order for the Arduino to not let any data go missing.
– st2000
Jun 23 at 0:49
Really, there is no "best delay" as it appears the delay needed depends on what the RPi sends the Arduino. The more the RPi sends, the longer the delay in order for the Arduino to not let any data go missing.
– st2000
Jun 23 at 0:49
Are you opening the serial port for every single character? Why? Should you not put the
with
line before the for
line?– Nick Gammon♦
Jun 23 at 4:39
Are you opening the serial port for every single character? Why? Should you not put the
with
line before the for
line?– Nick Gammon♦
Jun 23 at 4:39
@NickGammon it was a quick hack to flush the characters to the file.
– AJP
Jun 23 at 9:38
@NickGammon it was a quick hack to flush the characters to the file.
– AJP
Jun 23 at 9:38
1
1
Fairly obviously, you will get much better throughput if you don't send the word "Received:" for each character you receive. That's like putting 13 litres of water into a bucket every second and taking 1 litre out, and then wondering why it overflows.
– Nick Gammon♦
Jun 23 at 10:54
Fairly obviously, you will get much better throughput if you don't send the word "Received:" for each character you receive. That's like putting 13 litres of water into a bucket every second and taking 1 litre out, and then wondering why it overflows.
– Nick Gammon♦
Jun 23 at 10:54
1
1
The documentation should indeed make it clear that SoftwareSerial cannot receive at the same time as it is transmitting, so it is not a good choice for attempting to do that.
– Nick Gammon♦
Jun 23 at 21:47
The documentation should indeed make it clear that SoftwareSerial cannot receive at the same time as it is transmitting, so it is not a good choice for attempting to do that.
– Nick Gammon♦
Jun 23 at 21:47
|
show 4 more comments
2 Answers
2
active
oldest
votes
Simply put, a serial port implemented in software requires the program to sample the input pin fast enough to detect every transition. To satisfy the Nyquist frequency rate we need the program to sample the data twice as fast as we expect the data to change.
A serial port implemented in hardware might only need to be sampled at 1/16 this rate assuming the data size is 8 bits.
Your program implements a serial port in software which is admirably keeping up with the received data. However, when you tell the program to send the message "Received" plus the string that was received, the program stops looking at the serial input pin long enough for data to go missing.
By far the easiest solution would be to use a hardware implemented serial port. If this is not possible consider approaching the problem using interrupts. However, while a powerful programming tool, interrupts can be difficult to implement and hard to debug.
add a comment |
SoftwareSerial has a considerable overhead. It can often send at 115200 successfully but 9600 is about its limit for receiving, and you're trying both send and receive. In addition, for each character your code receives, it transmits considerably more than that 1 character (11 characters, by my count). It isn't too surprising that it would fall behind. You will need to either reduce the size of what you echo or reduce the rate at which you send characters to it (which is what you accomplished with your delay) so that the transmission (echo) rate can keep up.
Update:
The OP reports success with continuous reception by, and transmission from, the Arduino at 57600 baud (but not at the same time). The transmission rate would be consistent with my experience transmitting at 115200 baud (blindly and for short bursts, to reconfigure ESP8266-01s that were shipped with a preset 115200 rate.
1
13 characters because of the carriage-return/newline. This is right, you can't expect to send 13 characters for every one you receive, and keep up. Plus, when SoftwareSerial sends it turns interrupts off, so it can't receive at the same time.
– Nick Gammon♦
Jun 23 at 4:31
@JRobert I've managed to successfully transmit continuously at 57600 baud (but with no transmit from the Arduino until after the whole message has been sent to it). Could you edit your answer to reflect this and I'll mark it as the "correct" answer. Thanks.
– AJP
Jun 23 at 12:02
Sure - is that 57600 transmission to- or from the Arduino? Not having the direct experience, I feel I should quote yours in my update.
– JRobert
Jun 23 at 16:31
Yes 57600 transmission is to and from. I tried with 115200 but (and perhaps it's due to my longer electrical wires / lack of proper grounding / interference) but this didn't work and the echo was garbage. This could have just been the transmit from the Arduino to the Raspberry Pi but regardless in this case it didn't work at 115200 but did at 57600.
– AJP
Jun 23 at 22:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("schematics", function ()
StackExchange.schematics.init();
);
, "cicuitlab");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "540"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f66518%2fwhat-is-the-best-delay-to-use-between-characters-sent-to-the-serial-port%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simply put, a serial port implemented in software requires the program to sample the input pin fast enough to detect every transition. To satisfy the Nyquist frequency rate we need the program to sample the data twice as fast as we expect the data to change.
A serial port implemented in hardware might only need to be sampled at 1/16 this rate assuming the data size is 8 bits.
Your program implements a serial port in software which is admirably keeping up with the received data. However, when you tell the program to send the message "Received" plus the string that was received, the program stops looking at the serial input pin long enough for data to go missing.
By far the easiest solution would be to use a hardware implemented serial port. If this is not possible consider approaching the problem using interrupts. However, while a powerful programming tool, interrupts can be difficult to implement and hard to debug.
add a comment |
Simply put, a serial port implemented in software requires the program to sample the input pin fast enough to detect every transition. To satisfy the Nyquist frequency rate we need the program to sample the data twice as fast as we expect the data to change.
A serial port implemented in hardware might only need to be sampled at 1/16 this rate assuming the data size is 8 bits.
Your program implements a serial port in software which is admirably keeping up with the received data. However, when you tell the program to send the message "Received" plus the string that was received, the program stops looking at the serial input pin long enough for data to go missing.
By far the easiest solution would be to use a hardware implemented serial port. If this is not possible consider approaching the problem using interrupts. However, while a powerful programming tool, interrupts can be difficult to implement and hard to debug.
add a comment |
Simply put, a serial port implemented in software requires the program to sample the input pin fast enough to detect every transition. To satisfy the Nyquist frequency rate we need the program to sample the data twice as fast as we expect the data to change.
A serial port implemented in hardware might only need to be sampled at 1/16 this rate assuming the data size is 8 bits.
Your program implements a serial port in software which is admirably keeping up with the received data. However, when you tell the program to send the message "Received" plus the string that was received, the program stops looking at the serial input pin long enough for data to go missing.
By far the easiest solution would be to use a hardware implemented serial port. If this is not possible consider approaching the problem using interrupts. However, while a powerful programming tool, interrupts can be difficult to implement and hard to debug.
Simply put, a serial port implemented in software requires the program to sample the input pin fast enough to detect every transition. To satisfy the Nyquist frequency rate we need the program to sample the data twice as fast as we expect the data to change.
A serial port implemented in hardware might only need to be sampled at 1/16 this rate assuming the data size is 8 bits.
Your program implements a serial port in software which is admirably keeping up with the received data. However, when you tell the program to send the message "Received" plus the string that was received, the program stops looking at the serial input pin long enough for data to go missing.
By far the easiest solution would be to use a hardware implemented serial port. If this is not possible consider approaching the problem using interrupts. However, while a powerful programming tool, interrupts can be difficult to implement and hard to debug.
answered Jun 23 at 0:44
st2000st2000
4,7952 gold badges6 silver badges16 bronze badges
4,7952 gold badges6 silver badges16 bronze badges
add a comment |
add a comment |
SoftwareSerial has a considerable overhead. It can often send at 115200 successfully but 9600 is about its limit for receiving, and you're trying both send and receive. In addition, for each character your code receives, it transmits considerably more than that 1 character (11 characters, by my count). It isn't too surprising that it would fall behind. You will need to either reduce the size of what you echo or reduce the rate at which you send characters to it (which is what you accomplished with your delay) so that the transmission (echo) rate can keep up.
Update:
The OP reports success with continuous reception by, and transmission from, the Arduino at 57600 baud (but not at the same time). The transmission rate would be consistent with my experience transmitting at 115200 baud (blindly and for short bursts, to reconfigure ESP8266-01s that were shipped with a preset 115200 rate.
1
13 characters because of the carriage-return/newline. This is right, you can't expect to send 13 characters for every one you receive, and keep up. Plus, when SoftwareSerial sends it turns interrupts off, so it can't receive at the same time.
– Nick Gammon♦
Jun 23 at 4:31
@JRobert I've managed to successfully transmit continuously at 57600 baud (but with no transmit from the Arduino until after the whole message has been sent to it). Could you edit your answer to reflect this and I'll mark it as the "correct" answer. Thanks.
– AJP
Jun 23 at 12:02
Sure - is that 57600 transmission to- or from the Arduino? Not having the direct experience, I feel I should quote yours in my update.
– JRobert
Jun 23 at 16:31
Yes 57600 transmission is to and from. I tried with 115200 but (and perhaps it's due to my longer electrical wires / lack of proper grounding / interference) but this didn't work and the echo was garbage. This could have just been the transmit from the Arduino to the Raspberry Pi but regardless in this case it didn't work at 115200 but did at 57600.
– AJP
Jun 23 at 22:17
add a comment |
SoftwareSerial has a considerable overhead. It can often send at 115200 successfully but 9600 is about its limit for receiving, and you're trying both send and receive. In addition, for each character your code receives, it transmits considerably more than that 1 character (11 characters, by my count). It isn't too surprising that it would fall behind. You will need to either reduce the size of what you echo or reduce the rate at which you send characters to it (which is what you accomplished with your delay) so that the transmission (echo) rate can keep up.
Update:
The OP reports success with continuous reception by, and transmission from, the Arduino at 57600 baud (but not at the same time). The transmission rate would be consistent with my experience transmitting at 115200 baud (blindly and for short bursts, to reconfigure ESP8266-01s that were shipped with a preset 115200 rate.
1
13 characters because of the carriage-return/newline. This is right, you can't expect to send 13 characters for every one you receive, and keep up. Plus, when SoftwareSerial sends it turns interrupts off, so it can't receive at the same time.
– Nick Gammon♦
Jun 23 at 4:31
@JRobert I've managed to successfully transmit continuously at 57600 baud (but with no transmit from the Arduino until after the whole message has been sent to it). Could you edit your answer to reflect this and I'll mark it as the "correct" answer. Thanks.
– AJP
Jun 23 at 12:02
Sure - is that 57600 transmission to- or from the Arduino? Not having the direct experience, I feel I should quote yours in my update.
– JRobert
Jun 23 at 16:31
Yes 57600 transmission is to and from. I tried with 115200 but (and perhaps it's due to my longer electrical wires / lack of proper grounding / interference) but this didn't work and the echo was garbage. This could have just been the transmit from the Arduino to the Raspberry Pi but regardless in this case it didn't work at 115200 but did at 57600.
– AJP
Jun 23 at 22:17
add a comment |
SoftwareSerial has a considerable overhead. It can often send at 115200 successfully but 9600 is about its limit for receiving, and you're trying both send and receive. In addition, for each character your code receives, it transmits considerably more than that 1 character (11 characters, by my count). It isn't too surprising that it would fall behind. You will need to either reduce the size of what you echo or reduce the rate at which you send characters to it (which is what you accomplished with your delay) so that the transmission (echo) rate can keep up.
Update:
The OP reports success with continuous reception by, and transmission from, the Arduino at 57600 baud (but not at the same time). The transmission rate would be consistent with my experience transmitting at 115200 baud (blindly and for short bursts, to reconfigure ESP8266-01s that were shipped with a preset 115200 rate.
SoftwareSerial has a considerable overhead. It can often send at 115200 successfully but 9600 is about its limit for receiving, and you're trying both send and receive. In addition, for each character your code receives, it transmits considerably more than that 1 character (11 characters, by my count). It isn't too surprising that it would fall behind. You will need to either reduce the size of what you echo or reduce the rate at which you send characters to it (which is what you accomplished with your delay) so that the transmission (echo) rate can keep up.
Update:
The OP reports success with continuous reception by, and transmission from, the Arduino at 57600 baud (but not at the same time). The transmission rate would be consistent with my experience transmitting at 115200 baud (blindly and for short bursts, to reconfigure ESP8266-01s that were shipped with a preset 115200 rate.
edited Jun 23 at 23:26
answered Jun 23 at 0:35
JRobertJRobert
10.5k2 gold badges14 silver badges39 bronze badges
10.5k2 gold badges14 silver badges39 bronze badges
1
13 characters because of the carriage-return/newline. This is right, you can't expect to send 13 characters for every one you receive, and keep up. Plus, when SoftwareSerial sends it turns interrupts off, so it can't receive at the same time.
– Nick Gammon♦
Jun 23 at 4:31
@JRobert I've managed to successfully transmit continuously at 57600 baud (but with no transmit from the Arduino until after the whole message has been sent to it). Could you edit your answer to reflect this and I'll mark it as the "correct" answer. Thanks.
– AJP
Jun 23 at 12:02
Sure - is that 57600 transmission to- or from the Arduino? Not having the direct experience, I feel I should quote yours in my update.
– JRobert
Jun 23 at 16:31
Yes 57600 transmission is to and from. I tried with 115200 but (and perhaps it's due to my longer electrical wires / lack of proper grounding / interference) but this didn't work and the echo was garbage. This could have just been the transmit from the Arduino to the Raspberry Pi but regardless in this case it didn't work at 115200 but did at 57600.
– AJP
Jun 23 at 22:17
add a comment |
1
13 characters because of the carriage-return/newline. This is right, you can't expect to send 13 characters for every one you receive, and keep up. Plus, when SoftwareSerial sends it turns interrupts off, so it can't receive at the same time.
– Nick Gammon♦
Jun 23 at 4:31
@JRobert I've managed to successfully transmit continuously at 57600 baud (but with no transmit from the Arduino until after the whole message has been sent to it). Could you edit your answer to reflect this and I'll mark it as the "correct" answer. Thanks.
– AJP
Jun 23 at 12:02
Sure - is that 57600 transmission to- or from the Arduino? Not having the direct experience, I feel I should quote yours in my update.
– JRobert
Jun 23 at 16:31
Yes 57600 transmission is to and from. I tried with 115200 but (and perhaps it's due to my longer electrical wires / lack of proper grounding / interference) but this didn't work and the echo was garbage. This could have just been the transmit from the Arduino to the Raspberry Pi but regardless in this case it didn't work at 115200 but did at 57600.
– AJP
Jun 23 at 22:17
1
1
13 characters because of the carriage-return/newline. This is right, you can't expect to send 13 characters for every one you receive, and keep up. Plus, when SoftwareSerial sends it turns interrupts off, so it can't receive at the same time.
– Nick Gammon♦
Jun 23 at 4:31
13 characters because of the carriage-return/newline. This is right, you can't expect to send 13 characters for every one you receive, and keep up. Plus, when SoftwareSerial sends it turns interrupts off, so it can't receive at the same time.
– Nick Gammon♦
Jun 23 at 4:31
@JRobert I've managed to successfully transmit continuously at 57600 baud (but with no transmit from the Arduino until after the whole message has been sent to it). Could you edit your answer to reflect this and I'll mark it as the "correct" answer. Thanks.
– AJP
Jun 23 at 12:02
@JRobert I've managed to successfully transmit continuously at 57600 baud (but with no transmit from the Arduino until after the whole message has been sent to it). Could you edit your answer to reflect this and I'll mark it as the "correct" answer. Thanks.
– AJP
Jun 23 at 12:02
Sure - is that 57600 transmission to- or from the Arduino? Not having the direct experience, I feel I should quote yours in my update.
– JRobert
Jun 23 at 16:31
Sure - is that 57600 transmission to- or from the Arduino? Not having the direct experience, I feel I should quote yours in my update.
– JRobert
Jun 23 at 16:31
Yes 57600 transmission is to and from. I tried with 115200 but (and perhaps it's due to my longer electrical wires / lack of proper grounding / interference) but this didn't work and the echo was garbage. This could have just been the transmit from the Arduino to the Raspberry Pi but regardless in this case it didn't work at 115200 but did at 57600.
– AJP
Jun 23 at 22:17
Yes 57600 transmission is to and from. I tried with 115200 but (and perhaps it's due to my longer electrical wires / lack of proper grounding / interference) but this didn't work and the echo was garbage. This could have just been the transmit from the Arduino to the Raspberry Pi but regardless in this case it didn't work at 115200 but did at 57600.
– AJP
Jun 23 at 22:17
add a comment |
Thanks for contributing an answer to Arduino Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f66518%2fwhat-is-the-best-delay-to-use-between-characters-sent-to-the-serial-port%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Really, there is no "best delay" as it appears the delay needed depends on what the RPi sends the Arduino. The more the RPi sends, the longer the delay in order for the Arduino to not let any data go missing.
– st2000
Jun 23 at 0:49
Are you opening the serial port for every single character? Why? Should you not put the
with
line before thefor
line?– Nick Gammon♦
Jun 23 at 4:39
@NickGammon it was a quick hack to flush the characters to the file.
– AJP
Jun 23 at 9:38
1
Fairly obviously, you will get much better throughput if you don't send the word "Received:" for each character you receive. That's like putting 13 litres of water into a bucket every second and taking 1 litre out, and then wondering why it overflows.
– Nick Gammon♦
Jun 23 at 10:54
1
The documentation should indeed make it clear that SoftwareSerial cannot receive at the same time as it is transmitting, so it is not a good choice for attempting to do that.
– Nick Gammon♦
Jun 23 at 21:47