#include

#include

#include

#include

#pragma comment(lib,"ws2_32.lib")


HANDLE hCom=0;  //全局变量,串口句柄

char recv_ch[2]={0};

#define MAX_MESSAGE_LEN 100

char recv_str[MAX_MESSAGE_LEN];

char recv_cmd[10]={0};

char local_ip[20]={0};

char send_str[MAX_MESSAGE_LEN];

int debug_flag=0;



int myprintf(const char *format, ... )

{

if(debug_flag)

{

va_list args;

va_start(args, format);

vprintf(format, args);  

va_end(args);  

}


return 0;

}

int getlocaladdress()

{

WSADATA wsaData;

char name[155];

char *ip;

PHOSTENT hostinfo;

if ( WSAStartup(MAKEWORD(2,0), &wsaData ) == 0 )

{

if( gethostname(name, sizeof(name)) == 0)

{

if((hostinfo = gethostbyname(name)) != NULL)

{

ip = inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);

memcpy(local_ip,ip,sizeof(ip)*8);

myprintf("local ip address:%s\n",local_ip);

}

}

else

{

return -1;

}


WSACleanup( );

}

else

{

return -1;

}


return 0;

}


int getmessage()

{

int i;

BOOL bReadStat;

DWORD wCount;//读取的字节数


if(!hCom)

{

myprintf("the com2 fd is 0\n");

}


myprintf("geting the command from com2...\n");


PurgeComm(hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);


for(i=0;i

{

bReadStat=ReadFile(hCom,recv_ch,1,&wCount,NULL);

recv_ch[1]='\0';


if(!bReadStat)

{

myprintf("read the com2 error\n");

continue;

}


if(recv_ch[0] == '\n' || recv_ch[0] == ' ' || recv_ch[0] == '\0' || recv_ch[0] == '\t')

{

myprintf("nothing to get yet\n");

break;

}


myprintf("%c\n",recv_ch[0]);

recv_str[i] = recv_ch[0];

memset(recv_ch,0,sizeof(recv_ch));

}


myprintf("the command from com2 is: %s\n",recv_str);


return 0;

}


int parsemessage()

{

int i;

int j=0;

int flag = 0;


for(i=0;i

{

if(recv_str[i] == ':')

{

flag = 1;

continue;

}


if(recv_str[i] == '\n')

{

break;

}


if(flag)

{

recv_cmd[j++] = recv_str[i];

}

}


myprintf("parse the command: %s\n",recv_cmd);


return 0;

}


int responsemessag()

{

DWORD dwBytesWrite;

BOOL bWriteStat;

int len;


if(!hCom)

{

myprintf("the com2 fd is 0\n");

}


if(memcmp("ip",recv_cmd,2) == 0)

{

memcpy(send_str,"put:",4);

strcat(send_str,recv_cmd);

strcat(send_str, ":");


if(getlocaladdress() == 0)

{

strcat(send_str,local_ip);

}

else

{

myprintf("get local ip addr error\n");

return -1;

}


len = strlen(send_str);

send_str[len]= '\0';


myprintf("sending the data(%s) to com2\n",send_str);


PurgeComm(hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);


bWriteStat=WriteFile(hCom,send_str,len+1,&dwBytesWrite,NULL);

if(!bWriteStat)

{

myprintf("send message to com2 error\n");

}

else

{

myprintf("send message to com2 success,total send len: %d(byte)\n",dwBytesWrite);

}

}

else

{

myprintf("the command is not ip \n");

}


memset(recv_str,0,sizeof(recv_str));

memset(recv_cmd,0,sizeof(recv_cmd));

memset(send_str,0,sizeof(send_str));


return 0;

}


int main(int argc, char **argv)

{

if( argc == 2)

{

debug_flag = 1;

}


hCom =CreateFile("COM2",GENERIC_READ|GENERIC_WRITE, 0, NULL,OPEN_EXISTING, 0, NULL);

if(hCom ==INVALID_HANDLE_VALUE)

{

myprintf("open the com1 error\n");

return -1;

}


while(1)

{

getmessage();

parsemessage();

responsemessag();

}


return 0;

}