• VC菜菜鸟创建一个即时串口通信程序


    //编者注:串口接收基于多线程任务。

    #include <windows.h>
    #include <windowsx.h>
    #include "main.h"
    #include "dialogs.h"
    #include "resource.h"
    #include "stdio.h"

    HANDLE hCom;
    DWORD dwError;
    DCB LpdcbCom;
    OVERLAPPED m_ov;
    //Compile error using LPDCB
    char ScomBuf[256];

    DWORD WINAPI PTRead(LPVOID pParam)
    {
     DWORD EV_COMM;
     char str[256];
     char ByteRead[256];
     DWORD BytesSent;
     COMSTAT ComSta;
     DWORD Counter;
     DWORD ErrorNum;
     while(1)
     {
      ( WaitCommEvent(hCom,&EV_COMM,&m_ov));
      if( EV_COMM == EV_RXCHAR )
      {
    //   MessageBox(NULL,"RX Event Happened...","",MB_OK);
       EV_COMM = 0;
    // sprintf(ByteRead,"\0");
     for(int tmp=0; tmp<256; tmp++)
      ByteRead[tmp]= '\0';

     ClearCommError(hCom,&ErrorNum,&ComSta);
       ReadFile(hCom,  // Handle to COMM port
                  &ByteRead,   // RX Buffer Pointer
              ComSta.cbInQue,     // Read one byte
              &BytesSent,   // Stores number of bytes read
                     &m_ov);     // pointer to the m_ov structure
     sprintf(ByteRead,"%s\0",ByteRead);
     SetDlgItemText((HWND )(pParam),IDC_TEXT,ByteRead);//Note: HWND is a Pointer...
    //MessageBox(NULL,ByteRead,"",MB_OK);
    /*
        sprintf(str,"%c",ByteRead);
     if( ByteRead == '\0' )
     {
      MessageBox(NULL,str,NULL,MB_OK);
      break;
      
     }

    */

      }
    // MessageBox(NULL,"Sub-thread","",MB_OK);
    // Sleep(1);
     }
    /*
     while(1)
     {
      
       ReadFile(hCom,  // Handle to COMM port
                  &ByteRead,   // RX Buffer Pointer
              1,     // Read one byte
              &BytesSent,   // Stores number of bytes read
                     &m_ov);     // pointer to the m_ov structure
              sprintf(str,"%c",ByteRead);
     MessageBox(hwnd,str,NULL,MB_OK);
     if( ByteRead == '\0' ) break;
     
     }

    */
    }


    void OpenScom()
    {
     DWORD Error=0;

        CloseHandle(hCom); //Avoid Open Scomm too much time in the same thread.

     hCom = CreateFile( "COM7",\
          GENERIC_READ | GENERIC_WRITE,\
          0,\
          NULL,\
          OPEN_EXISTING,
          FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,\
          NULL
         );

     
     if( hCom == INVALID_HANDLE_VALUE)
     {
      
      if( 5 == GetLastError() )
      {
       sprintf(ScomBuf,"%s","串口已被占用");
       MessageBox(NULL,ScomBuf,NULL,MB_OK);
      }
      ClearCommError(hCom,&Error,NULL);  
    //  sprintf(ScomBuf,"%u",dwError);
     } 


     GetCommState(hCom,&LpdcbCom);
     BuildCommDCB("baud=9600 parity=N data=8 stop=1",&LpdcbCom);
     SetCommState(hCom,&LpdcbCom);
     
     
    }

    void InitScom()
    {
     
    }

    BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch(uMsg)
        {
            //BEGIN MESSAGE CRACK
            HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
            HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
            HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
            //END MESSAGE CRACK
        }

        return FALSE;
    }

    ////////////////////////////////////////////////////////////////////////////////
    //  Main_OnInitDialog
    BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
    {
        // Set app icons
        HICON hIcon = LoadIcon((HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE) ,MAKEINTRESOURCE(IDI_ICONAPP));
        SendMessage(hwnd, WM_SETICON, TRUE,  (LPARAM)hIcon);
        SendMessage(hwnd, WM_SETICON, FALSE, (LPARAM)hIcon);
    //    SetDlgItemText(hwnd,IDC_TEXT,"jr");


     CreateThread(NULL,0,PTRead,hwnd,0,NULL);   
        //
        // Add initializing code here
        //


       
        return TRUE;
    }

    ////////////////////////////////////////////////////////////////////////////////
    //  Main_OnCommand
    void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
    {
     DWORD BytesSent = 0;
     char ByteRead;
     char str[255];
        switch(id)
        {
         case IDC_SEND:
         //  MessageBox(hwnd,"TEST","TEST",MB_OK);
    //    TransmitCommChar(hCom,'J');

       WriteFile(hCom, // Handle to COMM Port
            "Hello,world.", // Pointer to message buffer in calling finction
            12,  // Length of message to send
            &BytesSent,  // Where to store the number of bytes sent
            &m_ov );   // Overlapped structure


      break;
         
            case IDC_OK:
    //            MessageBox(hwnd,TEXT("You clicked OK!"),TEXT("SCOM"),MB_OK);
       OpenScom();
       SetCommMask(hCom,EV_RXCHAR);
    //              EndDialog(hwnd, id);
            break;
            case IDC_CANCEL:
    //            MessageBox(hwnd,TEXT("You clicked Cancel!"),TEXT("SCOM"),MB_OK);

             CloseHandle(hCom);

    //        EndDialog(hwnd, id);
            break;
            default:break;
        }

    }

    ////////////////////////////////////////////////////////////////////////////////
    //  Main_OnClose
    void Main_OnClose(HWND hwnd)
    {
        EndDialog(hwnd, 0);
    }

  • 相关阅读:
    JVM 垃圾回收器工作原理及使用实例介绍(转载自IBM),直接复制粘贴,需要原文戳链接
    装tomcat和nginx心得
    jms的俩种模式
    裸奔Spring(1)
    一个最小mybatis
    SpringBoot和数据库连接
    SpringBoot的基础Pom
    SpringBoot读取配置文件
    埃拉托斯特尼素数筛法
    hdu 1175 连连看
  • 原文地址:https://www.cnblogs.com/techstone/p/2661216.html
Copyright © 2020-2023  润新知