- 积分
- 19
贡献43
飞刀0 FD
注册时间2014-12-9
在线时间7 小时
扫一扫,手机访问本帖
|
本人手里有四套飞凌OK6410开发板,想要实现设备控制功能,例如:用一台开发板控制其他三台开发板挂起等操作;
目前已经实现点对点的TCP、套接字通信,即两个开发板之间的互相通信,想问问群里的大神,我下一步想先实现一台wince控制另一台wince,只要实现挂起等简单功能即可,一下是初步实现两个板子通过tcp 套接字通信的代码,请问再此基础之上要怎么修改,希望各位朋友给我点指导,谢谢:
只给了tcp.cpp的代码,tcp.h的就是几个函数声明,都是飞凌教程的例子,如果有想看的朋友告诉我,我再贴上:
#include <windows.h>
#include <windowsx.h>
#include <CommCtrl.h>
#include "tcp.h"
#include <winuser.h>
#include <WinSock.h>
struct DecodeUnit DlgMessage[] ={
WM_INITDIALOG ,DoInit,
WM_COMMAND,DoCommand,
};
struct DecodeUnit CommandMessage[] = {
IDOK,OnClose,
IDCANCEL,OnClose,
IDC_BTNREC ,OnRec,
IDC_BTNSEND,OnSend,
};
HINSTANCE g_hInst;
HANDLE hThread ;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DlgPorc);
return (0);
}
INT_PTR CALLBACK DlgPorc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
int i;
for (i = 0;i < dim(DlgMessage);i++)
{
if(DlgMessage[i].message == message)
(*DlgMessage[i].fun)(hDlg,message,wParam,lParam);
}
return FALSE;
}
LONG DoCommand(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
int i ;
for(i =0 ;i< dim(CommandMessage);++i)
{
if(CommandMessage[i].message == LOWORD(wParam))
(*CommandMessage[i].fun)(hDlg,message,wParam,lParam);
}
return TRUE;
}
LONG DoInit(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
WORD wVer = MAKEWORD(2,2);
WSADATA wsData;
WSAStartup(wVer,&wsData);
Edit_SetText(GetDlgItem(hDlg,IDC_EDIT1),TEXT("this is tcp test"));
return TRUE;
}
LONG OnClose(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
CloseHandle(hThread);
WSACleanup();
EndDialog(hDlg,0);
return TRUE;
}
LONG OnSend(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
SOCKET sClient = socket(AF_INET,SOCK_STREAM,0);
if (sClient == SOCKET_ERROR)
{
return TRUE;
}
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.S_un.S_addr =inet_addr("192.168.0.211");
addr.sin_port = htons(6000);
connect(sClient,(sockaddr *)&addr,sizeof(SOCKADDR_IN));
TCHAR buf[64];
Edit_GetText(GetDlgItem(hDlg,IDC_EDIT1),buf,64);
char ascbuf[128];
WideCharToMultiByte(CP_ACP,0,buf,wcslen(buf),ascbuf,128,NULL,NULL);
send(sClient,ascbuf,strlen(ascbuf)+1,0);
closesocket(sClient);
return TRUE;
}
DWORD WINAPI ThreadProc(LPVOID pArg)
{
HWND hList = (HWND)pArg;
SOCKET sServer = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN adds;
adds.sin_family = AF_INET;
adds.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
adds.sin_port = htons(6000);
bind(sServer,(SOCKADDR *)& adds,sizeof(SOCKADDR_IN));
listen(sServer,5);
SOCKADDR_IN addc;
int nSize = sizeof(addc);
while(TRUE)
{
SOCKET cSocket = accept(sServer,(SOCKADDR *)&addc,&nSize);
char buf[128];
recv(cSocket,buf,128,0);
TCHAR tbuf[64];
MultiByteToWideChar(CP_ACP,0,buf,strlen(buf)+1,tbuf,128);
ListBox_AddString(hList,tbuf);
closesocket(cSocket);
}
closesocket(sServer);
}
LONG OnRec(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
HWND hList = GetDlgItem(hDlg,IDC_LIST1);
DWORD dwId;
hThread = CreateThread(NULL,0,ThreadProc,(LPVOID)hList,0,&dwId);
return TRUE;
}
有明白的朋友可以直接修改,再次谢谢大家;
|
|