C语言实的串行通信接口程序
ndows Sockets DLL,并发出终止应用程序的消息;在接到消息UM-SOCK时,它根据参数lParam指示的网络事件,进行相应的操作,然后选择下一个期望的网络事件。
程序3:部分Windows程序源代码(主窗口函数)
long FAR PASCAL
ClientProc(HWND hWnd, unsigned message, UINT wParam, LONG lParam)
{
int length,i;
WSADATAwsaData;/*描述Windows Sockets实现细节的数据结构*/
intStatus;
switch (message) {
case WM-USER:
Status=WSAStartup(0x101,&wsaData);
if (Status !=0) {
AlertUser(hWnd,"WSAStartup() failed");
PostQuitMessage(0);
}
if (LOBYTE(wsaData.wVersion) !=1 || HIBYTE(wsaData.wVersion) !=1)
{ /*现在支持的版本是WINSOCK.DLL 1.1*/
AlertUser(hWnd, "WSAStartup() Version not match");
WSACleanup();
PostQuitMessage(0);
}
hostaddr=gethostbyname(server-address);
if (hostaddr==NULL) {
AlertUser(hWnd, "gethostbyname ERROR ");
WSACleanup ();
PostQuitMessage(0);
}
memcpy(&hostnm,hostaddr,sizeof(struct hostent));
break;
case WM-COMMAND:
switch (wParam) {
case IDM-START:
if (!Client(hWnd)) {
closesocket(s);
AlertUser(hWnd, "Start Failed");
}
break;
case IDM-
WSACleanup();
PostQuitMessage(0);
break;
}
break;
case UM-SOCK:
switch (lParam) {
case FD-CONNECT:/*网络事件:连接建立*/
if (!set-select(hWnd, FD-WRITE))/*选择:期望发送*/
closesocket(s);
break;
case FD-READ:/*网络事件:读准备好*/
if (!receive-pkt(hWnd)) {/*接收数据*/
AlertUser(hWnd, "Receive Packet Failed");
closesocket(s);
break;
}
if (!set-select(hWnd, FD-WRITE))/*选择:期望发送*/
closesocket(s);
break;
case FD-WRITE:/*网络事件:写准备好*/
for (i=0;i<1024;i++)
buffer=(char) 'A'+i % 26;
length=1024;
if (!(send-pkt(hWnd,length))) {/*发送数据*/
AlertUser(hWnd, "Packet Send Failed");
closesocket(s);
break;
}
if (!set-select(hWnd, FD-READ)) /*选择:期望接收*/
closesocket(s);
break;
case FD-CLOSE:/*网络事件:连接关闭。操作:停止异步选择*/
if (WSAAsyncSelect(s,hWnd,0,0)==SOCKET-ERROR)
AlertUser(hWnd, "WSAAsyncSelect Failed");
AlertUser(hWnd, "Socket has been closed");
break;
default:/*网络出错则警告,其他事件忽略*/
if (WSAGETSELECTERROR(1Param) !=0) {
AlertUser(hWnd, "Socket Report Failure");
closesocket(s);
break;
}
break;
}
break;
case WM-DESTROY:
closesocket(s);/*关闭窗口前应该关闭套接字,并*/
WSACleanup();/*终止Windows Sockets DLL*/
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hWnd, message,
wParam, lParam));
}
return (NULL);
}
程序4:部分Windows程序源代码(子程序)
BOOL Client(HWND hWnd)/*客户机子程序*/
{
《C语言实的串行通信接口程序(第3页)》
本文链接地址:http://www.oyaya.net/fanwen/view/171929.html
程序3:部分Windows程序源代码(主窗口函数)
long FAR PASCAL
ClientProc(HWND hWnd, unsigned message, UINT wParam, LONG lParam)
{
int length,i;
WSADATAwsaData;/*描述Windows Sockets实现细节的数据结构*/
intStatus;
switch (message) {
case WM-USER:
Status=WSAStartup(0x101,&wsaData);
if (Status !=0) {
AlertUser(hWnd,"WSAStartup() failed");
PostQuitMessage(0);
}
if (LOBYTE(wsaData.wVersion) !=1 || HIBYTE(wsaData.wVersion) !=1)
{ /*现在支持的版本是WINSOCK.DLL 1.1*/
AlertUser(hWnd, "WSAStartup() Version not match");
WSACleanup();
PostQuitMessage(0);
}
hostaddr=gethostbyname(server-address);
if (hostaddr==NULL) {
AlertUser(hWnd, "gethostbyname ERROR ");
WSACleanup ();
PostQuitMessage(0);
}
memcpy(&hostnm,hostaddr,sizeof(struct hostent));
break;
case WM-COMMAND:
switch (wParam) {
case IDM-START:
if (!Client(hWnd)) {
closesocket(s);
AlertUser(hWnd, "Start Failed");
}
break;
case IDM-
STOP:
WSACleanup();
PostQuitMessage(0);
break;
}
break;
case UM-SOCK:
switch (lParam) {
case FD-CONNECT:/*网络事件:连接建立*/
if (!set-select(hWnd, FD-WRITE))/*选择:期望发送*/
closesocket(s);
break;
case FD-READ:/*网络事件:读准备好*/
if (!receive-pkt(hWnd)) {/*接收数据*/
AlertUser(hWnd, "Receive Packet Failed");
closesocket(s);
break;
}
if (!set-select(hWnd, FD-WRITE))/*选择:期望发送*/
closesocket(s);
break;
case FD-WRITE:/*网络事件:写准备好*/
for (i=0;i<1024;i++)
buffer=(char) 'A'+i % 26;
length=1024;
if (!(send-pkt(hWnd,length))) {/*发送数据*/
AlertUser(hWnd, "Packet Send Failed");
closesocket(s);
break;
}
if (!set-select(hWnd, FD-READ)) /*选择:期望接收*/
closesocket(s);
break;
case FD-CLOSE:/*网络事件:连接关闭。操作:停止异步选择*/
if (WSAAsyncSelect(s,hWnd,0,0)==SOCKET-ERROR)
AlertUser(hWnd, "WSAAsyncSelect Failed");
AlertUser(hWnd, "Socket has been closed");
break;
default:/*网络出错则警告,其他事件忽略*/
if (WSAGETSELECTERROR(1Param) !=0) {
AlertUser(hWnd, "Socket Report Failure");
closesocket(s);
break;
}
break;
}
break;
case WM-DESTROY:
closesocket(s);/*关闭窗口前应该关闭套接字,并*/
WSACleanup();/*终止Windows Sockets DLL*/
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hWnd, message,
wParam, lParam));
}
return (NULL);
}
程序4:部分Windows程序源代码(子程序)
BOOL Client(HWND hWnd)/*客户机子程序*/
{
《C语言实的串行通信接口程序(第3页)》