strcmp comparison bet predefined and received string - c

This is a mplab harmony code, I am trying to received a data at client, I want to control the LEDs from server socket, text "START" will turn the LEDs ON and "STOP" will turn the LEDs OFF, when I debug the code, it shows that Appbuffer[80] has all null variable, ACK[] = last variable is null \0, same in AOK[].I want to know that is it a right way to compare the string as I have written in the code here. Because when I debug the code, it escape this line and jump to server task init(). Please help me.
case APP_TCPIP_WAIT_FOR_RESPONSE:
{
char Appbuffer[80];
static const char ACK[]="START";
static const char AOK[]="STOP";
memset(Appbuffer, 0, sizeof(Appbuffer));
if (!TCPIP_TCP_IsConnected(appData.clientSocket))
{
SYS_CONSOLE_MESSAGE("\r\nConnection Closed\r\n");
appData.clientState = APP_TCPIP_WAITING_FOR_COMMAND;
break;
}
if (TCPIP_TCP_GetIsReady(appData.clientSocket))
{
TCPIP_TCP_ArrayGet(appData.clientSocket, (uint8_t*)Appbuffer, sizeof(Appbuffer) - 1);
SYS_CONSOLE_PRINT("%s", Appbuffer);
if(!strcmp(Appbuffer, ACK)) //// breakpoint
{
BSP_LEDStateSet(BSP_LED_1,BSP_LED_STATE_ON);
BSP_LEDStateSet(BSP_LED_2,BSP_LED_STATE_ON);
BSP_LEDStateSet(BSP_LED_3,BSP_LED_STATE_ON);
}
else if(!strcmp(Appbuffer, AOK)) // breakpoint
{
BSP_LEDStateSet(BSP_LED_1,BSP_LED_STATE_OFF);
appData.serverState = APP_TCPIP_CLOSING_CONNECTION;
SYS_CONSOLE_MESSAGE("Connection was closed\r\n");
}
}
}

AppBuffer has all null because of the call to function memset.
I didn't see in the code you posted, that AppBuffer is assigned a value.
Hence AppBuffer is essentially a zero-length string and therefore when you compare it with AOK, the result is false.

Related

strcpy() does not write new string

In my code, I want to update the contents of global array data via a function. When I call the function, however, the contents of data do not change, despite the function calling strcpy() to effect that change -- afterward, data still contains "prg". How can I use strcpy() or something similar to write a new value to data?
char data[255] = "prg";
void process_tuple(Tuple *t)
{
//Get key
int key = t->key;
//Get integer value, if present
int value = t->value->int32;
//Get string value, if present
char string_value[32];
strcpy(string_value, t->value->cstring);
strcpy(data, "prg1212");
//Decide what to do
switch(key) {
case key_0:
break;
};
}
static WeatherAppDataPoint s_data_points[] =
{
{
.city = data,
.description = "surfboard :)",
.icon = WEATHER_APP_ICON_GENERIC_WEATHER,
.current = 110,
.high = 120,
.low = 100,
},
};
You are mistaking, the program as written will change data to prg1212 if the function is executed. You are either not calling it (bad event hookup, etc), the code as written isn't accurate (partial code that hides the real problem) or evaluating the wrong pointer during debugging (or at the wrong time).
I have figured out the problem with help from #Blidny and #MikeofSST
It turns out that my code above contained in a file app_data.c wasn't running code outside if the functions created in app_data.h. I moved my code up into the void where my temperature values were formatted and written, and strcpy() worked like a charm.

Receiving a fixed length string on Arduino serial and storing it in a structure

I am currently receiving a fixed length string means it's length cant exceed more than 50 but can be less then that.i am using following code.
void loop()
{
char *datareceived;
int number;
char sword[] ="times";
uint8_t x;
digitalWrite(ledPin,LOW);
number = BTSerial.available();
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (number){
datareceived = (char *)malloc(sizeof(char)*number);
if(datareceived == 0){
Serial.print("Error: Out of Memmory");
}
if(number>0){
for(x=0;x<number;x++){
datareceived[x]=BTSerial.read();
if(datareceived[x] == '\n')
//datareceived[x]='\0';
break;
}
strcpy(dataReceivedfromBluetooth,datareceived);
Serial.println(dataReceivedfromBluetooth);
}
if((strstr(dataReceivedfromBluetooth,sword))){
Serial.println("Yes");
digitalWrite(ledPin,HIGH);
delay(30);
}
else{
digitalWrite(ledPin,LOW);
}
}
free(datareceived);
}
Now with this code, it is not printing any thing at all.I think my code of dynamic memory allocation is all right.
whereas a simple code like this
void loop(){
if (BTSerial.available())
Serial.write(BTSerial.read());
if (Serial.available())
BTSerial.write(Serial.read());
}
is working perfect ally fine and writing everything on serial port, so what i am trying in my main code is what ever is being received by BTSerail.read() is stored in a array/string and then i use that array for some of string operations like strstsr to find out whether that incoming string has a particular word or not.
Yes, it's very likely to be a problem in your code: you're not terminating the string.
A C string is just an array of characters, with a character with the value '\0' marking the end of the string. Unless you know that the sender includes the terminator, you're not writing one so the string just "runs off" into la-la-land.

Arduino Serial.println() outputs a blank line if not in loop()

I'm attempting to write a function that will pull text from different sources (Ethernet client/Serial/etc.) into a single line, then compare them and run other functions based on them. Simple..
And while this works, I am having issues when trying to call a simple Serial.println() from a function OTHER than loop().
So far, I have around 140 lines of code, but here's a trimmed down version of the portion that's causing me problems:
boolean fileTerm;
setup() {
fileTerm = false;
}
loop() {
char character;
String content="";
while (Serial.available()) {
character = Serial.read();
content.concat(character);
delay(1);
}
if (content != "") {
Serial.println("> " + content);
/** Error from Serial command string.
* 0 = No error
* 1 = Invalid command
*/
int err = testInput(content);
}
int testInput(String content) {
if (content == "term") {
fileTerm = true;
Serial.println("Starting Terminal Mode");
return 0;
}
if (content == "exit" && fileTerm == true) {
fileTerm = false;
Serial.println("Exiting Terminal Mode");
return 0;
}
return 1;
}
(full source at http://pastebin.com/prEuBaRJ)
So the point is to catch the "term" command and enter some sort of filesystem terminal mode (eventually to access and manipulate files on the SD card). The "exit" command will leave the terminal mode.
However, whenever I actually compile and type these commands with others into the Serial monitor, I see:
> hello
> term
> test for index.html
> exit
> test
> foo
> etc...
I figure the function is catching those reserved terms and actually processing them properly, but for whatever reason, is not sending the desired responses over the Serial bus.
Just for the sake of proper syntax, I am also declaring the testInput() function in a separate header, though I would doubt this has any bearing on whether or not this particular error would occur.
Any explainable reason for this?
Thanks.
Model: Arduino Uno R3, IDE version: 1.0.4, though this behavior also happened on v1.0.5 in some instances..
It is kinda guessable how you ended up putting delay(1) in your code, that was a workaround for a bug in your code. But you didn't solve it properly. What you probably saw was that your code was too eager to process the command, before you were done typing it. So you slowed it down.
But that wasn't the right fix, what you really want to do is wait for the entire command to be typed. Until you press the Enter key on your keyboard.
Which is the bug in your code right now, the content variable doesn't just contain "term", it also contains the character that was generated by your terminal's Enter key. Which is why you don't get a match.
So fix your code, add a test to check that you got the Enter key character. And then process the command.

Splitting a comma-delimited string of integers

My background is not in C (it's in Real Studio - similar to VB) and I'm really struggling to split a comma-delimited string since I'm not used to low-level string handling.
I'm sending strings to an Arduino over serial. These strings are commands in a certain format. For instance:
#20,2000,5!
#10,423,0!
'#' is the header indicating a new command and '!' is the terminating footer marking the end of a command. The first integer after '#' is the command id and the remaining integers are data (the number of integers passed as data may be anywhere from 0 - 10 integers).
I've written a sketch that gets the command (stripped of the '#' and '!') and calls a function called handleCommand() when there is a command to handle. The problem is, I really don't know how to split this command up to handle it!
Here's the sketch code:
String command; // a string to hold the incoming command
boolean commandReceived = false; // whether the command has been received in full
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// main loop
handleCommand();
}
void serialEvent(){
while (Serial.available()) {
// all we do is construct the incoming command to be handled in the main loop
// get the incoming byte from the serial stream
char incomingByte = (char)Serial.read();
if (incomingByte == '!')
{
// marks the end of a command
commandReceived = true;
return;
}
else if (incomingByte == '#')
{
// marks the start of a new command
command = "";
commandReceived = false;
return;
}
else
{
command += incomingByte;
return;
}
}
}
void handleCommand() {
if (!commandReceived) return; // no command to handle
// variables to hold the command id and the command data
int id;
int data[9];
// NOT SURE WHAT TO DO HERE!!
// flag that we've handled the command
commandReceived = false;
}
Say my PC sends the Arduino the string "#20,2000,5!". My sketch ends up with a String variable (called command) that contains "20,2000,5" and the commandRecieved boolean variable is set to True so the handleCommand() function is called.
What I would like to do in the (currently useless) handleCommand() function is assign 20 to a variable called id and 2000 and 5 to an array of integers called data, i.e: data[0] = 2000, data[1] = 5, etc.
I've read about strtok() and atoi() but frankly I just can't get my head around them and the concept of pointers. I'm sure my Arduino sketch could be optimised too.
Since you're using the Arduino core String type, strtok and other string.h functions aren't appropriate. Note that you can change your code to use standard C null-terminated strings instead, but using Arduino String will let you do this without using pointers.
The String type gives you indexOf and substring.
Assuming a String with the # and ! stripped off, finding your command and arguments would look something like this:
// given: String command
int data[MAX_ARGS];
int numArgs = 0;
int beginIdx = 0;
int idx = command.indexOf(",");
String arg;
char charBuffer[16];
while (idx != -1)
{
arg = command.substring(beginIdx, idx);
arg.toCharArray(charBuffer, 16);
// add error handling for atoi:
data[numArgs++] = atoi(charBuffer);
beginIdx = idx + 1;
idx = command.indexOf(",", beginIdx);
}
data[numArgs++] = command.substring(beginIdx);
This will give you your entire command in the data array, including the command number at data[0], while you've specified that only the args should be in data. But the necessary changes are minor.
seems to work, could be buggy:
#include<stdio.h>
#include <string.h>
int main(){
char string[]="20,2000,5";
int a,b,c;
sscanf(string,"%i,%i,%i",&a,&b,&c);
printf("%i %i %i\n",a,b,c);
a=b=c=0;
a=atoi(strtok(string,","));
b=atoi(strtok(0,","));
c=atoi(strtok(0,","));
printf("%i %i %i\n",a,b,c);
return 0;
}

variables value unexpectedly changes?

The program below uses a keypad and arduino to test whether an input is equal to the password. Whenever '#' is pressed the input is checked and then the variable storing the input resets to an empty string (char input[257] = ""). The program then loops back to the beginning of the void loop() code block. I am having a problem when my program loops back to the beginning of the "void loop()" code block. When i reset input to an empty string, input is reset to an empty string as it should. However, when the program loops the value of input is changed to what it was before. So if i originally entered "123abc" and hit '#' the program would tell me that the input was incorrect and then the program would reset the variable to an empty string, but when the program loops the variable storing the empty string is changed back to "123abc". What's happening? Why doesn't the variable remain an empty string?
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
char password[9] = "3994A", input[257]="";
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int x;
void setup(){
Serial.begin(9600);
}
void loop(){
x = 0;
Serial.println(input);
while (1)
{
char key = keypad.getKey();
if (key)
{
if (key == '#')
{
break;
}
input[x] = key;
x+=1;
}
}
if (strcmp(password,input) == 0)
{Serial.println("Access Granted");
}
else
{Serial.println("Access Denied");
}
char input[257] = "";
Serial.println(input);
}
Not the same variable. You've got one input in your top block and another in your loop function.
This line
char input[257] = "";
makes a new local variable called input, but you dont want that. You already made a global one here:
char password[9] = "3994A", input[257]="";
Change
char input[257] = "";
to
memset(input,0,257);
So you dont lose the new variable off the stack and instead use the global one you declared earlier.
The second char input[257] = ""; declares a new variable named input. It does not change the contents of the existing variable named input. Use memset() instead.
I agree with the error in declaring again the input variable
char input[257] = "";
but i would not solve it with memset() but doing
input[0]='\0';
because this is faster and doesn't require to include the memset() function on the code. we are talking about a microcontroller, cpu and memory is precious and it is a good habit to write fast and light code.
Here, how could I achieve this:
Go to the official Arduino Website Download Keypad.h http://playground.arduino.cc/Code/Keypad#Download (ctrl+f and type: download) Download the library to put on C:\Program Files (x86)\Arduino\libraries Close your current Arduino screen then re-open the screen. Rewrite your code and put the code first. I okey with this method.
#include <Keypad.h>

Resources