- void HID_SetOutReport(uint8_t *pu8EpBuf)
- {
- static uint8_t pu8Buffer[62] = {0}; //定义一个数组,用于处理数据
- memcpy(pu8Buffer, pu8EpBuf, 62);
- if((pu8Buffer[2]==0x55)&&(pu8Buffer[3]==0xaa)) //如果上位机发送0X55,0XAA给单片机,则进行一次温度采集
- {
- temperature=read_temp();//读取温度
- temperature_zheng=temperature/10;//温度整数
- temperature_xiao=temperature%10;//温度小数(小数点后一位)
- //pu8Buffer[0]=1;
- pu8Buffer[1]=2;//发送两个字节
- pu8Buffer[2]=temperature_zheng;//第一个字节为整数
- pu8Buffer[3]=temperature_xiao;//第二个字节为小数
- memcpy(g_au8DeviceReport,pu8Buffer,62);//把数据复制到USB缓冲区
- }
- }
2.上位机是MFC的,只简单的实现了打开,关闭,退出,显示温度几个功能。
- BOOL CTemperatureDlg::OnInitDialog() //DIALOG初始化函数
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
- CRect rect;
- GetDlgItem(IDC_STATIC_HISTOGRAM)->GetWindowRect(rect);
- ScreenToClient(rect);
- m_ctrlHistogram.Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP, rect, this, 1000);
- m_ctrlHistogram.SetRange(1, 500); //设置绘图范围
- m_ctrlHistogram.SetSpeed(CHistogramCtrl::HIGH_SPEED);//设置波形速度
- m_ctrlHistogram.SetGridsColor(RGB(0,120, 120)); //设置栅格的颜色
- m_ctrlHistogram.SetBkColor(RGB(0, 0, 0));//设置背景颜色
- m_ctrlHistogram.SetPen(3, RGB(255, 255, 0));//设置波形颜色
-
- open_close=0;
- SetTimer(1,1000,NULL); //创建一个定时器,用于采集温度
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CTemperatureDlg::OnOpenUsb() //打开按钮处理函数
- {
- // TODO: Add your control notification handler code here
- if(open_close==0)
- {
- if(!io.OpenDevice(0x051A,0x511B)) //打开设备
- {
- MessageBox("打开失败!","Error",MB_OK);
- m_status_label.SetWindowText("USB设备状态:关闭");
- }
- else
- {
- MessageBox("打开成功","Ok",MB_OK);
- m_status_label.SetWindowText("USB设备状态:打开");
- open_close=1;
- }
- }
- else
- {
- MessageBox("设备已经打开","WARNING",MB_OK);
- }
-
- }
- void CTemperatureDlg::OnCloseUsb() //关闭按钮处理函数
- {
- // TODO: Add your control notification handler code here
- if(open_close==1)
- {
- io.CloseDevice();
- m_status_label.SetWindowText("USB设备状态:关闭");
- MessageBox("设备已经关闭","CLOSE",MB_OK);
- open_close=0;
- sprintf(temp_string,"00.0");
- m_temp.SetWindowText(temp_string);
- m_ctrlHistogram.SetPos(1);
- }
- else
- {
- MessageBox("设备未打开,无需关闭","WARNING",MB_OK);
- }
-
- }
- void CTemperatureDlg::OnTimer(UINT nIDEvent) //定时器处理函数
- {
- // TODO: Add your message handler code here and/or call default
- //m_status_label.SetWindowText("Timer...");
- if(open_close==1) 如果设备打开了
- {
- io.WriteFile((const char *)&cmd,sizeof(cmd),&Length,2000); //向USB发送采集温度命令
- io.ReadFile(usb_buf,2, &length,2000);//读取温度
- if(usb_buf[0]>49) //这里温度的显示值为1-49,如果大于49,要做相应处理
- usb_buf[0]=49;
- sprintf(temp_string,"%d.%d",usb_buf[0],usb_buf[1]);
- m_temp.SetWindowText(temp_string);//设置显示温度值
- m_ctrlHistogram.SetPos(usb_buf[0]*10+usb_buf[1]); //显示坐标值
- }
- CDialog::OnTimer(nIDEvent);
- }