• 大小: 3.13MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-11-16
  • 语言: 其他
  • 标签: 串口通讯  

资源简介

控制台串口通讯程序 可以输入 16进制的字符 然后接受到的数据以16进制的格式显示出来 多线程异步 控制台串口通讯类 及其实例

资源截图

代码片段和文件信息

#include “StdAfx.h“
#include “ConsoleSerialPort.h“


CConsoleSerialPort::CConsoleSerialPort(void)
{
m_comm=NULL;
m_overlapped.Offset=0;
m_overlapped.OffsetHigh=0;
m_overlapped.hEvent=NULL;
m_write_event=NULL;
m_shutdown_event=NULL;
m_sz_write_buffer=NULL;
m_n_write_size=1;
m_thread_alive=FALSE;
m_thread=NULL;
}


CConsoleSerialPort::CConsoleSerialPort(CContral * parent)
{
m_comm=NULL;
m_overlapped.Offset=0;
m_overlapped.OffsetHigh=0;
m_overlapped.hEvent=NULL;
m_write_event=NULL;
m_shutdown_event=NULL;
m_sz_write_buffer=NULL;
m_n_write_size=1;
m_thread_alive=FALSE;
m_thread=NULL;
m_parent=parent;
}


CConsoleSerialPort::~CConsoleSerialPort(void)
{
do 
{
SetEvent(m_shutdown_event);
} while (m_thread_alive);
delete [] m_sz_write_buffer;
}


bool CConsoleSerialPort::init_port(unsigned int port /* =1 */ unsigned int baud/* =9600 */char parity/* =‘N‘ */ unsigned int databits/* =8 */unsigned int stopbits /* =1 */DWORD dwCommEvents /* = EV_RXCHAR */unsigned int write_buffer_size/* =1024 */)
{
assert(port>0&&port<9);
if (m_thread_alive)
{
do
{
SetEvent(m_shutdown_event);
} while (m_thread_alive);
}
if (m_overlapped.hEvent!=NULL)
ResetEvent(m_overlapped.hEvent);
m_overlapped.hEvent=CreateEvent(NULLTRUEFALSENULL);
if (m_write_event!=NULL)
ResetEvent(m_write_event);
m_write_event=CreateEvent(NULLTRUEFALSENULL);
if (m_shutdown_event!=NULL)
ResetEvent(m_shutdown_event);
m_shutdown_event=CreateEvent(NULLTRUEFALSENULL);

m_event_array[0]=m_shutdown_event;
m_event_array[1]=m_overlapped.hEvent;
m_event_array[2]=m_write_event;
InitializeCriticalSection(&m_mm_sync);
m_port=port;
if (m_sz_write_buffer!=NULL)
delete []m_sz_write_buffer;
m_sz_write_buffer=new char[write_buffer_size];
m_n_write_size=write_buffer_size;
m_dw_commEvents=dwCommEvents;
bool bResult=false;
WCHAR * szPort=new WCHAR[50];
WCHAR * szBaud=new WCHAR[50];
EnterCriticalSection(&m_mm_sync);
if (m_comm!=NULL)
{
CloseHandle(m_comm);
m_comm=NULL;
}
wsprintf(szPort_T(“COM%d“)port);
wsprintf(szBaud_T(“baud=%d parity=%c data=%d stop=%d“)baudparitydatabitsstopbits);
m_comm=CreateFile(szPortGENERIC_WRITE|GENERIC_READ0NULLOPEN_EXISTINGFILE_FLAG_OVERLAPPED0);
if (m_comm ==INVALID_HANDLE_VALUE)
{
delete [] szPort;
delete [] szBaud;
return false;
}
m_comm_time_out.ReadIntervalTimeout=1000;
m_comm_time_out.ReadTotalTimeoutMultiplier=1000;
m_comm_time_out.ReadTotalTimeoutConstant=1000;
m_comm_time_out.WriteTotalTimeoutMultiplier=1000;
m_comm_time_out.WriteTotalTimeoutConstant=1000;
if (SetCommTimeouts(m_comm&m_comm_time_out))
{
if (SetCommMask(m_commdwCommEvents))
{
if (GetCommState(m_comm&m_dcb))
{
m_dcb.EvtChar =‘q‘;
m_dcb.fRtsControl=RTS_CONTROL_ENABLE;
if (BuildCommDCB(szBaud&m_dcb))
{
if (SetCommState(m_comm&m_dcb))
;
else
process

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     743760  2011-02-20 02:01  Serialcommunication\Debug\msvcp100d.dll

     文件    1505104  2011-02-20 02:01  Serialcommunication\Debug\msvcr100d.dll

     文件      58368  2013-05-07 09:45  Serialcommunication\Debug\Serialcommunication.exe

     文件     545124  2013-05-07 09:45  Serialcommunication\Debug\Serialcommunication.ilk

     文件     691200  2013-05-07 09:45  Serialcommunication\Debug\Serialcommunication.pdb

     文件    2359296  2013-05-07 09:45  Serialcommunication\ipch\serialcommunication-9a27186f\serialcommunication-6ef59e62.ipch

     文件      10379  2013-05-07 09:42  Serialcommunication\Serialcommunication\ConsoleSerialPort.cpp

     文件       1550  2013-05-06 13:55  Serialcommunication\Serialcommunication\ConsoleSerialPort.h

     文件       9241  2013-05-04 15:58  Serialcommunication\Serialcommunication\ConsoleSerialPortback.cpp

     文件       1412  2013-05-04 13:05  Serialcommunication\Serialcommunication\ConsoleSerialPortback.h

     文件       1637  2013-05-03 13:54  Serialcommunication\Serialcommunication\ReadMe.txt

     文件       1564  2013-05-05 08:36  Serialcommunication\Serialcommunication\Serialcommunication.cpp

     文件       4546  2013-05-04 13:27  Serialcommunication\Serialcommunication\Serialcommunication.vcxproj

     文件       1523  2013-05-04 12:47  Serialcommunication\Serialcommunication\Serialcommunication.vcxproj.filters

     文件        143  2013-05-03 13:54  Serialcommunication\Serialcommunication\Serialcommunication.vcxproj.user

     文件      18408  2013-05-04 10:08  Serialcommunication\Serialcommunication\SerialPort.cpp

     文件       2791  2013-05-04 09:46  Serialcommunication\Serialcommunication\SerialPort.h

     文件        224  2013-05-03 13:54  Serialcommunication\Serialcommunication\stdafx.cpp

     文件        233  2013-05-03 13:54  Serialcommunication\Serialcommunication\stdafx.h

     文件        236  2013-05-03 13:54  Serialcommunication\Serialcommunication\targetver.h

     文件   10309632  2013-05-07 09:45  Serialcommunication\Serialcommunication.sdf

     文件        924  2013-05-03 13:54  Serialcommunication\Serialcommunication.sln

    ..A..H.     18432  2013-05-07 09:45  Serialcommunication\Serialcommunication.suo

     目录          0  2013-05-07 09:45  Serialcommunication\ipch\serialcommunication-9a27186f

     目录          0  2013-05-07 09:45  Serialcommunication\Debug

     目录          0  2013-05-07 09:45  Serialcommunication\ipch

     目录          0  2013-05-07 09:46  Serialcommunication\Serialcommunication

     目录          0  2013-05-07 09:45  Serialcommunication

----------- ---------  ---------- -----  ----

             16285727                    28

............此处省略1个文件信息

评论

共有 条评论