[Contiki-developers] send a value

137 views
Skip to first unread message

hamma hamma

unread,
Apr 2, 2010, 2:54:13 PM4/2/10
to contiki-d...@lists.sourceforge.net
hi all,
I want to send a value using the broadcast module so I try to build on the examples broadcast but an error appear when i try to send an integer value. my question is how to post an integer value in the buffer. i change (int *)&packetbuf_dataptr()
 packetbuf_copyfrom (value, 6);
thanks all

Nicolas Tsiftes

unread,
Apr 2, 2010, 3:42:36 PM4/2/10
to Contiki developer mailing list
hamma hamma skrev:
The sender can copy the integer directly into the packetbuf. Preferably,
htons() or an equivalent transformation should be used before copying in
order to ensure interoperability, but I assume that you test this in a
homogeneous environment.

void
broadcast_an_integer(struct broadcast_conn *bc_conn, int x)
{
packetbuf_copyfrom(&x, sizeof(x));
broadcast_send(bc_conn);
}


The receiver can then access it like this:

static void
broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from)
{
if(packetbuf_datalen() >= sizeof(int)) {
printf("broadcast message received from %d.%d: %d\n",
from->u8[0], from->u8[1], *(int *)packetbuf_dataptr());
} else {
printf("invalid broadcast size: %d\n", packetbuf_datalen());
}
}

Nicolas


------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Contiki-developers mailing list
Contiki-d...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/contiki-developers

Marek Bykowski

unread,
Apr 3, 2010, 6:17:02 AM4/3/10
to Contiki developer mailing list
Hi Nicolas and Hamma,

I was struggeling with a similar problem. It is indeed a walk round you suggested but can you tell me why the below code is not working. It's taking temp readings and sending them over unicast to the receiver.


struct datastruct{
   uint16_t temp;
   //unit16_t humidity;
   //more
};

transmitter part
==================================
struct datastruct *msg;
packetbuf_clear();
msg = (struct datastruct *) packetbuf_dataptr();
packetbuf_set_datalen(sizeof(msg)); 
msg->temp = sht11_temp();

addr.u8[0] = 157;
addr.u8[1] = 212;

//check if we are not sedning to ourself
if(!rimeaddr_cmp(&addr, &rimeaddr_node_addr)) {
          //if not dispatch the packet
          unicast_send(&uc, &addr);
}  

receiver part
====================================
static void
recv(struct unicast_conn *c, const rimeaddr_t *from)
{
   //define temp pointer
   struct datastruct *msg;
   msg = (struct datastruct *) packetbuf_dataptr();
   printf("Example2: unicast packet came from %d.%d\nwith the temp sensed %u\n\n ",

           from->u8[0], from->u8[1],
           msg->temp);
}
static const struct unicast_callbacks uni_call = { recv };

besides what is the difference between rimebuf and packetbuf? They are the same functions and seem to be doing the same stuff.

Cheers a million,
Marek

Kasun Hewage

unread,
Apr 5, 2010, 10:10:54 PM4/5/10
to Contiki developer mailing list
Hello Marek,

On Sat, Apr 3, 2010 at 3:47 PM, Marek Bykowski wrote:
 
struct datastruct *msg;
packetbuf_clear();
msg = (struct datastruct *) packetbuf_dataptr();
packetbuf_set_datalen(sizeof(msg)); 
msg->temp = sht11_temp();


In your code, "sizeof(msg)" says the size of a pointer on your platform(Typically, 2 bytes on most of the 8-bit and 16-bt microprocessors).  You need to use "sizeof(struct datastruct)" instead of "sizeof(msg)".
 

besides what is the difference between rimebuf and packetbuf? They are the same functions and seem to be doing the same stuff.


rimebuf is the old name. Now it's no longer being used.

--
Best Regards,
Kasun Hewage
කසුන් හේවගේ


Borms Joris

unread,
Apr 6, 2010, 5:46:43 AM4/6/10
to Contiki developer mailing list

I’m not sure what the problem could be, since you do not give a lot of details about the problem except for the code of your application. However, the statement packetbuf_set_datalen(sizeof(msg));  probably doesn’t result in what you want to happen. “msg” is a pointer to  datastruct, so “sizeof(msg)” will give you the size of the pointer (which is generally 2 on 16-bit platforms) instead of the size of the data structure. In the example below it seems to look like this might still work since your data structure only contains one 16-bit value, so there’s probably other issues as well.

Marek Bykowski

unread,
Apr 7, 2010, 3:51:46 AM4/7/10
to joris...@vub.ac.be, Contiki developer mailing list
Hello All,

Thanks for your reply Borms. I'd say so that sizeof... is not an issue in this case. As I changed it and it still doesn't work.
The thing I want to do is to send a value using a unicast transmission.

I use and modified an example code in the contki-2.4/example/rime/example-unicast.c. So instead of char I want to send a value on. And indeed besides many tries, changes of the code it is still not working.

Though an example sky-collect.c under contiki-2.4/example/sky/ that uses the same code in terms of sending data over is working. I'm really confused at the moment.

Thanks,
Marek

Nicolas Tsiftes

unread,
Apr 14, 2010, 5:24:24 PM4/14/10
to Contiki developer mailing list
Marek Bykowski skrev:

> Hello All,
>
> Thanks for your reply Borms. I'd say so that sizeof... is not an issue
> in this case. As I changed it and it still doesn't work.
> The thing I want to do is to send a value using a unicast transmission.
>
> I use and modified an example code in the
> contki-2.4/example/rime/example-unicast.c. So instead of char I want
> to send a value on. And indeed besides many tries, changes of the code
> it is still not working.
>
> Though an example sky-collect.c under contiki-2.4/example/sky/ that
> uses the same code in terms of sending data over is working. I'm
> really confused at the moment.
>
> Thanks,
> Marek
>

Did you try the method that I wrote about earlier in the thread, i.e.,
copying from and to a local variable rather than accessing the value
through a casted pointer to packetbuf_dataptr()? There is no guarantee
that the first byte pointed to by packetbuf_dataptr() is aligned.

Marek Bykowski

unread,
Apr 14, 2010, 8:53:02 PM4/14/10
to Contiki developer mailing list
Hello Nicolas,

Yes I tried that as well. I think that one of both of my tmote sky nodes I used for testing are buggy. I tried the same code in Cooja and there everything works alright. Although I haven't tested the code on other tmote sky nodes yet but really hoping that this was an issue.

Marek

arda...@gmail.com

unread,
Apr 26, 2014, 6:16:12 PM4/26/14
to osdeve_mirror_rtos...@googlegroups.com, Contiki developer mailing list, marek.b...@gmail.com
Your sky motes are not buggy. I have the same problem with z1 motes. It works fine for data type uint8_t however, uint16_t creates an error. If you only use the "int" data type, some byte shifting occurs. In any case it works fine with cooja. please post here if you can find a solution.

arda...@gmail.com

unread,
Apr 28, 2014, 11:47:29 AM4/28/14
to osdeve_mirror_rtos...@googlegroups.com, Contiki developer mailing list, marek.b...@gmail.com, arda...@gmail.com
It works fine if you convert it to string and then transmit. It solved my problem.
Reply all
Reply to author
Forward
0 new messages