Sign in

username or email:

password:



Not a member?
Forgot your Password?

Search rabbit-semi



Search tips

Subscribe to rabbit-semi



Discussion Groups

See Also

DSPFPGA

Discussion Groups | Rabbit-Semi | Help with UDP data handler

This is a group for folks designing and programming embedded systems using the Rabbit Semiconductor C-programmable microcontroller. This group is not affiliated with either Rabbit or Z-World, but is a user forum for sharing ideas, asking questions, flaunting knowledge, and other typical user group stuff. The Rabbit is a powerful uC, supported by a full-featured C-compiler.


So far in May, you have voted 0 times ou of a total of 20 votes by the community.
Please help us clean the archives from unuseful discussion threads by using the voting system! Details here.


Is this thread worth a thumbs up?

+2

Help with UDP data handler - Jeff Powell - Jul 30 11:57:28 2012

I posted this as part of a different question and nobody answered so I
will try again as it's own topic.

I have opened a udp socket with a datahandler.
I opened a second udp socket for sending multicast data when values change.
I get a connection, read from client, write back to client.
I get another connection. It does not go to the datahandler.
The main loop runs and multicast data continues to send properly.

What am I missing?

-- code --
if (!udp_open(&tsock, LOCAL_PORT, 0, 0, dataHandler))
{
printf("udp_lst_open failed!\n");
exit(0);
}
else
{
printf("Listener %s:%d\n",MY_IP_ADDRESS,LOCAL_PORT);
}
if (!udp_open(&usock, MBUS_PORT, resolve(MBUS_ADDRESS), MBUS_PORT,
NULL))
{
printf("udp_open failed!\n");
exit(0);
}

...

while (1)
{
idleProcessing(); // where values are processed and
multicast data is sent as needed.
tcp_tick(NULL);
}

...

int dataHandler(int event, udp_Socket *s, ll_Gather *g,
_udp_datagram_info *udi)
{
int status;
char cip[20];
CARDMSG_S *pkt;
if(handleUdp) // stops recursion, does not effect
handler death
return 1;
handleUdp=1;
if(event == UDP_DH_ICMPMSG) // from the example
{
return 1;
}
memset(&replyMsg, 0, sizeof (REPLYMSG_S));
xmem2root(pktbuf, g->data2, g->len2);
if(g->len3>0)
xmem2root(pktbuf+g->len2, g->data3, g->len3);
pkt=(CARDMSG_S*) &pktbuf;
inet_ntoa( cip, udi->remip);
printf("%s:%u Slot %d Type %d Device %d Offset %x Width %x Data %lx
%lx\n", cip, udi->remport, pkt->cSlot, pkt->cOperation, pkt->cDevice,
pkt->cOffset, pkt->cDataWidth, pkt->sData.longs[1], pkt->sData.longs[0] );
status = process_packet(pktbuf);
replyMsg.eStatus = status;
printf("Status: %x\n", status);
udp_sendto(s, (char*) &replyMsg, sizeof(REPLYMSG_S), udi->remip,
udi->remport); // straight from the
// even tried this but it does not help either. Returns 1 for NULL or
bytes recv'd for the incoming socket.
printf("Sent response %u\n", tcp_tick(NULL));
handleUdp=0;
return 1;
}





(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )

Re: Help with UDP data handler - Steve Trigero - Jul 31 13:30:13 2012

I can't tell you what you might doing wrong, but I can offer
some suggestions based on how I did something similar.
I did not use the callback feature. It seems to me there are
too many messages flying around to use this feature reliably.
 
I open several UDP sockets listening for broadcast messages.
 
 for(i=0;i    udp_extopen(&udpcon[i].socket,
                 IF_DEFAULT,
                 UDP_BROADCAST_PORT,
                 -1,-1,NULL,0,0);
 }

 
In the main loop I call a function that checks each socket for
a received message. If a message with data is found, the data
is copied to a buffer and passed to a paring routine.
 
// Look for incoming packets. for( s=0;s UdpParse( &udpcon[s]);
}
 
Occasionally, my program sends its own broadcast status.
When it does, it opens a UDP port, sends the message, then
closes the port.
 
// Open a Broadcast socket if( !udp_extopen( &udpsock.socket, IF_DEFAULT, UDP_BROADCAST_PORT-1, -1, UDP_BROADCAST_PORT, NULL, 0, 0) ) return;// Oops! // Build a status message FormatUDPStatusMsg( udpsock.buf); // Send it... udp_send( &udpsock.socket, udpsock.buf, strlen(udpsock.buf) ); udp_close( &udpsock.socket);
 
This works well in our system.
 
 
 
>From: Jeff Powell
>To: r...
>Sent: Monday, July 30, 2012 8:57 AM
>Subject: [rabbit-semi] Help with UDP data handler
> 
>I posted this as part of a different question and nobody answered so I
>will try again as it's own topic.
>
>I have opened a udp socket with a datahandler.
>I opened a second udp socket for sending multicast data when values change.
>I get a connection, read from client, write back to client.
>I get another connection. It does not go to the datahandler.
>The main loop runs and multicast data continues to send properly.
>
>What am I missing?
>
>-- code --
>if (!udp_open(&tsock, LOCAL_PORT, 0, 0, dataHandler))
>{
>printf("udp_lst_open failed!\n");
>exit(0);
>}
>else
>{
>printf("Listener %s:%d\n",MY_IP_ADDRESS,LOCAL_PORT);
>}
>if (!udp_open(&usock, MBUS_PORT, resolve(MBUS_ADDRESS), MBUS_PORT,
>NULL))
>{
>printf("udp_open failed!\n");
>exit(0);
>}
>
>...
>
>while (1)
>{
>idleProcessing(); // where values are processed and
>multicast data is sent as needed.
>tcp_tick(NULL);
>}
>
>...
>
>int dataHandler(int event, udp_Socket *s, ll_Gather *g,
>_udp_datagram_info *udi)
>{
>int status;
>char cip[20];
>CARDMSG_S *pkt;
>if(handleUdp) // stops recursion, does not effect
>handler death
>return 1;
>handleUdp=1;
>if(event == UDP_DH_ICMPMSG) // from the example
>{
>return 1;
>}
>memset(&replyMsg, 0, sizeof (REPLYMSG_S));
>xmem2root(pktbuf, g->data2, g->len2);
>if(g->len3>0)
>xmem2root(pktbuf+g->len2, g->data3, g->len3);
>pkt=(CARDMSG_S*) &pktbuf;
>inet_ntoa( cip, udi->remip);
>printf("%s:%u Slot %d Type %d Device %d Offset %x Width %x Data %lx
>%lx\n", cip, udi->remport, pkt->cSlot, pkt->cOperation, pkt->cDevice,
>pkt->cOffset, pkt->cDataWidth, pkt->sData.longs[1], pkt->sData.longs[0] );
>status = process_packet(pktbuf);
>replyMsg.eStatus = status;
>printf("Status: %x\n", status);
>udp_sendto(s, (char*) &replyMsg, sizeof(REPLYMSG_S), udi->remip,
>udi->remport); // straight from the
>// even tried this but it does not help either. Returns 1 for NULL or
>bytes recv'd for the incoming socket.
>printf("Sent response %u\n", tcp_tick(NULL));
>handleUdp=0;
>return 1;
>}
>


(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )

Re: Help with UDP data handler - Jeff Powell - Jul 31 17:01:44 2012

Thanks that's a good idea. I may deploy this approach in my application.
Could you tell me how you are getting the remote IP and PORT for the
recvfrom call?

Thanks.

On 07/31/2012 12:29 PM, Steve Trigero wrote:
> I can't tell you what you might doing wrong, but I can offer
> some suggestions based on how I did something similar.
> I did not use the callback feature. It seems to me there are
> too many messages flying around to use this feature reliably.
> I open several UDP sockets listening for broadcast messages.
> for(i=0;i > udp_extopen(&udpcon[i].socket,
> IF_DEFAULT,
> UDP_BROADCAST_PORT,
> -1,-1,NULL,0,0);
> }
> In the main loop I call a function that checks each socket for
> a received message. If a message with data is found, the data
> is copied to a buffer and passed to a paring routine.
> // Look for incoming packets.
> for (s=0; s >
> if (!udp_peek(&udpcon[s].socket,NULL ) ){
> continue; // No packets...
> }
>
> numBytes = udp_recvfrom(&udpcon[s].socket,
> udpcon[s].buf,
> sizeof(udpcon[s].buf),
> &udpcon[s].ip,
> &udpcon[s].port );
> if (numBytes < 1 ){
> continue; // No data...
> }
> udpcon[s].buf[numBytes] = NUL;
> UdpParse(&udpcon[s] );
> }
> Occasionally, my program sends its own broadcast status.
> When it does, it opens a UDP port, sends the message, then
> closes the port.
> // Open a Broadcast socket
> if (!udp_extopen(&udpsock.socket,IF_DEFAULT,UDP_BROADCAST_PORT-1,
> -1,UDP_BROADCAST_PORT,NULL,0,0 ) )
> return; // Oops!
>
> // Build a status message
> FormatUDPStatusMsg(udpsock.buf );
>
> // Send it...
> udp_send(&udpsock.socket,udpsock.buf,strlen(udpsock.buf) );
> udp_close(&udpsock.socket );
> This works well in our system.
>
> *From:* Jeff Powell
> *To:* r...
> *Sent:* Monday, July 30, 2012 8:57 AM
> *Subject:* [rabbit-semi] Help with UDP data handler
>
> I posted this as part of a different question and nobody answered
> so I
> will try again as it's own topic.
>
> I have opened a udp socket with a datahandler.
> I opened a second udp socket for sending multicast data when
> values change.
> I get a connection, read from client, write back to client.
> I get another connection. It does not go to the datahandler.
> The main loop runs and multicast data continues to send properly.
>
> What am I missing?
>
> -- code --
> if (!udp_open(&tsock, LOCAL_PORT, 0, 0, dataHandler))
> {
> printf("udp_lst_open failed!\n");
> exit(0);
> }
> else
> {
> printf("Listener %s:%d\n",MY_IP_ADDRESS,LOCAL_PORT);
> }
> if (!udp_open(&usock, MBUS_PORT, resolve(MBUS_ADDRESS), MBUS_PORT,
> NULL))
> {
> printf("udp_open failed!\n");
> exit(0);
> }
>
> ...
>
> while (1)
> {
> idleProcessing(); // where values are processed and
> multicast data is sent as needed.
> tcp_tick(NULL);
> }
>
> ...
>
> int dataHandler(int event, udp_Socket *s, ll_Gather *g,
> _udp_datagram_info *udi)
> {
> int status;
> char cip[20];
> CARDMSG_S *pkt;
> if(handleUdp) // stops recursion, does not effect
> handler death
> return 1;
> handleUdp=1;
> if(event == UDP_DH_ICMPMSG) // from the example
> {
> return 1;
> }
> memset(&replyMsg, 0, sizeof (REPLYMSG_S));
> xmem2root(pktbuf, g->data2, g->len2);
> if(g->len3>0)
> xmem2root(pktbuf+g->len2, g->data3, g->len3);
> pkt=(CARDMSG_S*) &pktbuf;
> inet_ntoa( cip, udi->remip);
> printf("%s:%u Slot %d Type %d Device %d Offset %x Width %x Data %lx
> %lx\n", cip, udi->remport, pkt->cSlot, pkt->cOperation, pkt->cDevice,
> pkt->cOffset, pkt->cDataWidth, pkt->sData.longs[1],
> pkt->sData.longs[0] );
> status = process_packet(pktbuf);
> replyMsg.eStatus = status;
> printf("Status: %x\n", status);
> udp_sendto(s, (char*) &replyMsg, sizeof(REPLYMSG_S), udi->remip,
> udi->remport); // straight from the
> // even tried this but it does not help either. Returns 1 for NULL or
> bytes recv'd for the incoming socket.
> printf("Sent response %u\n", tcp_tick(NULL));
> handleUdp=0;
> return 1;
> }


(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )

Re: Help with UDP data handler - Steve Trigero - Jul 31 19:35:31 2012

The function udp_recvfrom() stuffs the ip and port values from the received datagram
into the ip and port function arguments. That way, if the message requires a response
I can respond to the specific ip and port of the host device.
 
steve

From: Jeff Powell
>To: r...
>Sent: Tuesday, July 31, 2012 2:01 PM
>Subject: Re: [rabbit-semi] Help with UDP data handler
> 
>Thanks that's a good idea. I may deploy this approach in my application. Could you tell me how you are getting the remote IP and PORT for the recvfrom call?
>
>Thanks.
>On 07/31/2012 12:29 PM, Steve Trigero wrote:
>
> 
>>I can't tell you what you might doing wrong, but I can offer
>>some suggestions based on how I did something similar.
>>I did not use the callback feature. It seems to me there are
>>too many messages flying around to use this feature reliably.
>> 
>>I open several UDP sockets listening for broadcast messages.
>> 
>> for(i=0;i >>   udp_extopen(&udpcon[i].socket,
>>                 IF_DEFAULT,
>>                 UDP_BROADCAST_PORT,
>>                 -1,-1,NULL,0,0);
>> }
>>In the main loop I call a function that checks each socket for
>>a received message. If a message with data is found, the data
>>is copied to a buffer and passed to a paring routine.
>>
>>// Look for incoming packets. for( s=0;s >>UdpParse( &udpcon[s]);
>>}
>>
>>Occasionally, my program sends its own broadcast status.
>>When it does, it opens a UDP port, sends the message, then
>>closes the port.
>>
>>// Open a Broadcast socket if( !udp_extopen( &udpsock.socket, IF_DEFAULT, UDP_BROADCAST_PORT-1, -1, UDP_BROADCAST_PORT, NULL, 0, 0) ) return;// Oops! // Build a status message FormatUDPStatusMsg( udpsock.buf); // Send it... udp_send( &udpsock.socket, udpsock.buf, strlen(udpsock.buf) ); udp_close( &udpsock.socket);
>>
>>This works well in our system.
>>
>>>
>>>From: Jeff Powell mailto:j...@yahoo.com
>>>To: r...
>>>Sent: Monday, July 30, 2012 8:57 AM
>>>Subject: [rabbit-semi] Help with UDP data handler
>>>
>>>
>>> 
>>>I posted this as part of a different question and nobody answered so I
>>>will try again as it's own topic.
>>>
>>>I have opened a udp socket with a datahandler.
>>>I opened a second udp socket for sending multicast data when values change.
>>>I get a connection, read from client, write back to client.
>>>I get another connection. It does not go to the datahandler.
>>>The main loop runs and multicast data continues to send properly.
>>>
>>>What am I missing?
>>>
>>>-- code --
>>>if (!udp_open(&tsock, LOCAL_PORT, 0, 0, dataHandler))
>>>{
>>>printf("udp_lst_open failed!\n");
>>>exit(0);
>>>}
>>>else
>>>{
>>>printf("Listener %s:%d\n",MY_IP_ADDRESS,LOCAL_PORT);
>>>}
>>>if (!udp_open(&usock, MBUS_PORT, resolve(MBUS_ADDRESS), MBUS_PORT,
>>>NULL))
>>>{
>>>printf("udp_open failed!\n");
>>>exit(0);
>>>}
>>>
>>>...
>>>
>>>while (1)
>>>{
>>>idleProcessing(); // where values are processed and
>>>multicast data is sent as needed.
>>>tcp_tick(NULL);
>>>}
>>>
>>>...
>>>
>>>int dataHandler(int event, udp_Socket *s, ll_Gather *g,
>>>_udp_datagram_info *udi)
>>>{
>>>int status;
>>>char cip[20];
>>>CARDMSG_S *pkt;
>>>if(handleUdp) // stops recursion, does not effect
>>>handler death
>>>return 1;
>>>handleUdp=1;
>>>if(event == UDP_DH_ICMPMSG) // from the example
>>>{
>>>return 1;
>>>}
>>>memset(&replyMsg, 0, sizeof (REPLYMSG_S));
>>>xmem2root(pktbuf, g->data2, g->len2);
>>>if(g->len3>0)
>>>xmem2root(pktbuf+g->len2, g->data3, g->len3);
>>>pkt=(CARDMSG_S*) &pktbuf;
>>>inet_ntoa( cip, udi->remip);
>>>printf("%s:%u Slot %d Type %d Device %d Offset %x Width %x Data %lx
>>>%lx\n", cip, udi->remport, pkt->cSlot, pkt->cOperation, pkt->cDevice,
>>>pkt->cOffset, pkt->cDataWidth, pkt->sData.longs[1], pkt->sData.longs[0] );
>>>status = process_packet(pktbuf);
>>>replyMsg.eStatus = status;
>>>printf("Status: %x\n", status);
>>>udp_sendto(s, (char*) &replyMsg, sizeof(REPLYMSG_S), udi->remip,
>>>udi->remport); // straight from the
>>>// even tried this but it does not help either. Returns 1 for NULL or
>>>bytes recv'd for the incoming socket.
>>>printf("Sent response %u\n", tcp_tick(NULL));
>>>handleUdp=0;
>>>return 1;
>>>}
>>>
>>>
>>>
>>>


(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )