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);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述