user21437: 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?

user21437: Last test showed it mangles data only when using println() and doesn't depend on the char type. Then I remembered something about CRLF in the Sigfox datasheet:

Is recommended to use full duplex UART. In case of using half duplex, AT
  commands has to be ended only with one of ‘\r’ or ‘\n’ not both. Because if you send
  “AT\r\n” the Sigfox devkit starts sending “OK” instantly after it receive ‘\r’, but
  microcontroller is still sending byte ‘\n’.

SoftwareSerial is in fact half-duplex only (although I didn't find it in the reference documentation) and println() sends both '\r' and '\n', so while sending the later, Sigfox module is already trying to send back the answer, which gets mangled by SoftwareSerial.
Feel free to add your answer to explain why char is signed in Arduino and when to use print() and when write() with serial port.

user37622: I like the readBytes or readBytesUntil function. It doesn't end on a small gap between received bytes and fills the buffer. This functions are blocking with timeout, but if read some other device the input is usually fast and terminated reliably.
example for readBytesUntil:
if (Serial.available()) {
  char buff[32];
  int l = Serial.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);
}


21437: <p>I'm using SoftwareSerial to communicate with a Sigfox module. Documentation for the <a href="https://www.arduino.cc/en/Reference/SoftwareSerialRead" rel="nofollow noreferrer"><code>read</code></a> function assigns its value to a <code>char</code>. However using it I get mangled characters and also have read in other question that I should assign it to <code>int</code> as it can return <code>-1</code> for no data and <code>[0;255]</code> for received character.</p>
<p>This is my basic troubleshooting example:</p>
<pre><code>#include &lt;SoftwareSerial.h&gt;
SoftwareSerial Sigfox(11,10);

void setup() {
  Serial.begin(9600); // HW serial
  Sigfox.begin(9600); // SW serial --&gt; sigfox
}

void loop() {
  if(Sigfox.available()) {
    int c = Sigfox.read();
    Serial.write(c);
  }
  if(Serial.available()) Sigfox.write(Serial.read());
}
</code></pre>
<p>When <code>c</code> is char, I get <code>-5</code> instead of <code>OK</code>. Also other output is mangled. When <code>c</code> is int, it receives the data OK.</p>
<p>My question is - why they use <code>char</code> in the example in documentation? Why is <code>char</code> signed in Arduino?</p>
<p>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 <code>char buffer[32]</code>, but then again I get mangled data when I assign to it. If I use array of <code>byte</code>/<code>int</code>/<code>unsigned char</code>, I can't print it as <code>Serial.write(SF_buffer);</code> won't compile because of <code>no matching function for call to 'write(unsigned char [32])'</code>.</p>
<p>So what variable types should I use to receive and store the text from SoftwareSerial correctly?</p>
<p>Here is my function for reading the whole line:</p>
<pre><code>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);
}
</code></pre>
<p><strong>UPDATE:</strong></p>
<p>Trying <code>readBytesUntil</code> - the same result, but I have found that issue is only when I send data to module using <code>println</code> as in this example:</p>
<pre><code>#include &lt;SoftwareSerial.h&gt;
SoftwareSerial Sigfox(11,10);

void setup() {
  Serial.begin(9600); // HW serial
  Sigfox.begin(9600); // SW serial --&gt; sigfox
  Sigfox.println(&quot;AT$I=11&quot;);
}

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 &gt; 0 &amp;&amp; buff[l - 1] == '\r') {
      l--; // to remove \r if it is there
    }
    buff[l] = 0; // terminate the string
    Serial.println(buff);
  }
}
</code></pre>
<p>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.</p>
<blockquote>
<p>ӂ*⸮FBB1E27EE7</p>
<p>3430E2FBB1E27EE7</p>
</blockquote>
<p>Also maybe I'm little lost in when to use print and when write... Example says to use <code>write()</code> to send back the char that was <code>read()</code>. Juraj's response uses <code>println()</code> to print the null-terminated buffer. What is the difference when I write/println this buffer?</p>
 21437: <p>Last test showed it mangles data only when using <code>println()</code> and doesn't depend on the <code>char</code> type. Then I remembered something about CRLF in the Sigfox datasheet:</p>

<blockquote>
  <p>Is recommended to use full duplex UART. In case of using half duplex, AT
  commands has to be ended only with one of ‘\r’ or ‘\n’ not both. Because if you send
  “AT\r\n” the Sigfox devkit starts sending “OK” instantly after it receive ‘\r’, but
  microcontroller is still sending byte ‘\n’.</p>
</blockquote>

<p>SoftwareSerial is in fact <a href="https://electronics.stackexchange.com/questions/23265/arduino-software-serial-full-duplex">half-duplex only</a> (although I didn't find it in the reference documentation) and <code>println()</code> sends both '\r' and '\n', so while sending the later, Sigfox module is already trying to send back the answer, which gets mangled by SoftwareSerial.</p>

<p>Feel free to add your answer to explain why <code>char</code> is signed in Arduino and when to use <code>print()</code> and when <code>write()</code> with serial port.</p>
 37622: <p>I like the readBytes or readBytesUntil function. It doesn't end on a small gap between received bytes and fills the buffer. This functions are blocking with timeout, but if read some other device the input is usually fast and terminated reliably.</p>

<p>example for readBytesUntil:</p>

<pre><code>if (Serial.available()) {
  char buff[32];
  int l = Serial.readBytesUntil('\n', buff, sizeof(buff));
  if (l &gt; 0 &amp;&amp; buff[l - 1] == '\r') {
    l--; // to remove \r if it is there
  }
  buff[l] = 0; // terminate the string
  Serial.println(buff);
}
</code></pre>
