各位高手帮忙看看这个程序,万分感谢我现在在做一个关于8*8矩阵面板按键控制的C语言编程,8输入8输出,任何一个输入可以
选择一个或多个输出,有8个数字按键和“确定”,“end","and"三个功能按键,比如说我
要1通道输入2,3通道输出,我就按 1”确定”2 "AND" 3 "END",最终将信号传
送给芯片AD8109处理,用到AD8109的CLK,DATAIN,UPDATA三个管脚,现在程序写好了,可是
还有点问题,麻烦各位高手给看看,修改修改,不胜感激!
#define USERPORT 3333/*用户定义端口号*/
#define IDM-START101/*“启动”菜单项标志*/
#define IDM-EXIT102/*“退出”菜单项标志*/
#define UM-SOCKWM-USER+0x100/*用户定义网络消息*/
ClientMenu MENU/*客户机菜单*/
BEGIN
POPUP \"&Server\"
BEGIN
MENUITEM \"&Start...\", IDM-START
MENUITEM \"&Stop\",IDM-STOP
END
END
#include <winsock.h> /*必须包含winsock.h头文件*/
HANDLEhInst;
charserver-address = {0};/*地址缓冲区*/
charbuffer;/*接收发送缓冲区*/
char FAR *lpBuffer=&buffer;
SOCKETs=0;/*套接字*/
struct sockaddr-in dst-addr;/*目标地址*/
struct hostent *hostaddr;/*主机地址*/
struct hostent hostnm;
intcount=0;/*发送接收循环计数器*/
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lp
CmdL
ine, int nCmdShow)
{
HWND hWnd;
MSGmsg;
lstrcpy((LPSTR) server-address, lpCmdLine);/*取主机名字*/
if (!hPrevInstance)
if (!InitApplication(hInstance))
return (FALSE);
hInst=hInstance;
hWnd=CreateWindow(\"ClientClass\",\"Windows ECHO Client\",
WS-OVERLAPPEDWINDOW,CW-USEDEFAULT,CW-USEDEFAULT,
CW-USEDEFAULT,CW-USEDEFAULT,
NULL,NULL,hInstance,NULL);
if (!hWnd)
return (FALSE);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
/*给主窗口函数发送WM-USER消息*/
PostMessage(hWnd,WM-USER,(WPARAM) 0,(LPARAM) 0);
while (GetMessage(&msg,NULL,NULL,NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
long FAR PASCAL
ClientProc(HWND hWnd, unsigned message, UINT wParam, LONG lParam)
{
int length,i;
WSADATAwsaData;/*描述实现细节的数据结构*/
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)
{ 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)/*客户机子程序*/
{
if (!make-skt(hWnd))/*建立套接字*/
return(FALSE);
if (!set-Select(hWnd,FD-CONNECT))/*设置异步连接*/
return(FALSE);
if (!connect-skt(hWnd))/*建立连接*/
return(FALSE);
return(TRUE);
}
BOOL receive-pkt(HWND hWnd)/*接收数据子程序*/
{
HDC dc;
intlength;
int11,12,13;
charlinel,line2,line3;
count ++;/*循环计数器加1*/
if ((length=recv(s,lpBuffer,1024,0))==SOCKET-ERROR)
return(FALSE); /*如果接收数据出错,则返回FALSE*/
if (length==0) /*接收数据长度为零,表示连接中断*/
return(FALSE);
if (dc=GetDC(hWnd)) { /*接收数据成功,显示信息*/
11=wsprintf((LPSTR) line1,\"TCP Echo Client No.%d\",count);
12=wsprintf((LPSTR) line2,\"Received %d bytes\", length);
13=wsprintf((LPSTR) line3,\"Those are:%c,%c,%c,%c,%c,%c\",
buffer,buffer,buffer,buffer,buffer,buffer
);
TextOut(dc, 10, 2, (LPSTR) linel, 11);
TextOut(dc, 10, 22, (LPSTR) line2, 12);
TextOut(dc, 10, 42, (LPSTR) line3, 13);
ReleaseDC(hWnd, dc);
}
return(TRUE);
}
BOOL set-Select(HWND hWnd, long lEvent)/*异步选择子程序*/
{
if (WSAAsyncSelect(s,hWnd, UM-SOCK, lEvent)==SOCKET-ERROR) {
AlertUser(hWnd, \"WSAAsyncSelect Failed\");
return (FALSE);
}
return (TRUE);
}
BOOL make-skt(HWND hWnd)/*建立套接字子程序*/
{
if ((s=socket(AF-INET,sock-type,0))==INVALID-SOCKET) {
AlertUser(hWnd, \"Socket Failed\");
return (FALSE);
}
return (TRUE);
}
BOOL connect-skt(HWND hWnd)/*建立连接子程序*/
{
memset((void*) &dst-addr, sizeof(dst-addr),0);
dst-addr.sin-family=AF-INET;
dst-addr.sin-port=htons(USERPORT);
dst-addr.sin-addr.s-addr=*((unsigned long *)hostnm.h-addr-list );
if (connect(s, (struct sockaddr *) & dst-addr,
sizeof(dst-addr))==SOCKET-ERROR) {
AlertUser(hWnd, \"Connect Failed\");
return (FALSE);
}
return (TRUE);
}
BOOL send-pkt(HWND hWnd, int len)/*发送数据子程序*/
{
int length;
if ((length=send(s,lpBuffer,len,0))==SOCKET-ERROR)
return (FALSE);
else if (length !=len) {
AlertUser(hWnd, \"Send Length NOT Match!\");
return(FALSE);
}
return (TRUE);
}
/*TCP/IP必要的头文件*/
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define USERPORT 3333/*用户定义端口号,与客户机相同*/
#define HOST-IP-ADDR \"166.111.8.80\"/*我们的主机地址*/
main(int argc, char **argv)
{
char buf;/*buffer for sending and receiving data*/
struct sockaddr-in client;/*client address information*/
struct sockaddr-in server;/*server address information*/
int s;/*socket for accepting connections*/
int ns;/*socket connected to client*/
int namelen;/*length of client name*/
int pktlen;/*length of packet received or sended*/
if ((s = socket(AF-INET,SOCK-STREAM, 0)) <0) {
perror(\"Socket()\");
return;
}
/*Bind the socket to the server address.*/
bzero((char*)&server, sizeof(server));
server.sin-len =sizeof(struct sockaddr-in);
server.sin-family=AF-INET;
server.sin-port =htons(USERPORT);
server.sin-addr.s-addr=INADDR-ANY;
if (bind(s, (struct sockaddr *)&server, sizeof(server)) <0) {
perror (\"Bind()\");
return;
}
/*Listen for connections. Specify the backlog as 1. */
if (listen(s,1)!=0) {
perror(\"Listen()\");
return;
}
/*Accept a connection.*/
namelen=sizeof(client);
if ((ns = accept(s, (struct sockaddr *)&client,&namelen))==-1) {
perror(\"Accept()\");
return;
}
/*Receive the message on the newly connected socket.*/
for (;;){
if ((pktlen = recv(ns, buf, 1024, 0)) <0) {
perror(\"Recv()\");
break;
}
else if (pktlen==0) {
printf(\"Recv():return FAILED,connection is shut down! \");
break;
}
else printf(\"Recv():return SUCCESS, packet length = %d \",pktlen);
sleep(1); /*Sleep() 1秒钟是为了减慢数据交换速度*/
/*Send the message back to the client.*/
if (send(ns, buf, pktlen, 0) <0) {
perror(\"Send ()\");
break;
}
else printf(\"Send():return SUCCESS, packet length = %d \",pktlen);
}
close(ns);
close(s);
printf(\"Server ended successfully\");
}
#include "dos.h"
#include "stdlib.h"
#include "stdio.h"
#define PORT 0
void SendFile(char fname);
void Send(int s);
void SendFileName(char fname);
void ReceiveFile();
void GetFileName(char f);
void InitPort(int port,unsigned char para);
/ *初始化端口*/
void SendPort(int port,char c);
/ *端口发送*/
int ReadPort(int port);
/ *读端口字节*/
int CheckState(int port);
/ *检查端口状态*/
int Receive(int port,int G);
/ *接收一个字节*/
main(argc,argv)
int argc;
char *argv[];
{
if(argc <2){
printf("Please input R(receive)
or S(sent) parametre:");
exit(1);
}
InitPort(PORT,231);
if(argv[1]=='S')
/ *检查选择的有效性*/
SendFile(argv[2]);
else if(argv[1]=='R')
ReceiveFile();
else{
printf("Error parament.Please input again.");
exit(1);
}
}
void SendFile(fname)
char *fname;
{
FILE *fp;
int ch,s;
if(!(fp=fopen(fname,"rb"))){
printf("Can't open the file.\n");
exit(1);
}
SendFileName(fname);
do{
ch=(int)getc(fp);
if(ferror(fp)){
printf("Error reading file.\n");
break;
}
s=ch %16;
Send(s);
s=ch/16;
Send(s);
}while(!feof(fp));
s=46; / *发送文件结束信息*/
Send(s);
Send(s);
fclose(fp);
}
void Send(s)
int s;
{
int G;
SendPort(PORT,s);
G=ReadPort(PORT); / *等待信号*/
if(s!=G)
s=s +16;
do{
SendPort(PORT,s);
G=ReadPort(PORT); / *等待信号*/
}while(s!=G);
}
void SendFileName(fname)
char *fname;
{
int s,ch;
printf("Now transmit the file.Please wait...");
while( *fname){
ch=(int)fname ++;
s=ch %16;
/ *取文件名中一个字节的低4 位*/
Send(s);
s=ch/16;
Send(s);
/ *取文件名中一个字节的低4 位*/
}
s=32;/ *发送文件名结束标志*/
Send(s);
Send(s);
}
void ReceiveFile(){
FILE *fp;
char ch;
int G1,G2,G3;
char fname[15];
GetFileName(fname);
printf("Receiving file %s.\n",fname);
remove(fname);
if(!(fp=fopen(fname,"wb"))){
printf("Can't open output file.\n");
exit(1);
}
/ *循环为检测每次接受的数据是否为新数据,如果不是,则用此次接收的数据覆盖
上次接收的数据*/
G1=ReadPort(PORT);
G2=Receive(PORT, &G1);
do{
G3=Receive(PORT, &G2);
ch=(char)(G1 %16 +G2 *16);
/ *恢复分开的数据,
组合高4 位和低4 位*/
putc(ch,fp);
if(ferror(fp)){
printf("\nError writing file.");
exit(1);
}
G2=Receive(PORT, &G3);
G1=G3;
}while(G1/16!=48);
printf("\nTransmit finished.");
fclose(fp);
}
int Receive(port,G)
int port, *G;
{
int GM;
SendPort(port, *G);
GM=ReadPort(port);
if(GM/16==0)
return GM;
else if(GM/16==1){
do{
*G=GM;
SendPort(port,GM);
GM=ReadPort(port);
}while(GM/16==1);
}
return GM;
}
void GetFileName(f)
char *f;
{
int G1,G2,G3;
char ch;
G1=ReadPort(PORT);
G2=ReadPort(PORT);
do{
G3=Receive(PORT, &G3);
ch=(char)(G1 %16 +G2/16);
*f=ch;
*f ++;
G2=Receive(PORT, &G3);
G1=G3;
}while(G1/16!=32);
printf("File name transmit finished.\n");
}
void InitPort(port,para)
int port;
unsigned char para;
{
union REGS reg;
reg.x.dx=port;
reg.h.ah=0;
reg.h.al=para;
int86(0x14, &reg, &reg);
}
void SendPort(port,c)
int port;
char c;
{
union REGS reg;
reg.x.dx=port;
reg.h.al=c;
reg.h.ah=1;
int86(0x14, &reg, &reg);
if(reg.h.ah &128){
printf("\nSend mistakes!");
exit(1);
}
}
int ReadPort(port)
int port;
{
union REGS reg;
while(!(CheckState(port) &256)){
if(kbhit()){ / *如端口长期
无数据可人为终止等待*/
printf("Press any key to exit.");
getch();
exit(1);
}
}
reg.x.dx=port;
reg.h.ah=2;
int86(0x14, &reg, &reg);
if(reg.h.ah &128){
printf("\nRead mistake!");
exit(1);
}
return reg.h.al;
}
int CheckState(port)
int port;
{
union REGS reg;
reg.x.dx=port;
reg.h.ah=3;
int86(0x14, &reg, &reg);
return reg.x.ax;
}