How test return from Serial.readBytes against file text?[SOL

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

How test return from Serial.readBytes against file text?[SOL

Post by DanPolka »

In the part of a sketch below, I'm trying to make sure that I correctly received the contents of a file that was sent to Uno via USB, then read by Serial.readBytes. So I tried to make a test such that if the received data matches the original contents of the file, the led on Uno lights up. But it doesn't work, because of compile error:
GetMP3Chunks_2b:46: error: ISO C++ forbids comparison between pointer and integer

Code: Select all

       if (Serial.available() > 0){
            Serial.readBytes((char*)&headerBuf, sizeof(Header_t));  // this presumably 'stops' the loop, reads until all of header in headerBuf
            if (0xa5a5 == headerBuf.syncPattern) {//make sure sync pattern matches
              // digitalWrite(led, HIGH); // shows if sync pattern sent correctly
               Serial.readBytes((char*)&dataBuffer, headerBuf.dataBlockLen ); // reads all data?
               if ( (dataBuffer, headerBuf.dataBlockLen) == "First file is a short test file." )
                  digitalWrite(led, HIGH);
               Serial.write(dataBuffer, headerBuf.dataBlockLen);
  // mostly works, if directly above if-test is not there.
The compile error occurs in line:

Code: Select all

 if ( (dataBuffer, headerBuf.dataBlockLen) == "First file is a short test file." )
So how could I do this kind of test correctly? (I want to do the test inside the Uno sketch, because what I'm getting 'echoed' back through the USB port has some extra characters appended to the front of the original text, but I'm not sure if it's an error of receiving the text into the Uno sketch, sending the text from the sketch, or displaying the text in a separate terminal program. That's why I want to test the received text inside the Uno sketch via the led.)
Last edited by DanPolka on Fri Apr 11, 2014 8:09 pm, edited 2 times in total.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: How test return from Serial.readBytes against file text?

Post by adafruit_support_mike »

It looks like you forgot the name of a function.. the expression:

Code: Select all

(dataBuffer, headerBuf.dataBlockLen)
does have a meaning, but not one that would be useful for a string comparison.

DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: How test return from Serial.readBytes against file text?

Post by DanPolka »

Not sure I understand, but I did solve this problem, not by figuring out how to test the received data against the sent data, but by echoing the received data better & seeing that it's what was sent.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How test return from Serial.readBytes against file text?

Post by adafruit_support_rick »

First thing to do is to capture the return value from Serial.readBytes, which tells you how many bytes it read. Then, you run your checksum calculator on dataBuffer, and compare the

Code: Select all

int bytesRead = Serial.readBytes((char*)&dataBuffer, headerBuf.dataBlockLen ); // reads all data?
if (bytesRead == headerBuf.dataBlockLen)
{
  if (calcCheckSum(dataBuffer, bytesRead) == headerBuf.checkSum)
  {
    //dataBuffer was received successfully
In your final code, won't really be doing this,

Code: Select all

if ( (dataBuffer, headerBuf.dataBlockLen) == "First file is a short test file." )
but the way to do it is to use either the strncmp or memcmp functions. strncmp() compares two strings up to a maximum length, memcmp() compares two arbitrary buffers of a specific length. Both return zero if the strings/buffers are equivalent:

Code: Select all

#define TEST_STR "First file is a short test file."
if (0 == strncmp(TEST_STR, dataBuffer, strlen(TEST_STR))
{
  //strings are equivalent
or

Code: Select all

#define TEST_STR "First file is a short test file."
if (0 == memcmp(TEST_STR, dataBuffer, strlen(TEST_STR)
{ //buffers are equivalent

DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: How test return from Serial.readBytes against file text?

Post by DanPolka »

Ok, I'll bear that in mind as I might need it later, but I did get the data to return ok with the following code snippet:

Code: Select all

    if (Serial.available() > 0){
            Serial.readBytes((char*)&headerBuf, sizeof(Header_t));  // this presumably 'stops' the loop, reads until all of header in headerBuf
            if (0xa5a5a5a5 == headerBuf.syncPattern) {//make sure sync pattern matches
               digitalWrite(led, HIGH); // shows if sync pattern sent correctly
               Serial.readBytes((char*)&dataBuffer, headerBuf.dataBlockLen ); // reads all data?
               
          // THIS SENDS THE *DATA* BACK TO EuTerm FOR *VALIDATION OF RECEPTION*:
               
               Serial.write(dataBuffer, headerBuf.dataBlockLen); // WORKS
This presupposes that I've made the EuTerminal program send the filename as 13 characters, so sizeOf the header works, but I have not yet figured out IF I can send a null character at end, nor if it's necessary; I just send zeroes to pad the filename, and that works so far, though I'm having trouble removing those zeroes in the sketch, will ask about that in the main thread, with any other questions.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: How test return from Serial.readBytes against file text?

Post by adafruit_support_rick »

Don't worry about padding the filename. If you treat is as a string on both sides of the protocol, it will be null-terminated. It doesn't matter what is in the filename buffer after the null terminator.

DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: How test return from Serial.readBytes against file text?

Post by DanPolka »

Hmmm; is it the case that even though I've allocated space in the Uno sketch for a header structure, but I send other than those lengths of parts to the header, like NOT as much as 12 or 13 characters for the file name, anything following that variable length filename will be put into the structure correctly? I'm not seeing how that will happen.
Don't worry about padding the filename. If you treat is as a string on both sides of the protocol, it will be null-terminated. It doesn't matter what is in the filename buffer after the null terminator.
But worse, I'm not sure I can send a null character or a string with a null termination from my program. I did figure out how to remove the padding, though.

DanPolka
 
Posts: 108
Joined: Tue Feb 18, 2014 5:31 am

Re: How test return from Serial.readBytes against file text?

Post by DanPolka »

I guess you're right! I found that I can send a null at end of filename, and that when I do, and use,

Code: Select all

Serial.print(headerBuf.fileName);
I do simply get the filename echoed by itself correctly.
That tells me I've sent it correctly, and received it such that I can use it.
Thanks!

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”