Serial input reading troubles

Thak you very much you all. I will certainly give your suggestions a very welcome attention tomorrow when the Sun shines again.

Thanks @paulpaulson, but still no display on Serial Monitor.

Hello
try this mods. A Serial.print will show us the character when the receiver has received a character.

void loop() { // read 14-byte echo plus 14-byte answer
int charCnt=0; 
do {
	if (HC12.available()) {
    char inChar = (char)HC12.read();
   Serial.println(inChar); 
    inputString += inChar;
	charCnt++;
	}
} while (charCnt<=28); 

Thanks, but it still keeps printing blank lines over and over after printing a string on the very beginning wich I missed. Follows the code using contributions of both of you:

#include <SoftwareSerial.h>
SoftwareSerial HC12(2,3);      // RX pino 2 , TX pino 3

int i;
boolean messageReceived = false;
String inputString = "";         // a String to hold incoming data

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
  HC12.begin(9600);            // Porta Serial do HC12
}

void loop() { // read 14-byte echo plus 14-byte answer
int charCnt=0; 
     if (HC12.available()==28) {
     for (i=1; i<=28; i=i+1) {
      char inChar = (char)HC12.read();
      inputString += inChar;
      charCnt++;
      }
     } messageReceived = true;
  
  
  // print the string
  
  if (messageReceived) { 
    Serial.println(inputString);
    // clear the string and reset buffer:
    inputString = "";
    messageReceived = false;
    }
}

Will try @paulpaulson mod next...

Aaahhrrgh... now I received an ugly error message. I guess it will need help from Arduino people, unless you could understand all those blah-blah and some portuguese:

D:\Arquivos de programas\Arduino\arduino-builder -dump-prefs -logger=machine -hardware D:\Arquivos de programas\Arduino\hardware -tools D:\Arquivos de programas\Arduino\tools-builder -tools D:\Arquivos de programas\Arduino\hardware\tools\avr -built-in-libraries D:\Arquivos de programas\Arduino\libraries -libraries D:\Meus documentos\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=1A86_7523 -ide-version=10809 -build-path C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216 -warnings=all -build-cache C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_cache_637954 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-5.4.0-atmel3.6.1-arduino2.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.2.1.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino14.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -verbose D:\Meus documentos\Arduino\Sniffer_HC-12\Sniffer_HC-12.ino
D:\Arquivos de programas\Arduino\arduino-builder -compile -logger=machine -hardware D:\Arquivos de programas\Arduino\hardware -tools D:\Arquivos de programas\Arduino\tools-builder -tools D:\Arquivos de programas\Arduino\hardware\tools\avr -built-in-libraries D:\Arquivos de programas\Arduino\libraries -libraries D:\Meus documentos\Arduino\libraries -fqbn=arduino:avr:uno -vid-pid=1A86_7523 -ide-version=10809 -build-path C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216 -warnings=all -build-cache C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_cache_637954 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-5.4.0-atmel3.6.1-arduino2.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.2.1.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino14.path=D:\Arquivos de programas\Arduino\hardware\tools\avr -verbose D:\Meus documentos\Arduino\Sniffer_HC-12\Sniffer_HC-12.ino
Using board 'uno' from platform in folder: D:\Arquivos de programas\Arduino\hardware\arduino\avr
Using core 'arduino' from platform in folder: D:\Arquivos de programas\Arduino\hardware\arduino\avr
Detecting libraries used...
"D:\\Arquivos de programas\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216\\sketch\\Sniffer_HC-12.ino.cpp" -o nul
"D:\\Arquivos de programas\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\libraries\\SoftwareSerial\\src" "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216\\sketch\\Sniffer_HC-12.ino.cpp" -o nul
Using cached library dependencies for file: D:\Arquivos de programas\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src\SoftwareSerial.cpp
Generating function prototypes...
"D:\\Arquivos de programas\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\libraries\\SoftwareSerial\\src" "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216\\sketch\\Sniffer_HC-12.ino.cpp" -o "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216\\preproc\\ctags_target_for_gcc_minus_e.cpp"
"D:\\Arquivos de programas\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216\\preproc\\ctags_target_for_gcc_minus_e.cpp"
Compilando sketch...
"D:\\Arquivos de programas\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\variants\\standard" "-ID:\\Arquivos de programas\\Arduino\\hardware\\arduino\\avr\\libraries\\SoftwareSerial\\src" "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216\\sketch\\Sniffer_HC-12.ino.cpp" -o "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216\\sketch\\Sniffer_HC-12.ino.cpp.o"
Compiling libraries...
Compiling library "SoftwareSerial"
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\libraries\SoftwareSerial\SoftwareSerial.cpp.o
Compiling core...
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\wiring_pulse.S.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\WInterrupts.c.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\hooks.c.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\wiring.c.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\wiring_analog.c.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\wiring_digital.c.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\wiring_pulse.c.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\wiring_shift.c.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\CDC.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\HardwareSerial.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\HardwareSerial0.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\HardwareSerial1.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\HardwareSerial2.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\HardwareSerial3.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\IPAddress.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\PluggableUSB.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\Print.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\Stream.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\Tone.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\USBCore.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\WMath.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\WString.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\abi.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\main.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\new.cpp.o
Usando arquivo compilado anteriormente: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_build_797216\core\core.a
Arquivando núcleo compilado (cacheando) em: C:\DOCUME~1\PAULOS~1\CONFIG~1\Temp\arduino_cache_637954\core\core_arduino_avr_uno_c6063ed0b16a195d847cd96686e87d1b.a
Linking everything together...
"D:\\Arquivos de programas\\Arduino\\hardware\\tools\\avr/bin/avr-gcc" -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216/Sniffer_HC-12.ino.elf" "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216\\sketch\\Sniffer_HC-12.ino.cpp.o" "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216\\libraries\\SoftwareSerial\\SoftwareSerial.cpp.o" "C:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216/core\\core.a" "-LC:\\DOCUME~1\\PAULOS~1\\CONFIG~1\\Temp\\arduino_build_797216" -lm
D:\Arquivos de programas\Arduino\hardware\arduino\avr\cores\arduino\HardwareSerial.cpp: In member function 'availableForWrite':

D:\Arquivos de programas\Arduino\hardware\arduino\avr\cores\arduino\HardwareSerial.cpp:203:1: internal compiler error: Segmentation fault

 }

 ^

Please submit a full bug report,

with preprocessed source if appropriate.

See <http://gcc.gnu.org/bugs.html> for instructions.

lto-wrapper.exe: fatal error: D:\Arquivos de programas\Arduino\hardware\tools\avr/bin/avr-gcc returned 1 exit status

compilation terminated.

d:/arquivos de programas/arduino/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed

collect2.exe: error: ld returned 1 exit status

Usando a biblioteca SoftwareSerial na versão 1.0 na pasta: D:\Arquivos de programas\Arduino\hardware\arduino\avr\libraries\SoftwareSerial 
exit status 1
Erro compilando para a placa Arduino/Genuino Uno

This is my sketch:

#include <SoftwareSerial.h>
SoftwareSerial HC12(2,3);      // RX pino 2 , TX pino 3

int i;
boolean messageReceived = false;
String inputString = "";         // a String to hold incoming data

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
  HC12.begin(9600);            // Porta Serial do HC12
}

void loop() { // read 14-byte echo plus 14-byte answer
int charCnt=0; 
do {     
    if (HC12.available()) {
    char inChar = (char)HC12.read();
   Serial.println(inChar); 
    inputString += inChar;
    charCnt++;
    messageReceived = true;
    }
   } while (charCnt<=28);
 
  if (messageReceived) { 
    Serial.println(inputString);
    // clear the string and reset buffer:
    inputString = "";
    messageReceived = false;
    }
}  

You have messageReceived set = true at the wrong level.

if (HC12.available()==28) {
     for (i=1; i<=28; i=i+1) {
      char inChar = (char)HC12.read();
      inputString += inChar;
      charCnt++;
      }
      messageReceived = true;
     } //messageReceived = true;

Thanks @cattledog, at least I got rid of that horrible error message, but still no print on the Serial Monitor.

This is the present code

void loop() { // read 14-byte echo plus 14-byte answer
int charCnt=0; 
do {     
    if (HC12.available()) {
    char inChar = (char)HC12.read();
   Serial.println(inChar); 
    inputString += inChar;
    charCnt++;
    }
    messageReceived = true;
   } while (charCnt<=28);

  
  // print the string
  
  if (messageReceived) { 
    Serial.println(inputString);
    // clear the string and reset buffer:
    inputString = "";
    messageReceived = false;
    }
}  

I can't understand why it does not print anything, it seems to never get HC12.available()

The Sun is going down again, I have to stop tests until tomorrow, thank you so much for your support.

Is it still the case that the HC12 on the pc and the inverter are working properly but the issue is that the HC12 on the Arduino can not "sniff" the transmission between the other two units?

Yes @cattledog, the PC and the inverter works nice and consistently on the very same configurations of Arduino's HC-12.
The most intriguing fact is that with some of my early (wrong) attempts it did print a string to the monitor first line but followed an infinite loop on blank lines prints.
This tells me there is no problem with Arduino's HC-12 but rather my script contains some mistake that prevents it to get the bytes. Today the inverter has gone sleep but tomorrow I'll try to get at least that endless loop printing again. Maybe comparing both codes we can find some clue.

Are you absolutely certain that the sniffer is on the same channel and using the same mode as the other two? It's not just baud rate and serial protocol 8n1 which are important.

Hello
did you check the HF transmission with a radio receiver simply ?

You are receiving 28 hex bytes and you start with charCount = 0. You increase the index after the character is received, so, you need to count to 27. (Alternatively, you can start your count with 1 and stop at 28.)

You are receiving Hex values which don't translate into printable characters. However, you should be able to confirm that something is received by the sniffer.

I would think that you would have more clarity on what you were sniffing if you kept with the Hex bytes scheme.

When I look at the 28 hex bytes

f2 fe 12 12 d6 e9 1b 00 00 40 00 08 cf 65 0e ab 59 10 01 3b 37 37 00 00 04 43 13 88

I see this as the character print out

òþ..Öé...@..Ïe.«Y..;77...C.ˆ

void loop() { // read 14-byte echo plus 14-byte answer
int charCnt=0; 
do {     
    if (HC12.available()) {
     (char) inChar = (char)HC12.read();  
     Serial.println(inChar); 
     inputString += inChar;
     charCnt++;
    }
    messageReceived = true;
   } while (charCnt<=27);

  
  // print the string
  
  if (messageReceived) { 
    Serial.println(inputString);
    // clear the string and reset buffer:
    inputString = "";
    messageReceived = false;
    }
}

Yes, @cattledog, so much so that with some versions of the script the answer appears on the monitor but with the annoying endless blank lines deluge.
For now I don't mind receiving in hex format or unreadable ASCII characters, as long as I can see anything on the monitor.
Once I got a script to consistently print the received message on monitor I'll deal with the formatting.

Unfortunatelly I have no HF radio receiver other than HC-12s but I do plugged a logic analyzer into RX and TX lines of the sniffer and they do show the hex codes exactly as sent.
So the problem is the Arduino fails to get the bytes even when they are there. For some reason it behaves as the bytes were not available.

One thing to try is to move to different software serial pins incase there is an issue in the hardware. Perhaps also try to only connect the HC12 TX to the Arduino RX as there is no radio sending.

Alternatively, as a hardware test, you can move the HC12 to the hardware serial pins 0,1. As normal with these devices, the TX>RX and RX>TX. You will need to disconnect the HC12 when you upload code. Then connect the radio to the serial pins after the load is complete.

Serial Input to the Arduino can only come from the HC12, and serial print output will go to the monitor and the radio.
Again, on the hardware serial side, you can only wire the receiving.

I don't know if this may be a problem, but I am using a prototype shield to place components. I routed the wires direct to Arduino board but to no avail.
I moved RX and TX from pins 2 and 3 to pins 4 and 5 both physically and in the script and still same null result.
Removing HC-12 RX->TX Arduino also had no effect. Those late tests were done bypassing prototype shield.
Regarding use of pins 0 and 1, I intend to use only HC-12 TX, so to wich pin (0 or 1) should I connect? Would it confuse Serial Monitor operation?
I also noticed that the link between PC and inverter did not work earlier today, I had to turn them off and on again to work. It may be a characteristic of Termite only, but is there a certain order to connect the components of the link?

After reading a great tutorial by @Robin2 I decided to start the code all over again, this time using a nice and well segmented set of functions. It was good when I wrote Pascal code in the '80s and should be good with Arduino today too.
The Sun is setting soon so I'll stop my tests by now. Thank you for the support.

Connect the HC12-TX to the Arduino RX which is pin 0.

The serial monitor should show what is received by the HC12. All your Software Serial will be replace by Serial.

Did you look at the voltage levels on the signal lines as I suggested in reply #20? Use an oscilloscope not a logic analyzer. The ATmega on the UNO seems to have a nice range of input level HIGH voltage of (Min: 0.6*Vcc) but if the HIGH level out of the HC12 module is fairly low it might be too low.