MFC串口问题(C++语言)

各位高手,请教一个问题:我现在已经通过mcscomm.ocx控件弄了一个串口在主对话框(它可以实现我们自己在发送编辑框写任何数据,通过串口可以在接收区编辑框成功接收到数据。)现在我在其子对话框里弄了一个东西【是byte型的数组---都把它放在myBuf(自己定义的byte这个数组名字)里面了】,现在我想通过串口把这个数组传输出去,请问怎么传输,要调用到哪些函数,妹子是新手,还请各位大哥帮帮忙,讲讲具体的做法,当然有代码就更好了,谢谢。

这位妹子你卖萌么?
mcscomm.ocx控件对MFC支持 不好的

给你封装几个函数
拿去用下

HANDLE hComm;
OVERLAPPED m_ov;
COMSTAT comstat;
DWORD m_dwCommEvents;
int ts;

bool openport(char *portname)//打开一个串口
{
hComm = CreateFile(portname,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (hComm == INVALID_HANDLE_VALUE)
return FALSE;
else
return true;
}

bool setupdcb(int rate_arg) 设置波特率
{
DCB dcb;
int rate= rate_arg;
memset(&dcb,0,sizeof(dcb));
if(!GetCommState(hComm,&dcb))//获取当前DCB配置
{
return FALSE;
}
/* -------------------------------------------------------------------- */
// set DCB to configure the serial port
dcb.DCBlength = sizeof(dcb);
/* ---------- Serial Port Config ------- */
dcb.BaudRate = rate;
dcb.Parity = NOPARITY;
dcb.fParity = 0;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
dcb.fOutxCtsFlow = 0;
dcb.fOutxDsrFlow = 0;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = 0;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutX = 0;
dcb.fInX = 0;
/* ----------------- misc parameters ----- */
dcb.fErrorChar = 0;
dcb.fBinary = 1;
dcb.fNull = 0;
dcb.fAbortOnError = 0;
dcb.wReserved = 0;
dcb.XonLim = 2;
dcb.XoffLim = 4;
dcb.XonChar = 0x13;
dcb.XoffChar = 0x19;
dcb.EvtChar = 0;
/* -------------------------------------------------------------------- */
// set DCB
if(!SetCommState(hComm,&dcb))
{
return false;
}
else
return true;
}

bool setuptimeout(DWORD ReadInterval,DWORD ReadTotalMultiplier,DWORD ReadTotalconstant,DWORD WriteTotalMultiplier,DWORD WriteTotalconstant)
{//设置超时时间
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout=ReadInterval;
timeouts.ReadTotalTimeoutConstant=ReadTotalconstant;
timeouts.ReadTotalTimeoutMultiplier=ReadTotalMultiplier;
timeouts.WriteTotalTimeoutConstant=WriteTotalconstant;
timeouts.WriteTotalTimeoutMultiplier=WriteTotalMultiplier;
if(!SetCommTimeouts(hComm, &timeouts))
{
return false;
}
else
return true;
}

UINT ReceiveChar(LPVOID pParam)//接收数据,这是一个MFC线程函数,非阻塞的方式打开一般接收数据 都要放在线程中做的
{

char szRevBuf[30];
ZeroMemory(szRevBuf,30);
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
char RXBuff;
for (;;)
{
if(ts==0)return 0;
bResult = ClearCommError(hComm, &dwError, &comstat);
if (comstat.cbInQue == 0)
continue;
if (bRead)
{
bResult = ReadFile(hComm, // Handle to COMM port
&RXBuff, // RX Buffer Pointer
1, // Read one byte
&BytesRead, // Stores number of bytes read
&m_ov); // pointer to the m_ov structure
sprintf(szRevBuf,"%s%c",szRevBuf,RXBuff);
SetDlgItemText(AfxGetApp()->m_pMainWnd->m_hWnd,IDC_RES,szRevBuf);
if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
bRead = FALSE;
break;
}
default:
{
break;
}
}
}
else
{
bRead = TRUE;
}
} // close if (bRead)
if (!bRead)
{
bRead = TRUE;
bResult = GetOverlappedResult(hComm, // Handle to COMM port
&m_ov, // Overlapped structure
&BytesRead, // Stores number of bytes read
TRUE); // Wait flag
}

}

}

void WriteChar(char* m_szWriteBuffer,DWORD m_nToSend)//发送数据
{
BOOL bWrite = TRUE;
BOOL bResult = TRUE;
DWORD BytesSent = 0;
HANDLE m_hWriteEvent;
ResetEvent(m_hWriteEvent);
if (bWrite)
{
m_ov.Offset = 0;
m_ov.OffsetHigh = 0;
// Clear buffer
bResult = WriteFile(hComm, // Handle to COMM Port
m_szWriteBuffer, // Pointer to message buffer in calling finction
m_nToSend, // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&m_ov ); // Overlapped structure
if (!bResult)
{
DWORD dwError = GetLastError();
switch (dwError)
{
case ERROR_IO_PENDING:
{
// continue to GetOverlappedResults()
BytesSent = 0;
bWrite = FALSE;
break;
}
default:
{
// all other error codes
break;
}
}
}
} // end if(bWrite)
if (!bWrite)
{
bWrite = TRUE;
bResult = GetOverlappedResult(hComm, // Handle to COMM port
&m_ov, // Overlapped structure
&BytesSent, // Stores number of bytes sent
TRUE); // Wait flag

// deal with the error code
if (!bResult)
{
printf("GetOverlappedResults() in WriteFile()");
}
} // end if (!bWrite)

// Verify that the data size send equals what we tried to send
if (BytesSent != m_nToSend)
{
printf("WARNING: WriteFile() error.. Bytes Sent: %d; Message Length: %d\n", BytesSent, strlen((char*)m_szWriteBuffer));
}
//return true;
}

用法如下
if(openport("COM1") && setupdcb(9600) && setuptimeout(0,0,0,0,0))

AfxMessageBox("初始化串口成功!");
else AfxMessageBox("初始化串口失败!");

发送数据:

WriteChar(待发送的数据缓冲区,数据长度);

接收数据

这位妹子如果需要现成的工程 就内线我要吧!这个东西不方便挂在网上
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-06

看一下这个文档吧,随便还可以系统地学习一下。

你的那个实现起来不难的。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网