2

I am using some third party libraries for some third party hardware. The libraries communicate with the hardware over a serial connection. Using the libraries I send data over the serial interface to the hardware and get a response, which is stored in an array:

// This is the byte array declared in the third party libraries
// that stores data sent back from the external hardware
byte comm_buf[201];

/* I send data to hardware, comm_buf gets filled */

// Printing out the received data via a second serial line to which
// I have a serial monitor to see the data

for (int i = 0; i <= 50; i++) {
  Serial.print(gsm.comm_buf[i]);
}    

// This is printed via the second monitoring serial connection (without spaces)
13 10 43 67 82 69 71 58 32 48 44 51 13 10 13 10 79 75 13 10 00

// It is the decimal ascii codes for the following text
+CREG: 0,3 

How can I convert a byte array into a format that I can evalute in code so I can perform an operation like the following pseudo code;

byte comm_buf[201];

/* I send data to hardware, comm_buf gets filled */

if (comm_buf[] == "CREG: 0,3" ) {
  // do stuff here
}

Do I need to convert it to a string some how, or compare to another char array perhaps?

8
  • 1
    Are you looking for strcmp(com_buffer, "CREG: 0,3")? Commented Aug 1, 2013 at 18:39
  • if (strcmp(gsm.comm_buf,"\r\n+CREG: 0,3\r\n\r\nOK\n")) { gives the error invalid conversion from 'byte*' to 'const char*' Commented Aug 1, 2013 at 18:48
  • Yes, and you can resolve that error by googling the error message ;) Commented Aug 1, 2013 at 18:49
  • 1
    strcmp((const char *)comm_buf, "foobar") Commented Aug 1, 2013 at 18:53
  • 1
    It has nothing to do with efficiency. The typecasting operation is, in this (simplest) case, does nothing in the code - it just fools the compiler. The reason: strcmp() expects two arguments of type const char *, but your array is of type byte[] which decays into byte * when passed to the function - and two pointers of which the base type is different (const char * and byte * in your case) are incompatible types. Commented Aug 1, 2013 at 18:58

1 Answer 1

3

Here are all functions in string.h for string/memory comparison that you could use with arduino. You could use strcmp or memcmp.

Beware you can't compare in C two strings by simply using == operator. You would just compare values of two memory pointers.

Here is an example of comparison inside your buffer:

if (strcmp((const char*)gsm.comm_buf, "\r\n+CREG: 0,3\r\n\r\nOK\n")==0)
{
    Serial.print("abc");
}

You can use strcmp if your recieved message is null byte terminated, if not you will have to use memcmp for the job.

For both functions You have to check if return value is zero, then those strings are equal.

If you want to compare not from first byte of buffer (with zero index) but for example fifth (index 4) you can just add 4 to your pointer:

if (strcmp((const char*)gsm.comm_buf + 4, "\r\n+CREG: 0,3\r\n\r\nOK\n")==0)
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent advice, I was unaware of both the reference you linked and the == problem. Thanks!
memcmp expects a last parameter (.., .., size_t num)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.