⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialportcontrol.cpp

📁 本程序没有使用SerialComm控件
💻 CPP
字号:
#include "StdAfx.h"
#include "SerialPortControl.h"

HANDLE hCom; //全局变量,串口句柄
HANDLE hCommThread; //全局变量,串口线程

BOOL OpenSerialPort1()

{
//打开并设置COM1
hCom=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0,NULL , OPEN_EXISTING, 0, NULL);
if (hCom==(HANDLE)-1)
{
   AfxMessageBox("打开COM1失败");
    return false;
}
else
{
DCB wdcb;
GetCommState (hCom, &wdcb); 
//波特率:9600,其他:不变
wdcb.BaudRate=9600;            
SetCommState (hCom, &wdcb);
PurgeComm(hCom, PURGE_TXCLEAR);
}
return true;
}


//以一个线程不同监控串口行接收的数据
DWORD WINAPI SerialPort1ThreadProcess( HWND hWnd)  //主窗口句柄
{
   char str[101];
//读取的字节数
   DWORD wCount; 
   while(1)
   {
  ReadFile(hCom,str, 100, &wCount, NULL);
              //收到数据
    if(wCount > 0) 
	{
     str[wCount] = '\0';
      ::PostMessage(hWnd, COM_RECVDATA, (unsigned int) str, wCount); //发送消息给对话框主窗口,以进行接收内容的显示
	}
   }
return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -