I'm using SoftwareSerial to communicate with a Sigfox module. Documentation for the read
function assigns its value to a char
. However using it I get mangled characters and also have read in other question that I should assign it to int
as it can return -1
for no data and [0;255]
for received character.
This is my basic troubleshooting example:
#include <SoftwareSerial.h>
SoftwareSerial Sigfox(11,10);
void setup() {
Serial.begin(9600); // HW serial
Sigfox.begin(9600); // SW serial --> sigfox
}
void loop() {
if(Sigfox.available()) {
int c = Sigfox.read();
Serial.write(c);
}
if(Serial.available()) Sigfox.write(Serial.read());
}
When c
is char, I get -5
instead of OK
. Also other output is mangled. When c
is int, it receives the data OK.
My question is - why they use char
in the example in documentation? Why is char
signed in Arduino?
I want to read the entire line of text from serial port and store it into array. How to define that array? I have found an example on web that uses char buffer[32]
, but then again I get mangled data when I assign to it. If I use array of byte
/int
/unsigned char
, I can't print it as Serial.write(SF_buffer);
won't compile because of no matching function for call to 'write(unsigned char [32])'
.
So what variable types should I use to receive and store the text from SoftwareSerial correctly?
Here is my function for reading the whole line:
unsigned char SF_buffer [32]; // max length of line to read from serial
bool SigfoxReadine() { // return true when whole line was received and is in buffer
static int SF_cnt=0;
while (Sigfox.available()) {
int c = Sigfox.read();
SF_buffer[SF_cnt++] = c;
if ((c == '\n') || (SF_cnt == sizeof(SF_buffer)-1)) {
SF_buffer[SF_cnt] = '\0';
SF_cnt = 0;
return(true);
}
}
return(false);
}
UPDATE:
Trying readBytesUntil
- the same result, but I have found that issue is only when I send data to module using println
as in this example:
#include <SoftwareSerial.h>
SoftwareSerial Sigfox(11,10);
void setup() {
Serial.begin(9600); // HW serial
Sigfox.begin(9600); // SW serial --> sigfox
Sigfox.println("AT$I=11");
}
void loop() {
if(Serial.available()) Sigfox.write(Serial.read());
SF_read();
}
void SF_read() {
if (Sigfox.available()) {
char buff[32];
int l = Sigfox.readBytesUntil('\n', buff, sizeof(buff));
if (l > 0 && buff[l - 1] == '\r') {
l--; // to remove \r if it is there
}
buff[l] = 0; // terminate the string
Serial.println(buff);
}
}
The response to first command (in setup) is mangled. The response to the same AT command manually written into serial monitor (and sent to module via Sigfox.write in loop) is OK.
ӂ*⸮FBB1E27EE7
3430E2FBB1E27EE7
Also maybe I'm little lost in when to use print and when write... Example says to use write()
to send back the char that was read()
. Juraj's response uses println()
to print the null-terminated buffer. What is the difference when I write/println this buffer?
int
data type is advisable ... if you usebyte
data type, then there is a conflict between data and an error flag ... a-1
is equivalent to hex0xff
, which is decimal255
.... forint
type, the decimal 255 is represented as0x00ff
and-1
is represented as 0xffff ... no conflict between those two .... please note that only a single byte is transmitted over serial ... the two byteint
is created by the serial library so that your sketch has a way of receiving an error status from the librarySerial.write(SF_buffer, length);
or for null terminated char arraySerial.print(SF_buffer);
SF_buffer
is char type, I get mangled output (SF_buffer[SF_cnt++]=c;
). How to assign theint c
into thechar
array correctly?