Windows 环境 串口通信例子

// test.cpp : Defines the entry point for the console application.

#include "stdafx.h"
//#include <stdio.h>
//#include <stdlib.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <stack>
#include <queue>
#include <assert.h>

//#include "SerialPort.h"

#include <windows.h>

using namespace std;

/*
HANDLE openProt(char* portName)
{
	HANDLE hCom;
	hCom = CreateFile((LPCWSTR)portName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED,0);
	return hCom;
}
 
BOOL setPort(HANDLE hCom)
{

	DCB dcb;
	dcb.BaudRate = 115200; //波特率为115200
	dcb.ByteSize = 8; //数据位数为8位
	dcb.Parity = NOPARITY; //奇偶校验
	dcb.StopBits = 1; //1个停止位
	dcb.fBinary = TRUE;
	dcb.fParity = TRUE;
	if (!SetCommState(hCom, &dcb))
	{
 	  //MessageBox("error");
	} 
	SetupComm(hCom, 1024, 1024);
	PurgeComm(hCom, PURCE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);


return TRUE;

}
*/

int _tmain(int argc, _TCHAR* argv[])
{

  HANDLE hCom;
  DWORD dwError;
  BOOL ret = 0;
  
  //hCom = CreateFile(L"COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  hCom = CreateFile(L"COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  if (hCom == (HANDLE)0xFFFFFFFF)
  {
    dwError = GetLastError();
    //MessageBox(dwError);
    printf("open failed %d \n", dwError);
  }
  else
  {
    printf("open SUCESS \n");

	//设置参数
	DCB dcb;
        memset(&dcb, 0, sizeof(DCB));

	dcb.DCBlength=sizeof(DCB);
	ret = GetCommState(hCom,&dcb);
	if(! ret)
	{
		dwError = GetLastError();
		printf("GetCommState error code : %d \n", dwError);
	}

	dcb.BaudRate = CBR_115200; //波特率为115200
	dcb.ByteSize = 8; //数据位数为8位
	dcb.Parity = NOPARITY; //奇偶校验
	dcb.StopBits = 2; //1个停止位
	//dcb.fBinary = TRUE;
	//dcb.fParity = TRUE;

	ret = SetCommState(hCom, &dcb);
	if (! ret)
	{
		dwError = GetLastError();
		printf("SetCommState failed %d \n", dwError);
	} 
	else
	{
		printf("SetCommState SUCESS\n");
	}

	SetupComm(hCom, 4096, 4096);
	//PurgeComm(hCom, PURCE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
	PurgeComm(hCom, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

	//设置读写超时间隔
	COMMTIMEOUTS existSetting;
	GetCommTimeouts(hCom, &existSetting);
	printf("existing setting: %d \n", existSetting.ReadIntervalTimeout);


	COMMTIMEOUTS to;
	memset(&to, 0, sizeof(to));
	to.ReadIntervalTimeout = 100;
	SetCommTimeouts(hCom, &to);


	ret = SetCommMask(hCom, EV_RXCHAR);
	if (! ret)
	{
		dwError = GetLastError();
		printf("SetCommMask failed %d \n", dwError);
	} 

        int a = 0; 
	char str[4096+1];
	unsigned long wCount;
	COMSTAT ComStat;
	DWORD dwErrorFlags;
	DWORD dwCommModemStatus;
	DWORD dwLength;
	char* buf = (char*) malloc(sizeof(char)*4096 +2 );
	if(buf == NULL)
		printf("nalloc failed \n");

	while(1)
	{

		ret=ClearCommError(hCom,&dwErrorFlags,&ComStat);
		if(!ret)
		{
			continue;//失败就丢弃!
		}
		WaitCommEvent (hCom, &dwCommModemStatus, NULL);

		if(dwCommModemStatus & EV_RXCHAR)
		{
			ClearCommError(hCom,&dwErrorFlags,&ComStat);
			//cbInQue返回在串行驱动程序输入队列中的字符数
			dwLength=ComStat.cbInQue;
		   
			// printf("%d--\n",dwLength);
			if(dwLength>0)
			{
				printf("got data :dwLength= %d \n", dwLength);
			}
		}
        
		memset(buf, 0, 4096);
		ret = ReadFile(hCom,(LPVOID)buf, 4096, &wCount, NULL);
		if (! ret)
		{
			dwError = GetLastError();
			printf("read failed %d \n", dwError);
		} 

		if(wCount > 0) //收到数据
		{
			printf("%s \n", buf);
			//发送消息给对话框主窗口,以进行接收内容的显示
		}

		a += wCount;
		if(a > 10000)
			break;

	}


       free(buf);
       buf=NULL;


	ret = CloseHandle(hCom);
	if(! ret)
	{
		dwError = GetLastError();
		printf("error code : %d \n", dwError);
	}
	else
	{
		printf("close OK\n");
	}

  }


    DWORD threadID;
    HANDLE hThread;
    hThread = CreateThread(NULL,0,ThreadProc,NULL,0,&threadID); // 创建线程

    system("pause");
 
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值