Start a new topic

Software Serial Problem

I have the following Problem. I have a start page with 6 Buttons.

If i press the button arduino should go in a new loop type: in test_mode

My Code: 

//#include <Wire.h>
#include <SoftwareSerial.h>
//#include <Nextion.h>
SoftwareSerial softser(2,3); //RX,TX



int cycle = 0;
int cycle_help = 0;
int cycle_temp[] = {};
int cycle_time[] = {}; 
char softread = "";


void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  while (!Serial){
    
  }
  Serial.print("test");
   softser.begin(9600);
  
  

  
}

void loop() {
 
  if (softser.available()){
    softread = softser.read();
    Serial.print(softser.read());
  }
  if (softread == "test_mode"){
  
    test_mode();
  }
  
  if (softread == "cycle"){
    while (1){
    cycle = softser.read();  
      if (cycle > 0){
        break;  
      }
    
    }
  }
  if (softread == "temp"){
    while(1){
        
        //myNextion.print("Value" + cycle_help);
        cycle_temp[cycle_help] = softser.read();
        cycle_help++;
        if (cycle_help == cycle){
          cycle_help = 0;
          break;
        } 
      
    }
    
  }
  if (softread == "time"){
    while(1){
        
        //softserial.print("Value" + cycle_help);
        cycle_time[cycle_help] = softser.read();
        cycle_help++;
        if (cycle_help == cycle){
          break;
        } 
      
    }
  }
 
    
   if (softread == "go"){
      sendData();
   }     
 
}

void sendData(){
int temp_ok = 0;
int time_ok = 0;


while(!Serial){
  
 }
  
  
  while(temp_ok == 0){
  Serial.print("Ready");  
  temp_ok  = Serial.read();
      
  }
  int i;
  for (i = 0; i < sizeof(cycle_temp); i++) {
  Serial.print(cycle_temp[i]);
  }
  i = 0;
 
  while(time_ok == 0){
  time_ok = Serial.read();  
  }

  for (i = 0; i < sizeof(cycle_time); i++) {
  Serial.print(cycle_time[i]);
  }
  delay(100);
  Serial.print("Finished");
  
}
void test_mode(){
  
  Serial.begin(9600);
  Serial.print("Entered test_mode");

  if (softser.read()=="heat"){
    Serial.print("heat");
  }
  if (softser.read()=="cool"){
    Serial.print("cool"); 
   }
  if (softser.read()=="pwm_fan"){
    Serial.print("pwm_fan");
    Serial.print(softser.read());
   }else if(softser.read()=="pwm_heat"){
    Serial.print("pwm_heat");
    Serial.print(softser.read()); 
   }
  
  
  
  
  
}

 And if i include Nextion it wont compile.

My Second Problem is i have this test code:

 

/*
  Software serial multple serial test

 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.

 The circuit:
 * RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)

 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts,
 so only the following can be used for RX:
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

 Not all pins on the Leonardo and Micro support change interrupts,
 so only the following can be used for RX:
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

 created back in the mists of time
 modified 25 May 2012
 by Tom Igoe
 based on Mikal Hart's example

 This example code is in the public domain.

 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

 if i run this code i get the Serial print from the Nextion without problems.

When i useing my first code it just print "-1" or some random nummbers


First example //Nextion.h with Software serial - not Itead's Library.

- perhaps ask the owners of such library for support?


Second example

  mySerial.println("Hello, world?");

is not a valid Nextion text based command. I should expect this not to work


Main loop

  takes bytes from one port, dumps to other

No magic there, Expected to work.


Perhaps when you have familiarized yourself with Nextion

you might be then able to pose a Nextion question

Nextion Instruction Set is found on Nextion HMI Solution page

Our tasks do not include debugging user code.


Dumping your Arduino code here is simply not proper.

While learning to code: your question belongs on Arduino forum.


I would also recommend actually using the Iteadlib

   Link to Iteadlib is on Nextion HMI Solution page

and trying the corresponding Arduino Nextion examples.

It is clear you have more reading to do

- best place to start is Selecting Help under the Help Menu.

Arduino Nextion examples is the top thread in Free Chat.


Hi Tobias


Your understanding of .read() is all wrong in the first example.


And I really don't want to rewrite the whole thing for you. Basically the function reads one character at a time and not a whole string as you have it.


You need to use the function to build Arduino style Strings or 'C' style character arrays.


Lines such as softread = softser.read();


will just result in a copy of the character read at that instance being assigned to softread[0]

over and over  


To keep it simple in your case I would stick with building Arduino style Strings.


And to do that from the serial port goes something like this... 

String softread;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  while (!Serial){    
  }
  Serial.print("test");
  softser.begin(9600);
}

void loop() {
 
  while (softser.available()) {

    char rChar = softser.read();
    softread += rChar;
    if (rChar == '\n') {//You need a terminating character to identify end of string. Here we use newline.      
      Serial.println(softread);
    }
  }
}

Building character array 'C' strings is a bit more advanced. You need to do some more reading for sure.

@Tobias

Here are a couple of interesting websites talking about Serial.read():

https://hackingmajenkoblog.wordpress.com/2016/02/01/reading-serial-on-the-arduino/

https://www.gammon.com.au/serial

.

Login or Signup to post a comment