I noticed that your using C++ and not C looking at the message again.

On Wed, April 11, 2007 2:03 pm, Ben Buckner said:
>> I have trouble sending msg from laptop (using c++) code to tmote.  All
the
>> code is at the bottom. I have no problem reading the data from the serial
but sending it back dooesn't seam to work. Anyone knows why? Thanks. Indy
> [...]
>>     memcpy(output_buffer, &msgOUT, sizeof(msgOUT));
>
> Out of the dozens of things that could be wrong, it might be worth
pointing
> out that C does not guarantee that structures can be serialized by copying
bytewise out of memory like that. It can work, but you never know. The
most
> usual reason why that would fail is if the compiler tries to align the
members of the structure and inserts padding (which it almost always does
with odd-sized byte fields). At any rate, it would be a good idea to make
sure that the code really generates the byte stream that you think it's
generating.
>
> Ben B.

If you're using C++, you should not use "memcpy()"

The structures you define should actually be classes with their own defined
copy, either a function or an override of "="

Your code would then be:

    output_buffer = msgOUT;

and your "copy()" would do the copying of each member field. Usually C++
compilers are smart enough to create "=" as a copy function for you as just
a direct field to field copy of its members, however you ought to be
defining this yourself.

Also, I would use iostream functions to put data into your buffer. Adding
the members of your class one at a time to the buffer using iostream will
ensure you have no padding. Of course you do not want to use string
functions on the resulting character array either, the string functions will
abort on null. (0x00 in strings)

Many people have rewritten c-string handling to use a size index rather then
null termination, which is what you need to implement. Have a look at
other's code.

Don't use sizeof(). Your maximum is defined either in your makefile or as
TinyOS' default of 28 (29 on some implementations, but use 28 for
compatibility) for data[] plus the transmitted headers (from AM.h). Your
motes are rejecting any larger packets, and will look as if they aren't
receiving them.

Particularly: write(fd, pData, sizeof(msgOUT));
sizeof better return something under 36. --28(data) + 4(header) + 2(crc) +
2(sync bytes)-- and it doesn't appear you've escaped anything. That's all in
the Octave document.

Therefore, you need to know the length of your message before you copy it.
Keep track of it as you copy members into the output_buffer array.

It is also worth noting that if you are using AM.h to define your TOS_Msg,
you are trying to send members that are used for TinyOS' internal use.
Copying each member individually will leave you with only the ones you want.
TinyOS does that itself, but I have no idea what your C++ serial code is
doing.

Make sure you have the right message structure, and I believe that the UART
does not use CRC checks. Have you looked at the Octave document on packet
structure? There are other message parts I don't see in your code.

-- 
The difference between the right word and the almost right word is really a
large matter- it's the difference between a lightning bug and the lightning.
-Twain




_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to