资源简介
1.Grbl上位机串口控制程序:带坐标解析反馈、单轴运动控制调试、串口指令输入;
2.完整且经典的串口编程(非MSCOMM32.OCX ActiveX控件),学习方便;
3.VS2015 x64 系统win10编译通过;
代码片段和文件信息
#include “stdafx.h“
#include “SerialPort.h“
#include
#include
CSerialPort::CSerialPort()
{
m_hComm = NULL;
//初始化overlapped
m_ov.Offset = 0;
m_ov.OffsetHigh = 0;
m_ov.hEvent = NULL;
m_hWriteEvent = NULL;
m_hShutdownEvent = NULL;
m_szWriteBuffer = NULL;
m_nWriteSize = 1;
m_bThreadAlive = FALSE;
}
CSerialPort::~CSerialPort()
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
if (m_hShutdownEvent != NULL)
CloseHandle(m_hShutdownEvent);
if (m_ov.hEvent != NULL)
CloseHandle(m_ov.hEvent);
if (m_hWriteEvent != NULL)
CloseHandle(m_hWriteEvent);
TRACE(“Thread ended\n“);
delete[] m_szWriteBuffer;
}
BOOL CSerialPort::InitPort(CWnd * pPortOwner UINT portnr UINT baud char parity UINT databits UINT stopbits DWORD dwCommEvents UINT writebuffersize)
{
assert(portnr > 0 && portnr < 5);
assert(pPortOwner != NULL);
if (m_bThreadAlive)
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE(“Thread ended\n“);
}
//创建事件
if (m_ov.hEvent != NULL)
ResetEvent(m_ov.hEvent);
else
m_ov.hEvent = CreateEvent(NULL TRUE FALSE NULL);
if (m_hWriteEvent != NULL)
ResetEvent(m_hWriteEvent);
else
m_hWriteEvent = CreateEvent(NULL TRUE FALSE NULL);
if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
else
m_hShutdownEvent = CreateEvent(NULL TRUE FALSE NULL);
//初始化事件对象
m_hEventArray[0] = m_hShutdownEvent;
m_hEventArray[1] = m_ov.hEvent;
m_hEventArray[2] = m_hWriteEvent;
InitializeCriticalSection(&m_csCommunicationSync);
m_pOwner = pPortOwner;
if (m_szWriteBuffer != NULL)
delete[] m_szWriteBuffer;
m_szWriteBuffer = new char[writebuffersize];
m_nPortNr = portnr;
m_nWriteBufferSize = writebuffersize;
m_dwCommEvents = dwCommEvents;
BOOL bResult = FALSE;
//LPCWSTR szPort = _T(“COM1“);// new char[50];
char* szPort = new char[50];
char* szBaud = new char[50];
/*CString str = CString(szPort);
USES_CONVERSION;
LPCWSTR wszPort = A2CW(W2A(str));
str = CString(szBaud);
LPCWSTR wszBaud = A2CW(W2A(str));
str.ReleaseBuffer();*/
EnterCriticalSection(&m_csCommunicationSync);
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
sprintf(szPort “COM%d“ portnr);
sprintf(szBaud “baud=%d parity=%c data=%d stop=%d“ baud parity databits stopbits);
m_hComm = CreateFile(szPort GENERIC_READ | GENERIC_WRITE 0 NULL OPEN_EXISTING FILE_FLAG_OVERLAPPED 0);
if (m_hComm == INVALID_HANDLE_VALUE)
{
//没有找到串口
delete[] szPort;
delete[] szBaud;
return FALSE;
}
//设置超时
m_CommTimeouts.ReadIntervalTimeout = 1000;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
m_CommTimeouts.WriteTotalTimeo
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
..A..H. 69632 2018-09-19 12:43 SerialPortApplication\.vs\SerialPortApplication\v14\.suo
文件 5523 2018-08-10 13:44 SerialPortApplication\SerialPortApplication\ReadMe.txt
文件 29 2018-08-13 11:11 SerialPortApplication\SerialPortApplication\Rec00.txt
文件 67777 2015-09-20 17:20 SerialPortApplication\SerialPortApplication\res\SerialPortApplication.ico
文件 826 2018-08-10 13:44 SerialPortApplication\SerialPortApplication\res\SerialPortApplication.rc2
文件 5622 2018-08-13 17:41 SerialPortApplication\SerialPortApplication\resource.h
文件 12493 2018-08-13 16:06 SerialPortApplication\SerialPortApplication\SerialPort.cpp
文件 2033 2018-08-13 16:06 SerialPortApplication\SerialPortApplication\SerialPort.h
文件 97024 2018-09-06 16:32 SerialPortApplication\SerialPortApplication\SerialPortApplication.aps
文件 3160 2018-08-10 13:44 SerialPortApplication\SerialPortApplication\SerialPortApplication.cpp
文件 595 2018-08-10 13:44 SerialPortApplication\SerialPortApplication\SerialPortApplication.h
文件 23766 2018-08-18 16:51 SerialPortApplication\SerialPortApplication\SerialPortApplication.rc
文件 10826 2018-08-19 09:12 SerialPortApplication\SerialPortApplication\SerialPortApplication.vcxproj
文件 2383 2018-08-10 13:53 SerialPortApplication\SerialPortApplication\SerialPortApplication.vcxproj.filters
文件 165 2018-08-10 16:52 SerialPortApplication\SerialPortApplication\SerialPortApplication.vcxproj.user
文件 39304 2018-09-19 11:53 SerialPortApplication\SerialPortApplication\SerialPortApplicationDlg.cpp
文件 4154 2018-08-17 18:57 SerialPortApplication\SerialPortApplication\SerialPortApplicationDlg.h
文件 223 2018-08-10 13:44 SerialPortApplication\SerialPortApplication\stdafx.cpp
文件 1753 2018-08-10 13:44 SerialPortApplication\SerialPortApplication\stdafx.h
文件 314 2018-08-10 13:44 SerialPortApplication\SerialPortApplication\targetver.h
文件 3702 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\CL.command.1.tlog
文件 91080 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\CL.read.1.tlog
文件 3032 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\CL.write.1.tlog
文件 2208 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\li
文件 6932 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\li
文件 1290 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\li
文件 520 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\rc.command.1.tlog
文件 3216 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\rc.read.1.tlog
文件 350 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\rc.write.1.tlog
文件 197 2018-09-19 12:40 SerialPortApplication\SerialPortApplication\x64\Debug\SerialPo.F64EB80D.tlog\SerialPortApplication.lastbuildstate
............此处省略37个文件信息
评论
共有 条评论