|
-
February 16th, 2008, 06:29 PM
#1
need help with socket prgrming in C on a microcontroller
Hi,
I have to create a socket program in C which could send and recieve data from 2 nodes .I'm wondering if any one could advice me what microcontrollers i could use ?
-
February 18th, 2008, 07:14 AM
#2
Re: need help with socket prgrming in C on a microcontroller
That mainly depends on the size and frequency of this "data" and what the uC is supposed to do with it? Speed/calculation power needed.
In any case have a look at FreeRTOS, lwIP and uIP (Googling should get you a lot of info on these).
-
February 18th, 2008, 12:33 PM
#3
Re: need help with socket prgrming in C on a microcontroller
Actually i need to send voice signals . i was looking at some products at Compulab but those are very expensive !..
-
February 18th, 2008, 01:41 PM
#4
Re: need help with socket prgrming in C on a microcontroller
Try the PIC18F Microchip family. They have a free TCP/IP stack.
http://www.microchip.com/stellent/id...GE&nodeId=1489
-
February 18th, 2008, 03:37 PM
#5
Re: need help with socket prgrming in C on a microcontroller
If you need to process signals in real-time then that should be your starting point. You need to select a controller that is powerfull enough to do the calculations while handling the network.
Thanks to free TCP/IP stacks like uIP and lwIP you can add networking to any controller. On the FreeRTOS website you'll find many ports already available for AVR, PIC, 8051 and ARM core based controllers.
Also important is of course if you want to be able to solder the controller yourself (is it available in a DIL package?) or do you want to use a commercial development kit (does one exist with everything you need ?) If you are going to use a completely build PCB of your own design then this does not matter of course, but a reference design with most of you requirements in place would be helpful then.
-
February 23rd, 2008, 02:56 PM
#6
Re: need help with socket prgrming in C on a microcontroller
Hi, Actually i have some experience in C but got no experience with micro-controllers and network programming .I am all confused as to where and how to start .If anyone could help me get started then i will catch up .
What i have to do is : I have to send data(voice) from one node (say a laptop) to another node and be able to playback the file .Later i want to convert it into a real-time application .
Can someone please help me with some basic code to get started ??? I would really appreciate any help.....
thanks,
-
February 27th, 2008, 07:49 AM
#7
Re: need help with socket prgrming in C on a microcontroller
It is unfortunately not that straight-forward with micro controllers. Yes, you can program them in C, but there are so many differences between different architectures that all we could give you is some pseudo code with a lot of blanks like <hereth be thei nethworking sendth>.
The best thing to do would be to try and decide on an architecture on beforehand depending on what your main calculation needs are. Most architectures like ARM, AVR and PIC have a large family of different products attached to them so that you can still select more ROM/RAM/IO if you need to.
When you've decided on a family then see if you can find some tutorials on programming these and have a good look at these.
-
February 27th, 2008, 10:42 AM
#8
Re: need help with socket prgrming in C
Thanks Edders ..it makes sense .
K right now to get started and have a feel of it what i'm doing is that using 2 Unix systems :i've got some code from Beej's Guide to network programming trying to understand how it works .got a lot out of that website !
Issue : i want to Broadcast an audio file to my listeners : which i can record and save .now here is some changes that i've made into the exsisting code but it seems like i have to convert the file into binary first and then convert it back to playback .Not sure how to do that.Please see if anyone could help me do that .
Code : Broadcasting file.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SERVERPORT 4950 // the port users will be connecting to
int main(int argc,char *argv[])
{
int sockfd;
struct sockaddr_in their_addr; // connector's address information
struct hostent *he;
int numbytes;
int broadcast = 1;
FILE *fp1;
int infile[50000]; //filesize
int *mypointer
char c;
int i = 0;
mypointer = infile;
if (argc != 3) {
fprintf(stderr,"usage: broadcaster hostname message\n");
exit(1);
}
if ((he = gethostbyname(argv[1])) == NULL) { // get the host info
herror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast,
sizeof broadcast) == -1) {
perror("setsockopt (SO_BROADCAST)");
exit(1);
}
fp1 = fopen(argv[2],"r");
while(( c = getc(fp1)) != EOF)
{
infile[i] = c;
i = i+1;
};
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(SERVERPORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
if ((numbytes=sendto(sockfd, mypointer, i, 0,
(struct sockaddr *)&their_addr, sizeof their_addr)) == -1) {
perror("sendto");
exit(1);
}
printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
close(sockfd);
return 0;
}
-------------------------------------------------------------------------------------
Code :listening file
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MYPORT 4950 // the port users will be connecting to
#define MAXBUFLEN 50000
int main(void)
{
FILE *out;
int sockfd;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
char buf[MAXBUFLEN];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
out = fopen("../thanga/xxxx.c","a");//save in a file
fprintf(out,"got packet from %s\n",inet_ntoa(their_addr.sin_addr));
fprintf(out,"packet is %d bytes long\n",numbytes);
buf[numbytes] = '\0';
fprintf(out,"packet contains \"%s\"\n",buf);
fclose(out);mohan
close(sockfd);
return 0;
}
-------------------------------------------------------------------------
it listens the contents from broadcasting file and saves it in a file xxxx.c but when sending and audio file it recives some garbage value....
Please advice
Thanks...
Last edited by phantom3008; February 27th, 2008 at 10:47 AM.
-
February 27th, 2008, 11:20 AM
#9
Re: need help with socket prgrming in C on a microcontroller
Testing with datagrams is indeed the simplest, please remember that you may not receive all the UDP packets that you have sent, you may receive them in a different order and you may even receive the same packet a multiple of times. On a local network this should be happening less frequent (if at all) so should not really interfere with your test.
Why are you setting the SO_BROADCAST option?
The best way to go to send the data is to open the file as a binary (with something like the fopen() function) and then read a small bit (like 1200 bytes, it is usually best to stay below 1500) with fread() and sendto() to send the data - continue to read() and sendto() until read() signals an EOF. Then close the file and the socket.
On receiving end you do the opposite - use fopen() to open your binary writable file and recvfrom() to receive the small packets and fwrite() to save them to the file. Again continue to readfrom() and fwrite(0 until no more data is received and close the file and the socket.
You can make the sender signal the end of the file by ensuring that all packets have the same size apart from the last packet.
Hope this makes sense?
-
February 27th, 2008, 12:59 PM
#10
Re: need help with socket prgrming in C on a microcontroller
Would that be able to playback the audio file ? coz i need to send an audio ..
-
February 27th, 2008, 05:43 PM
#11
Re: need help with socket prgrming in C on a microcontroller
Well, the only thing that happens is that a binary file is sent to the receiver. This does not include anything to do with the actual playing of audio itself. But the file created by the reciever should be bit-for-bit the same as the file read by the sending application.
-
February 28th, 2008, 06:50 PM
#12
Re: need help with socket prgrming in C on a microcontroller
Hey if you could provide me with some code to do that ,it wud be awesome .coz i'm new to this so what i'm trying to do is to try n get some codes integrate them accordingly and make alterations ..
-
March 1st, 2008, 10:41 AM
#13
Re: need help with socket prgrming in C on a microcontroller
Can anyone help me out here ?????
-
March 3rd, 2008, 08:03 AM
#14
Re: need help with socket prgrming in C on a microcontroller
Sorry for the late reply - I'm very busy at the moment I'm afraid. Since you are currently looking into testing on unix based systems you may want to start a new thread here at the network forum? This thread is titled "C on a microcontroller" - not many of the unix guru's may therefore visit this thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|