CSerialPort的应用问题!

Painot 2012-07-04 03:00:02
我想用CSerialPort类来实现工业控制,我想把它做成一个基于单文档的程序,但是由于对VC++不是很熟悉,所以在这里请教下大侠们,我到底该怎么去实现呢!我知道怎么添加到单文档,就是不明白我要做的事情该怎么去添加到程序中,望大家帮帮忙,谢谢!!!
...全文
240 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tomyi 2012-07-16
  • 打赏
  • 举报
回复
没有出现过这类问题。

有看了一下你的问题:


现在就是串口打不开了,我想打开4个串口,或者更多的,我定义的是50个,
assert(portnr>0 && portnr<50);//怎么就错了呢?我很无语,运行的结果就是Debug Error!


在assert(portnr>0 && portnr<50);程序里的串口只能是1-49,定义到50当然错了。

如果非要50个以上,则将assert(portnr>0 && portnr<101); // 可以支持COM1-COM100了。

Painot 2012-07-06
  • 打赏
  • 举报
回复
嗯哪,我也知道一个CSerialPort只对应一个串口,所以我把串口名搞成数组了,生成了多个对象啊,可是怎么就是编译通过,但是执行就报错呢?这是让我不理解的地方,我发现我遇到的问题有点妖!该改的地方我也改了啊!有知情人士,望伸出援助之手,HELP!HELP!HELP!
tomyi 2012-07-05
  • 打赏
  • 举报
回复
其实你的想法已经有了,但如何用程序实现,这个一时半时说不清楚。
建议先把多线程及基本的MFC学好再说。

通讯核心思想:
1、建立发送数据管理类
2、协议翻译类
3、按协议发送请求数据
4、等待回应,如果超时,则重发或进行下一个数据
5、如果回应正确,进行下一个数据发送

3-5是一个循环过程

大概就是这么个样子。
jiuzhoulh 2012-07-05
  • 打赏
  • 举报
回复
楼主问的已经不光是串口的使用了,而是程序的架构设计问题了
tomyi 2012-07-05
  • 打赏
  • 举报
回复
BOOL CSerialPort::InitPort(CWnd* pPortOwner,	// the owner (CWnd) of the port (receives message)
UINT portnr, // portnumber (1..4)
UINT baud, // baudrate
char parity, // parity
UINT databits, // databits
UINT stopbits, // stopbits
DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
UINT writebuffersize) // size to the writebuffer
{
assert(portnr > 0 && portnr < 5);
assert(pPortOwner != NULL);

// if the thread is alive: Kill
if (m_bThreadAlive)
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE("Thread ended\n");
}

// create events
if (m_ov.hEvent != NULL)
ResetEvent(m_ov.hEvent);
m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (m_hWriteEvent != NULL)
ResetEvent(m_hWriteEvent);
m_hWriteEvent = CreateEvent(NULL,
TRUE, FALSE, NULL);
// m_hWriteEvent = CreateEvent(NULL,
// FALSE, FALSE, "");

if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

// initialize the event objects
m_hEventArray[0] = m_hShutdownEvent; // highest priority
m_hEventArray[1] = m_ov.hEvent;
m_hEventArray[2] = m_hWriteEvent;

// initialize critical section
InitializeCriticalSection(&m_csCommunicationSync);

// set buffersize for writing and save the owner
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;
char *szPort = new char[50];
char *szBaud = new char[50];

// now it critical!
EnterCriticalSection(&m_csCommunicationSync);

// if the port is already opened: close it
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}

// prepare port strings
sprintf(szPort, "\\\\.\\COM%d", portnr); //打开10以上的串口号
sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits);

// get a handle to the port
m_hComm = CreateFile(szPort, // communication port string (COMX)
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // Async I/O
0); // template must be 0 for comm devices

if (m_hComm == INVALID_HANDLE_VALUE)
{
// port not found
delete [] szPort;
delete [] szBaud;

return FALSE;
}

// set the timeout values
m_CommTimeouts.ReadIntervalTimeout = 1000;
m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
m_CommTimeouts.WriteTotalTimeoutConstant = 1000;

// configure
if (::SetCommTimeouts(m_hComm, &m_CommTimeouts))
{
if (::SetCommMask(m_hComm, dwCommEvents))
{
if (::GetCommState(m_hComm, &m_dcb))
{
m_dcb.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!
if (::BuildCommDCB(szBaud, &m_dcb))
{
if (::SetCommState(m_hComm, &m_dcb))
; // normal operation... continue
else
ProcessErrorMessage("SetCommState()");
}
else
ProcessErrorMessage("BuildCommDCB()");
}
else
ProcessErrorMessage("GetCommState()");
}
else
ProcessErrorMessage("SetCommMask()");
}
else
ProcessErrorMessage("SetCommTimeouts()");

delete [] szPort;
delete [] szBaud;

// flush the port
::PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

// release critical section
LeaveCriticalSection(&m_csCommunicationSync);

TRACE("Initialisation for communicationport %d completed.\nUse Startmonitor to communicate.\n", portnr);

return TRUE;
}


通常一个CSerialPort实例只对应一个串口,如果要开50个,就有50个CSerialPort实例,这样就不会有问题了。
wyx100 2012-07-05
  • 打赏
  • 举报
回复
#3楼 得分:0回复于:2012-07-05 10:50:27其实你的想法已经有了,但如何用程序实现,这个一时半时说不清楚。
建议先把多线程及基本的MFC学好再说。

通讯核心思想:
1、建立发送数据管理类
2、协议翻译类
3、按协议发送请求数据
4、等待回应,如果超时,则重发或进行下一个数据
5、如果回应正确,进行下一个数据发送

3-5是一个循环过程

大概就是这么个样子。
Painot 2012-07-05
  • 打赏
  • 举报
回复
那些已经有了,现在就是串口打不开了,我想打开4个串口,或者更多的,我定义的是50个,
assert(portnr>0 && portnr<50);//怎么就错了呢?我很无语,运行的结果就是Debug Error!
到底我的程序该怎么改啊?
BOOL CSerialPort::InitPort(CWnd* pPortOwner,UINT portnr,UINT baud,char parity,
UINT databits,UINT stopbits,
DWORD dwCommEvents,
UINT writebuffersize,
DWORD ReadIntervalTimeout,
DWORD ReadTotalTimeoutMultiplier,
DWORD ReadTotalTimeoutConstant,
DWORD WriteTotalTimeoutMultiplier,
DWORD WriteTotalTimeoutConstant)
{
// assert(portnr>0 && portnr<50);
assert(pPortOwner!=NULL);

m_hComm=CreateFile(szPort,GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
望知道的给我指点下,详细点啊 ,谢谢!!!
Painot 2012-07-04
  • 打赏
  • 举报
回复
补充说明:
在信息采集方面的应用,多创建几个对象,然后预留一个对象来实现实时通讯的,如果环路设备中有设备损坏或者故障的,就挂起线程,3分钟重启一次,如果三次都不好用就直接关闭!基本要求就是这样的吧。有CSerialPort使用经验的前辈们,给我指点下!

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧