已结贴√
问题点数:20 回复次数:2
求助。为什么用API 做的一个按钮控件只有第一次按有效,按第一次
#include
#include
#include
#include
#include
#define IDB_RELAY_ON 101
#define IDB_RELAY_OFF 102
HWND hStatic_channel1,hStatic_channel2,hStatic_channel3,hStatic_channel4,
hStatic_channel5,hStatic_channel6,hStatic_channel7,hStatic_channel8,
hEdit_feedbackvalue1,hEdit_feedbackvalue2,hEdit_feedbackvalue3,hEdit_feedbackvalue4,
hEdit_feedbackvalue5,hEdit_feedbackvalue6,hEdit_feedbackvalue7,hEdit_feedbackvalue8,
hEdit_setvalue1,hEdit_setvalue2,hEdit_setvalue3,hEdit_setvalue4,
hEdit_setvalue5,hEdit_setvalue6,hEdit_setvalue7,hEdit_setvalue8,
hStatic_setvalue,hStatic_feedback,
hButton_relay_on,hButton_relay_off,
hStatic_relay;
HINSTANCE hInst;
HANDLE hCom;//窗口句柄
DWORD IDB1,IDB2;
UCHAR relay_on_buffer[4]={0xDD,0xEE,0x0A,0xFF};
UCHAR relay_off_buffer[4]={0xDD,0xEE,0x0B,0xFF};
DWORD dwBytesWritten=4;
DWORD dwErrorFlags;
COMSTAT ComStart;
OVERLAPPED m_osWrite;
BOOL bWriteStart;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{ //创建主窗口
HWND hwnd;MSG Msg;
WNDCLASS wndclass;
char lpszClassName[]="窗口";
char lpszTitle[]="八路数控电源";
hInst=hInstance;
wndclass.style=0;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra =0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName=lpszClassName;
if(!RegisterClass(&wndclass))
{MessageBeep(0);return FALSE;}
hwnd= CreateWindow(
"窗口",
"八路数控电源",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInst,
(LPVOID)NULL
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
//打开、配置串口
hCom=CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
if(hCom==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,TEXT("打开COM1失败"),TEXT("提示"),MB_OK);
//return FALSE;
}
//return TRUE;
SetupComm(hCom,1024,1024);
COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout=1000;
TimeOuts.ReadTotalTimeoutMultiplier=500;
TimeOuts.ReadTotalTimeoutConstant=5000;
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier=500;
TimeOuts.WriteTotalTimeoutConstant=2000;
SetCommTimeouts(hCom,&TimeOuts); //设置超时
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate=9600; //波特率为9600
dcb.ByteSize=8; //每个字节有8位
dcb.Parity=NOPARITY; //无奇偶校验位
dcb.StopBits=ONESTOPBIT; //两个停止位
SetCommState(hCom,&dcb);
ClearCommError(hCom,&dwErrorFlags,&ComStart);
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);//清空缓冲区
while(GetMessage(&Msg,NULL,0,0))
{ TranslateMessage(&Msg);DispatchMessage(&Msg);}
return Msg.wParam ;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
hButton_relay_on=CreateWindow("BUTTON","打开",WS_CHILD|WS_VISIBLE|ES_CENTER|BS_AUTORADIOBUTTON,550,80,60,60,hwnd,(HMENU)IDB_RELAY_ON,hInst,NULL);
hButton_relay_on=CreateWindow("BUTTON","关闭",WS_CHILD|WS_VISIBLE|ES_CENTER|BS_AUTORADIOBUTTON,550,260,60,60,hwnd,(HMENU)IDB_RELAY_OFF,hInst,NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_RELAY_ON:
bWriteStart=WriteFile(hCom,relay_on_buffer,dwBytesWritten,&dwBytesWritten,&m_osWrite); break;
case IDB_RELAY_OFF:
bWriteStart=WriteFile(hCom,relay_off_buffer,dwBytesWritten,&dwBytesWritten,&m_osWrite); break;
}
break;
case WM_DESTROY:
CloseHandle(hCom);
PostQuitMessage(0);break;
default : return DefWindowProc(hwnd,message,wParam,lParam);
return 0;
}
}
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_RELAY_ON:
bWriteStart=WriteFile(hCom,relay_on_buffer,dwBytesWritten,&dwBytesWritten,&m_osWrite); break;
case IDB_RELAY_OFF:
bWriteStart=WriteFile(hCom,relay_off_buffer,dwBytesWritten,&dwBytesWritten,&m_osWrite); break;
}
break;
上面的一小段代码是用两个按钮控件向下位机发送开关信号,每次打开程序只有第一次按下按钮有效,按其他的按钮或者第二次按都没反应。大家知道这是怎么回事吗
[本帖最后由 zcswinner 于 2013-8-26 18:30 编辑]
搜索更多相关主题的帖子:
include