November 9, 2008

Make Controller network communication

For our project in Computer Architecture, we are programing several Make controllers to work together as a home control system. As the programing manager, apposed to the general manager, and part of the system controller group, I have been trying to compile function and a basic API together for the other teams to drop in and use. Here is the easiest way to get networking working on the make controller, using Datagram Sockets. I must say, I hate how small the code is compare to the 7+ hours of my own time and another 2+ hours working with others it took to get working...

Drop these functions into the code...



int sendMessage(int num, int address, int port)
{
struct netconn* udpSocket = DatagramSocket( port );
int val = DatagramSocketSend( udpSocket, address, port, &num, 4 );
DatagramSocketClose( udpSocket );
return val;
}

int recieveMessage(int address, int port)
{
struct netconn* udpSocket = DatagramSocket( port );
int num = 0;
DatagramSocketReceive( udpSocket, port, &address, &port, &num, 4 );
DatagramSocketClose( udpSocket );
return num;
}


API information...


int sendMessage(int num, int address, int port)


Sends out an integer over a specified socket on the network to a network device based on its IP.

Parameters:

num: The integer to send.
address: The IP address of the network device that will be receiving the packets.
port: The port that the network device will be listening on.

Returns:

This function returns an int returned by DatagramSocketSend, which gives the number of bytes sent.

Example:

int address = IP_ADDRESS( 192, 168, 0, 210 );
int num = 1;
int port = 12345;
sendMessage(num, address, port);



int recieveMessage(int address, int port)


Receives data from the network buffer and stores it for use. Please Note: This function sits and waits for data to be written to the socket buffer before continuing, so it best used in a separate task.

Parameters:

address: The IP address of the network device that will be sending the packets.
port: The port that the network device will sending packets on.

Returns:

This function returns an integer that was written using the sendMessage function.

Example:


int address = IP_ADDRESS( 192, 168, 0, 200 );
int port = 12345;
int num = recieveMessage(address,port);

No comments: