Jump to content
43oh

Serial.println(String) prints empty line


Recommended Posts

Hello again,

 

my actual project is a windowsphone app. With this app i want to control some led lights over bluetooth -> means that a command string comes over softwareserial

I have already solved a lot myself, but now I need support.

 

First Problem is that these 2 Lines only prints an empty row on the serial console, but not the word TEST.

In a simple sketch this is working good! But not in my program, an i don't know why.

 

:excl: look at the simple example at the bottom 

 

String pointer5 = "TEST";
Serial.println(pointer5); 

 

 

Second, this has not worked:

 

if (pointer[5] == "True" {

 

}

.

.

 

Is there a solution?


#include <WS2811Driver.h>

#include <SoftwareSerial.h>

const int TX_BT = 13;
const int RX_BT = 12;

const int PIN_RED = 2;
const int PIN_GREEN = 14;
const int PIN_YELLOW = 7;

const int PIN_PROXIMITY_SENSOR = 5;

SoftwareSerial btSerial(TX_BT, RX_BT);
byte leds0[30];
WS2811Driver ledStrip;
const char* color = "";


#define SOP '<'
#define EOP '>'


bool started = false;
bool ended = false;

int commandSize = 0;
int commandPos = 0;
char command[40];
char *pointer[6];

void processCommand(char* command) {
    btSerial.end();
    
  char *p = command;
  char *str;
  int i =0;
  
  
while ((str = strtok_r(p, ",", &p)) != NULL) // delimiter is comma
 {   
 
 pointer[i] = str;
 i++;
 }



 Serial.println("NEW COMMAND");
 Serial.println(pointer[0]);
 Serial.println(pointer[1]);
 Serial.println(pointer[2]);
 Serial.println(pointer[3]);
 Serial.println(pointer[4]);
 Serial.println(pointer[5]);
 String pointer5 = "TEST";   
 Serial.println(pointer5);   //This Line is printing an empty row, not TEST

    btSerial.begin(9600);  
}




//Setup MSP430 function
void setup() {
  Serial.begin(9600);
  //Serial.println("USB Connected");
  btSerial.begin(9600);
  
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_YELLOW, OUTPUT);  
  
  pinMode(PIN_PROXIMITY_SENSOR, INPUT_PULLUP);
  
  ledStrip.setLEDCount(10); // setup for 4 leds on a strip
  ledStrip.begin();   
}
 //Loop MSP430 function
void loop() {
  if(btSerial.available()) {
      commandSize = (int)btSerial.read();
      
      commandPos = 0;
      
      while(commandPos < commandSize) {
        if(btSerial.available()) {
        char buffer = btSerial.read();   
        if(buffer == SOP)    {
        commandPos = 0;
        command[commandPos] = '\0';
        started = true;
        ended = false;
        }
        else if(buffer == EOP){
        ended = true;
        break;
        }
        else
        {
        command[commandPos] = buffer;
        commandPos++;
        command[commandPos] = '\0';
        }
        }
        } //end while
        }
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    processCommand(command);
    // Reset for the next packet
    started = false;
    ended = false;
    commandPos = 0;
    command[commandPos] = '\0';
   }
             
} //this is the end

This is the console output:

 

NEW COMMAND
126
141
F53900
True
True
True
                                                           <- here in this line should be: TEST
NEW COMMAND
126
141
F53900
False
True
True
 
NEW COMMAND
126
141
F53900
True
True
True
 

 

Update: if I do not turn on software serial, the output of the strings works again

 

 

here's some simpler code which shows the strange behavior:

#include <SoftwareSerial.h>


String text = "blablabla";


SoftwareSerial btSerial(P1_4, P1_3);


char command[40];



void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  //btSerial.begin(9600);            // if you uncomment this line, the String text is not printed:
}

void processCommand(char* command) {
  btSerial.end();
  Serial.println("HELLO");
  Serial.println(text);             //works only with btSerial.begin(9600) commentet above
  
}

  void loop() {
   
  
       processCommand(command);
  delay(500);      
} 

Edited by StefanWxx
Link to post
Share on other sites

Serial handling is done in a separate routine, you're actually printing to a buffer that is scooped empty in between loop() calls (and between setup() and loop()).

As a result you enable your btSerial before actual transmission of serial data is done.

I do agree it's odd that serial and software serial do not work together...

Why do you disable btSerial while in your processCommand()?

Link to post
Share on other sites

Thanks for your reply,

 

My app has just sent to many commands when a slider is moved, so i get corrupt data.

Disabling btSerial has helped.

 

 

In the meantime I have changed my sketch, the bluetooth receiver is now connected to the hardwareserial ports, Pin3&4 . Although I can not debug, but .... it works now as it should  :thumbsu:

and in this case i had to comment out the disable Serial command in processCommand() but don't know why.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...