Visio studio 2015 使用串口

串口通讯是诸多传感器、执行器与主机间常用的通讯方式,在linux系统下串口通讯c++编程非常简单。然而,对于一些采用在windows下编译的环境来说,串口通讯在VS下的使用相对来说资料较少,因此,今天介绍下VS2015下的串口通讯编程。

Windows下编程使用串口:这里使用VS2015编程。
其中串口相关的头文件如下:

/*"Serial_Vs.h"*/
#include
#include 
#include


HANDLESerial_open(LPCWSTR,int);
intSerial_read(HANDLE,void*,int);
intSerial_write(HANDLE,constvoid*,int);
voidSerial_close(HANDLE);
voidclear_buf(unsignedchar*,int);

/**
open serial
@param COMx: eg:_T("COM1")
@param BaudRate:
return 0 success ,return Negative is haed err
*/
HANDLESerial_open(LPCWSTRCOMx,intBaudRate)
{
       HANDLEhCom;
       DCBdcb = { 0 };
       hCom =CreateFile(COMx,
              GENERIC_READ|GENERIC_WRITE,
              0,
              0,
              OPEN_EXISTING,
              0,//FILE_FLAG_OVERLAPPED,   //同步方式 或 重叠方式 
              0
              );

       if(hCom ==INVALID_HANDLE_VALUE)
       {
              DWORDdwError = GetLastError();
              printf("Sorry, failed to open the serial\n");
              //return -1;
              printf("The program will terminate in 3 seconds\n");
              Sleep(3000);
              exit(0);
       }
       else
              printf("The serial is successfully opened in a Baudrate %d!\n",BaudRate);

       dcb.DCBlength =sizeof(DCB);

       if(!GetCommState(hCom, &dcb))
       {
              DWORDdwError = GetLastError();
              return(HANDLE)(-1);
       }

       dcb.BaudRate =BaudRate;   //波特率 
       dcb.ByteSize = 8;          //位数 
       dcb.Parity =NOPARITY;     //奇偶检验 
       dcb.StopBits =ONESTOPBIT;  //停止位数 

       if(!SetCommState(hCom, &dcb))
       {
              DWORDdwError = GetLastError();
              return(HANDLE)(-1);
       }
       if(!PurgeComm(hCom,PURGE_RXCLEAR))   return(HANDLE)(-1);

       SetupComm(hCom, 1024, 1024);
       returnhCom;;
}



/**
serial read
@param Buf:data buf
@param size:
@return The len of read
*/
intSerial_read(HANDLEhCom,void*OutBuf,intsize)
{
       DWORDcnt = 0;
       ReadFile(hCom,OutBuf,size, &cnt, 0);
       returncnt;

}

/**
serial write
@param Buf:data buf
@param size:bytes of Buf
@return The len of writen
*/
intSerial_write(HANDLEhCom,constvoid*Buf,intsize)
{
       DWORDdw;
       WriteFile(hCom,Buf,size, &dw,NULL);
       returndw;
}

/**
serial close
*/
voidSerial_close(HANDLEhCom)
{
       CloseHandle(hCom);
}

/**
clear buf
*/
voidclear_buf(unsignedchar*buf,intN)
{
       inti;
       for(i = 0; i 



主函数如下
/*main.cpp*/
HANDLEhCom;
int main()
{
     
     //--open serial port as a default baudrate 9600
     hCom = Serial_open(_T(Portname), 9600);
     //--Clear receive memory
     PurgeComm(hCom,PURGE_RXCLEAR);
     //--Close and reopen the port
     Serial_close(hCom);
}



转载请注明:

http://write.blog.csdn.net/postedit/51065789

Powered by Xiaoliang Wang

[email protected]

Beijing Institute of Technology-The University of British Columbia

China


你可能感兴趣的:(Visio studio 2015 使用串口)