我正在尝试将我的wifi连接发送到reciver进行实验。我设法使用Windows API Native for wifi将卡连接到reciver。
我将我的卡的GUID存储在pIfInfo指针(PWLAN_INTERFACE_INFO)中并解压缩以创建应该允许我在连接上写入的句柄(WifiAcess)。
然后,如果我运行这个,我会在envois之前使用和不使用(void *)转换在WriteFile函数上得到错误ERROR_INVALID_PARAMETER。
嗯,我的问题是,我不知道哪个参数无效
DWORD bytesSend;
char *buffer ;
unsigned int nbChar ;
要发送的数据
int headerData[4] = {85,0,11,0};
int ServosData[6] = {100,100,0,100,0,0};
BYTE * envois = new BYTE[11];
envois[0] = headerData[0] ;
envois[1] = headerData[1] ;
envois[2] = headerData[2] ;
envois[3] = headerData[3] ;
envois[4] = ServosData[0] ;
envois[5] = ServosData[1] ;
envois[6] = ServosData[2] ;
envois[7] = ServosData[3] ;
envois[8] = ServosData[4] ;
envois[9] = ServosData[5] ;
检查汇总
for(int i = 0 ; i<10;i++)
{
envois[10] = envois[10] + envois[i] ;
}
envois[10] = envois[10]%256 ;
nbChar = 11 ;
HANDLE WifiAcess = INVALID_HANDLE_VALUE;
LPCSTR AcessGUID ;
char GuidString2Char[46] ;
char DefChar = ' ';
bool sendtOk = false ;
这里我们从wifi连接获取我们的Wlan卡的GUID
sprintf(GuidString2Char, "\\\\.\\{%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}",
pIfInfo->InterfaceGuid.Data1,
pIfInfo->InterfaceGuid.Data2,
pIfInfo->InterfaceGuid.Data3,
pIfInfo->InterfaceGuid.Data4[0],
pIfInfo->InterfaceGuid.Data4[1],
pIfInfo->InterfaceGuid.Data4[2],
pIfInfo->InterfaceGuid.Data4[3],
pIfInfo->InterfaceGuid.Data4[4],
pIfInfo->InterfaceGuid.Data4[5],
pIfInfo->InterfaceGuid.Data4[6],
pIfInfo->InterfaceGuid.Data4[7]);
将已翻译的GUID放入LPCSTR
AcessGUID = GuidString2Char ;
然后创建句柄
WifiAcess = CreateFile(
AcessGUID ,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
(HANDLE)INVALID_HANDLE_VALUE); // NULL
检查错误
if(WifiAcess == INVALID_HANDLE_VALUE)
{
dwResult = GetLastError();
switch(dwResult)
{
case ERROR_FILE_NOT_FOUND :
wprintf(L"\t\t\CreateFile ERROR_FILE_NOT_FOUND\n");
break ;
default :
wprintf(L"\t\t\CreateFile default\n");
break;
}
}
else { wprintf(L"\t\t\CreateFile Success\n"); }
尝试在WifiAcess Handle上编写缓冲区
sendtOk = WriteFile(WifiAcess, envois, nbChar, &bytesSend, NULL) ;
if(!sendtOk)
{
wprintf(L"\n\t\t\tEcheque de l envois : %d bytes envoyes\n\n", bytesSend);
dwResult = GetLastError();
switch(dwResult)
{
/////////////////////////////////////////////// ////////////////////////////////////////////////// ///
case ERROR_INVALID_PARAMETER :
wprintf(L"\t\t\WriteFile ERROR_INVALID_PARAMETER\n");
break ;
/////////////////////////////////////////////// ////////////////////////////////////////////////// ///
case S_FALSE :
wprintf(L"\t\t\WriteFile S_FALSE\n");
break ;
case E_UNEXPECTED :
wprintf(L"\t\t\WriteFile E_UNEXPECTED\n");
break ;
default :
wprintf(L"\t\t\WriteFile default with dword : 0x%x \n", dwResult);
break;
}
}else
{
wprintf(L"Data Sendt\n\n");
}
答案 0 :(得分:0)
好的,我发现错误,不能使用串口com的类比,而是使用了winsock类:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms738545(v=vs.85).aspx
=============================================== ================================================== ==== 初始化:
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// ======================================================================================================
// Création du Socket :
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_INET ;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
#define DEFAULT_PORT "27015"
// Resolve the server address and port
iResult = getaddrinfo("10.10.100.254","8899" , &hints, &result); //"10.10.100.254" DEFAULT_PORT "8899"
if (iResult == 0) {
printf("\n\t\tgetaddrinfo, adresse trouvee\n");
struct addrinfo *res = NULL;
int error;
for (res = result; res != NULL; res = res->ai_next)
{
char hostname[NI_MAXHOST] = "";
error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0);
if (error != 0)
{
fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
continue;
}
if (*hostname != '\0')
{
printf("hostname: %s\n", hostname);
ptr=res;
}
}
}else
{
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
SOCKET ConnectSocket = INVALID_SOCKET;
// Attempt to connect to the first address returned by
// the call to getaddrinfo
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// ======================================================================================================
// Connect to a socket :
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
switch(WSAGetLastError())
{
case WSAEHOSTUNREACH:
wprintf(L"\n\t\tNo route to host. A socket operation was attempted to an unreachable host. See WSAENETUNREACH.\n");
break;
default:
break;
}
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
}
// Should really try the next address returned by getaddrinfo
// if the connect call failed
// But for this simple example we just free the resources
// returned by getaddrinfo and print an error message
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
// ======================================================================================================
// Send and recive data :
#define DEFAULT_BUFLEN 512
int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "this is a test";
//char recvbuf[DEFAULT_BUFLEN];
// Send an initial buffer
for(int i = 0; i<100;i++)
{
if(sendCommand(ConnectSocket,i, i, 0, i)==-1) break;
Sleep(10);
}
// Disconnecting the Client : http://msdn.microsoft.com/en-us/library/windows/desktop/bb530743(v=vs.85).aspx
// shutdown the send half of the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ConnectSocket);
WSACleanup();
这对我来说很有效率
答案 1 :(得分:0)
您切换到Windows套接字看起来不错。我唯一的建议是,如果您需要跨版本兼容性,添加一些回退逻辑,因为WSAStartup无法找到您要求的2.2。
其他建议是风格化的,主要是通过整合所有案例的清理代码来清理您对WSACleanup的使用 - 例如使用SEH / leave或goto fail case。