7
« on: February 24, 2009, 02:21:53 PM »
Hi Mark, I continued to play around with UDP, I have now grouped all the ADC values along with some other variables into a char array and I'm sending out an updated packet every 50ms.
static char StrMsg[36]= "String Build - Error"; // If string is not built this messege will be sent.
#define OWN_TASK TASK_SUDP
#define UDP_BUFFER_SIZE 37 // buffer size for UDP test message
#define MY_UDP_PORT 1234
#define E_TIMER_NEXT_MESSAGE 1
typedef struct stUDP_MESSAGE
{
unsigned short usLength;
UDP_HEADER tUDP_Header; // reserve header space
unsigned char ucUDP_Message[UDP_BUFFER_SIZE]; // reserve message space
} UDP_MESSAGE;
static int fnUDPListner(USOCKET SocketNr, unsigned char ucEvent, unsigned char *ucIP, unsigned short usPortNr, unsigned char *data, unsigned short usLength)
{
return 0;
}
// <<<<<<<<<<<<<<<<<<< Send UDP Function >>>>>>>>>>>>>>>>
extern void fnSUDP(TTASKTABLE *ptrTaskTable)
{
static USOCKET MyUDP_Socket = -1;
static UDP_MESSAGE *ptrUDP_Frame;
const unsigned char ucIP[IPV4_LENGTH]= {192, 168, 0, 101};
QUEUE_HANDLE PortIDInternal = ptrTaskTable->TaskID; // queue ID for task input
unsigned char ucInputMessage[HEADER_LENGTH]; // reserve space for receiving messages
if (MyUDP_Socket < 0) { // only perform once
ptrUDP_Frame = uMalloc(sizeof(UDP_MESSAGE)); // get some memory for UDP frame
MyUDP_Socket = fnGetUDP_socket(TOS_MINIMISE_DELAY, fnUDPListner, (UDP_OPT_SEND_CS | UDP_OPT_CHECK_CS));
fnBindSocket(MyUDP_Socket, MY_UDP_PORT);
uMemcpy(&ptrUDP_Frame->ucUDP_Message,StrMsg, UDP_BUFFER_SIZE);
fnSendUDP(MyUDP_Socket, (unsigned char *)ucIP, MY_UDP_PORT, (unsigned char*)&ptrUDP_Frame->tUDP_Header, UDP_BUFFER_SIZE, OWN_TASK);
uTaskerMonoTimer( OWN_TASK, (DELAY_LIMIT)(0.05*SEC), E_TIMER_NEXT_MESSAGE );
}
while ( fnRead( PortIDInternal, ucInputMessage, HEADER_LENGTH )) { // check input queue
switch ( ucInputMessage[MSG_SOURCE_TASK] ) { // switch depending on message source
case TIMER_EVENT:
if (E_TIMER_NEXT_MESSAGE == ucInputMessage[MSG_TIMER_EVENT]) {
uMemcpy(&ptrUDP_Frame->ucUDP_Message,StrMsg, UDP_BUFFER_SIZE);
fnSendUDP(MyUDP_Socket, (unsigned char *)ucIP, MY_UDP_PORT, (unsigned char*)&ptrUDP_Frame->tUDP_Header, UDP_BUFFER_SIZE, OWN_TASK);
uTaskerMonoTimer( OWN_TASK, (DELAY_LIMIT)(0.05*SEC), E_TIMER_NEXT_MESSAGE );
}
break;
case TASK_ARP:
fnRead( PortIDInternal, ucInputMessage, ucInputMessage[MSG_CONTENT_LENGTH]); // read the contents
switch (ucInputMessage[ 0 ]) { // ARP sends us either ARP resolution success or failed
case ARP_RESOLUTION_SUCCESS: // IP address has been resolved (repeat UDP frame).
uMemcpy(&ptrUDP_Frame->ucUDP_Message,StrMsg, UDP_BUFFER_SIZE);
fnSendUDP(MyUDP_Socket, (unsigned char *)ucIP, MY_UDP_PORT, (unsigned char*)&ptrUDP_Frame->tUDP_Header, UDP_BUFFER_SIZE, OWN_TASK);
break;
case ARP_RESOLUTION_FAILED: // IP address could not be resolved...
break;
}
break;
}
}
}
I wrote a simple program, to convert an unsigned intiger into 2 chars, I'm sure there is a simpler way to do it in C.
// <<<<<<<<<<<<< Convert 2byte int to 2 chars >>>>>>>>>>>>>>>>>>
unsigned char IntToString(unsigned int InpInt, int ChrNum)
{
char OutChar[3];
char RetChar;
OutChar[1]=(char)(InpInt / 256);
OutChar[2] =(char)(InpInt%256);
RetChar = OutChar[ChrNum];
return((char)RetChar);
}
I then wrote a program which is on a timer from TaskConfig.h, which then combines all the chars into StrMsg.
extern void fnBuildPacket(TTASKTABLE *ptrTaskTable)
{
char StrStart[2]= "["; //Start of Data
char StrStop[2]= "]"; //Stop of Data
unsigned int intTPS;
unsigned int intSPS;
unsigned int intSpd;
unsigned int intRpm;
unsigned int intTmp;
unsigned int intPsi;
unsigned int intAlt;
unsigned int intOLS;
unsigned int intFLS;
unsigned int intTrm;
char chrRSK = 'R';
char chrFNR = 'N';
char chrCrC = 'F';
char chrSpL = 'F';
char chrSyC = 'T';
static int cntrA; // Counter needed for endcap placement on Packet
static int cntrB; // Counter needed for 2byte data segment placement on packet
static unsigned int cntrC = 0;
cntrC++; //Test Data used to check update of transmission.
intTPS = ATDDR0H; // ADC channel 0
intSPS = ATDDR1H; // ADC channel 1
intSpd = ATDDR2H; // ADC channel 2
intRpm = ATDDR3H; // ADC channel 3
intTmp = ATDDR4H; // ADC channel 4
intPsi = ATDDR5H; // ADC channel 5
intAlt = ATDDR6H; // ADC channel 6
intOLS = ATDDR7H; // ADC channel 7
intFLS = cntrC;
intTrm = cntrC;
// <<<<<<<<<<<<<<<<< MSG string Construction >>>>>>>>>>>>>>
// Place the endcaps on the string
for (cntrA = 0; cntrA <= 1; cntrA++)
{
StrMsg[cntrA] = StrStart[cntrA]; //Add the start Bracket
StrMsg[cntrA + 26] = StrStop[cntrA]; //Add the Stop Bracket
}
// Add all the 2 Byte data segments
for (cntrB = 0; cntrB <= 1; cntrB++)
{
StrMsg[cntrB + 1] = IntToString(intTPS, cntrB + 1);
StrMsg[cntrB + 3] = IntToString(intSPS, cntrB + 1);
StrMsg[cntrB + 5] = IntToString(intSpd, cntrB + 1);
StrMsg[cntrB + 7] = IntToString(intRpm, cntrB + 1);
StrMsg[cntrB + 9] = IntToString(intTmp, cntrB + 1);
StrMsg[cntrB + 11] = IntToString(intPsi, cntrB + 1);
StrMsg[cntrB + 13] = IntToString(intAlt, cntrB + 1);
StrMsg[cntrB + 15] = IntToString(intOLS, cntrB + 1);
StrMsg[cntrB + 17] = IntToString(intFLS, cntrB + 1);
StrMsg[cntrB + 19] = IntToString(intTrm, cntrB + 1);
}
// Add aditional Controlls
StrMsg[21] = chrRSK;
StrMsg[22] = chrFNR;
StrMsg[23] = chrCrC;
StrMsg[24] = chrSpL;
StrMsg[25] = chrSyC;
}
I just thought its a nice thing to play around with and mabe someone might find it usefull, I also uploaded my UDP test program for windows. Which makes use of winsock32