Mercurial > hg > beaglert
changeset 28:adcb57fd3d75 bbb_network
- added a c script to have the oscillator perform a sine sweep
- added documentation for the network
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sun, 10 May 2015 13:55:43 +0100 |
parents | d358d4410d01 |
children | cc434ecf4486 |
files | resources/network/network_readme.txt resources/network/udp-client-sweep.c resources/network/udp-client.c resources/network/udp-server.c resources/udp-client.c resources/udp-server.c |
diffstat | 6 files changed, 189 insertions(+), 106 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/network/network_readme.txt Sun May 10 13:55:43 2015 +0100 @@ -0,0 +1,24 @@ +Three simple c scripts can be used to test the network, in conjunction with the basic_network project: +udp-client.c, udp-client-sweep.c,d udp-server.c +compile them with gcc -o udp-client udp-client.c and similar +They can be compiled and run from either the host or the bbb + +usage +./udp-client 192.168.7.2 9998 #sends individual messages to the bbb on port 9998 +#enter desired values for frequency (and phase). +123.0;0; + +The parser is currently very stupid, so the format of the message has to be:: +frequencyValue;phaseValue; +example: +700.0;0.1; + +./udp-client-sweep 192.168.7.2 9998 +sends messages every 1ms to control the frequency of the oscillator, thus generating a sine sweep + +./udp-server 9999 +#will print the info received from BeagleRT with the following format +printf("%8d;%.3f;%.3;",gCounter,gFrequency,gPhase); +example: +88201;700.000;0.123; +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/network/udp-client-sweep.c Sun May 10 13:55:43 2015 +0100 @@ -0,0 +1,59 @@ +/* UDP client in the internet domain */ +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +void error(const char *); +int main(int argc, char *argv[]) +{ + int sock, n; + unsigned int length; + struct sockaddr_in server, from; + struct hostent *hp; + char buffer[256]; + + if (argc != 3) { printf("Usage: server port\n"); + exit(1); + } + + server.sin_family = AF_INET; + hp = gethostbyname(argv[1]); + if (hp==0) error("Unknown host"); + + bcopy((char *)hp->h_addr, + (char *)&server.sin_addr, + hp->h_length); + server.sin_port = htons(atoi(argv[2])); + length=sizeof(struct sockaddr_in); + while (1){ + sock= socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) error("socket"); + bzero(buffer,256); +// printf("Please enter the message: "); +// fgets(buffer,255,stdin); + double freq=50; + while(1){ + freq*=1.001; + if(freq>20000) freq=50; + sprintf(buffer,"%.4f;",freq); + n=sendto(sock,buffer, + strlen(buffer),0,(const struct sockaddr *)&server,length); + if (n < 0) error("Sendto"); + usleep(1000); + } + } + close(sock); + return 0; +} + +void error(const char *msg) +{ + perror(msg); + exit(0); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/network/udp-client.c Sun May 10 13:55:43 2015 +0100 @@ -0,0 +1,52 @@ +/* UDP client in the internet domain */ +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +void error(const char *); +int main(int argc, char *argv[]) +{ + int sock, n; + unsigned int length; + struct sockaddr_in server, from; + struct hostent *hp; + char buffer[256]; + + if (argc != 3) { printf("Usage: server port\n"); + exit(1); + } + + server.sin_family = AF_INET; + hp = gethostbyname(argv[1]); + if (hp==0) error("Unknown host"); + + bcopy((char *)hp->h_addr, + (char *)&server.sin_addr, + hp->h_length); + server.sin_port = htons(atoi(argv[2])); + length=sizeof(struct sockaddr_in); + while (1){ + sock= socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) error("socket"); + bzero(buffer,256); + printf("Please enter the message: "); + fgets(buffer,255,stdin); + n=sendto(sock,buffer, + strlen(buffer),0,(const struct sockaddr *)&server,length); + if (n < 0) error("Sendto"); + } + close(sock); + return 0; +} + +void error(const char *msg) +{ + perror(msg); + exit(0); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/network/udp-server.c Sun May 10 13:55:43 2015 +0100 @@ -0,0 +1,54 @@ +/* Creates a datagram server. The port + number is passed as an argument. This + server runs forever */ + +#include <sys/types.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <string.h> +#include <netdb.h> +#include <stdio.h> + +void error(const char *msg) +{ + perror(msg); + exit(0); +} + +int main(int argc, char *argv[]) +{ + int sock, length, n; + socklen_t fromlen; + struct sockaddr_in server; + struct sockaddr_in from; + char buf[1024]; + + if (argc < 2) { + fprintf(stderr, "ERROR, no port provided\n"); + exit(0); + } + + sock=socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) error("Opening socket"); + length = sizeof(server); + bzero(&server,length); + server.sin_family=AF_INET; + server.sin_addr.s_addr=INADDR_ANY; + server.sin_port=htons(atoi(argv[1])); + if (bind(sock,(struct sockaddr *)&server,length)<0) + error("binding"); + fromlen = sizeof(struct sockaddr_in); + while (1) { + n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen); + if (n < 0) error("recvfrom"); + write(1,"Received a datagram: ",21); + write(1,buf,n); + n = sendto(sock,"Got your message\n",17, + 0,(struct sockaddr *)&from,fromlen); + if (n < 0) error("sendto"); + } + return 0; + } +
--- a/resources/udp-client.c Sun May 10 02:01:45 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -/* UDP client in the internet domain */ -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netdb.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> - -void error(const char *); -int main(int argc, char *argv[]) -{ - int sock, n; - unsigned int length; - struct sockaddr_in server, from; - struct hostent *hp; - char buffer[256]; - - if (argc != 3) { printf("Usage: server port\n"); - exit(1); - } - - server.sin_family = AF_INET; - hp = gethostbyname(argv[1]); - if (hp==0) error("Unknown host"); - - bcopy((char *)hp->h_addr, - (char *)&server.sin_addr, - hp->h_length); - server.sin_port = htons(atoi(argv[2])); - length=sizeof(struct sockaddr_in); - while (1){ - sock= socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) error("socket"); - bzero(buffer,256); - printf("Please enter the message: "); - fgets(buffer,255,stdin); - n=sendto(sock,buffer, - strlen(buffer),0,(const struct sockaddr *)&server,length); - if (n < 0) error("Sendto"); - } - close(sock); - return 0; -} - -void error(const char *msg) -{ - perror(msg); - exit(0); -}
--- a/resources/udp-server.c Sun May 10 02:01:45 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -/* Creates a datagram server. The port - number is passed as an argument. This - server runs forever */ - -#include <sys/types.h> -#include <stdlib.h> -#include <unistd.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <string.h> -#include <netdb.h> -#include <stdio.h> - -void error(const char *msg) -{ - perror(msg); - exit(0); -} - -int main(int argc, char *argv[]) -{ - int sock, length, n; - socklen_t fromlen; - struct sockaddr_in server; - struct sockaddr_in from; - char buf[1024]; - - if (argc < 2) { - fprintf(stderr, "ERROR, no port provided\n"); - exit(0); - } - - sock=socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) error("Opening socket"); - length = sizeof(server); - bzero(&server,length); - server.sin_family=AF_INET; - server.sin_addr.s_addr=INADDR_ANY; - server.sin_port=htons(atoi(argv[1])); - if (bind(sock,(struct sockaddr *)&server,length)<0) - error("binding"); - fromlen = sizeof(struct sockaddr_in); - while (1) { - n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen); - if (n < 0) error("recvfrom"); - write(1,"Received a datagram: ",21); - write(1,buf,n); - n = sendto(sock,"Got your message\n",17, - 0,(struct sockaddr *)&from,fromlen); - if (n < 0) error("sendto"); - } - return 0; - } -