| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 605 人关注过本帖
标题:求助。为什么用API 做的一个按钮控件只有第一次按有效,按第一次
取消只看楼主 加入收藏
zcswinner
Rank: 1
等 级:新手上路
帖 子:9
专家分:5
注 册:2013-8-26
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:1 
求助。为什么用API 做的一个按钮控件只有第一次按有效,按第一次
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <TCHAR.H>

#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 
2013-08-26 18:14
zcswinner
Rank: 1
等 级:新手上路
帖 子:9
专家分:5
注 册:2013-8-26
收藏
得分:0 
回复 2楼 liufashuai
我的意思是打开程序后,第一次按下打开按钮,可以打开下位机的继电器,但是接下来我按下关闭按钮,继电器没反应,用示波器观察了,确实没有信号发出去。这时候我把下位机断电使继电器关闭,再按下打开按钮,继电器还是没打开。
2013-08-26 18:19
快速回复:求助。为什么用API 做的一个按钮控件只有第一次按有效,按第一次
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.068844 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved