如何用VC实现串口通信

chinahaerbin 2003-11-19 06:46:33
我正在研究VC与串口通信,希望大家帮助或给于一些好的书!
谢谢大家!
...全文
175 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hx 2003-11-25
  • 打赏
  • 举报
回复
同意无妻。。
网上,一个专讲串口通讯的网站。有代码下载,搜一下吧。
duyhui 2003-11-25
  • 打赏
  • 举报
回复
www.gjwtech.com确实不错
skylaugher 2003-11-24
  • 打赏
  • 举报
回复
www.gjwtech.com
lovenoend 2003-11-24
  • 打赏
  • 举报
回复
http://www.yesky.com/20010529/181919.shtml VC++ 的串口通讯 讲的很详细
98440622 2003-11-20
  • 打赏
  • 举报
回复
高手
bugyouth 2003-11-20
  • 打赏
  • 举报
回复
www.gjwtech.com
sxslyy 2003-11-20
  • 打赏
  • 举报
回复
很全了混分.
huanyun 2003-11-19
  • 打赏
  • 举报
回复
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
DWORD BytesReadCount = 0;
BOOL bXOFF = FALSE;
int dwReadLen = 0;
unsigned char RXBuff[RX_BUFFER_SIZE];

while(1)
{
if(pSerialComm->m_deqBuffer.GetDataLen() > 1024)
{
if(dwReadLen != RX_BUFFER_SIZE)
{
dwReadLen = RX_BUFFER_SIZE;
}
}
else dwReadLen = 1;
//临界区
EnterCriticalSection(&pSerialComm->m_CriticalSection);
bResult = ClearCommError(pSerialComm->m_hComm, &dwError, &comstat);
LeaveCriticalSection(&pSerialComm->m_CriticalSection);

if (comstat.cbInQue == 0) break; //读取完成

//临界区
EnterCriticalSection(&pSerialComm->m_CriticalSection);
if (bRead)
{
//读取接收到的数据
bResult = ReadFile(pSerialComm->m_hComm, // Handle to COMM port
RXBuff, // RX Buffer Pointer
dwReadLen, // Read one byte
&BytesRead, // Stores number of bytes read
&pSerialComm->m_Overlapped); // pointer to the m_ov structure
//读取发生错误
if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING: //异步I/O处理未完成
{
bRead = FALSE;
break;
}
default://其他类型的错误
{
pSerialComm->ProcessErrorMessage("ReadFile()");
break;
}
}
}
}
if (!bRead)
{
bRead = TRUE;
//获取交迭操作的起因
bResult = GetOverlappedResult(pSerialComm->m_hComm, // Handle to COMM port
&pSerialComm->m_Overlapped, // Overlapped structure
&BytesRead, // Stores number of bytes read
TRUE); // Wait flag

//读取发生错误
if (!bResult)
{
pSerialComm->ProcessErrorMessage("GetOverlappedResults() in ReadFile()");
//出临界区
LeaveCriticalSection(&pSerialComm->m_CriticalSection);
return ;
}
}

//出临界区
LeaveCriticalSection(&pSerialComm->m_CriticalSection);
if (BytesRead<=0) continue ; ////&&&&&&&&&&没有数据
BytesReadCount += BytesRead;
if(BytesReadCount > 512)
{
::PostMessage(pSerialComm->m_hWnd, WM_STATE_CHANGE, pSerialComm->m_bIsBP?REFLASH_BPDATA:REFLASH_MAINDATA, BytesRead);
BytesReadCount = 0;
}

for(int j=0;j<(int)BytesRead;j++)
{
// 如果收到XON/XOFF,转换发送标志
if (!TranByte(pSerialComm,RXBuff[j])) continue; //// 2002.2.12
// 获取数据
pSerialComm->m_deqBuffer.Push(&RXBuff[j],1);
if(pSerialComm->m_deqBuffer.IsFull() && pSerialComm->m_bRcvFlag)
{
::TransmitCommChar(pSerialComm->m_hComm, XOFF);
pSerialComm->m_bRcvFlag = FALSE;
::PostMessage(pSerialComm->m_hWnd, WM_STATE_CHANGE, REFLASH_SYSSTATE, 0);
}
}
}读取串口的函数
huanyun 2003-11-19
  • 打赏
  • 举报
回复
UINT CCommCtrl::CommThread(LPVOID pParam)
{
CCommCtrl *pSerialComm = (CCommCtrl*)pParam;
pSerialComm->m_bThreadAlive = TRUE;
//设置状态 标示端口已经启动
DWORD BytesTransfered = 0;
DWORD Event = 0;
DWORD CommEvent = 0;
DWORD dwError = 0;
COMSTAT comstat;
BOOL bResult = TRUE;

//刷新端口
if (pSerialComm->m_hComm) PurgeComm(pSerialComm->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
while(1)
{
//等待端口消息
bResult = WaitCommEvent(pSerialComm->m_hComm, &Event, &pSerialComm->m_Overlapped);
if (!bResult)
{
//如果WaitCommEvent()返回FALSE, 根据最后一个错误起因处理
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING://正常消息,读取数据
break;
case 87://未知信息,当作正常处理
break;
case ERROR_INVALID_HANDLE://端口句柄已经无效
return -1;
default://提示错误
pSerialComm->ProcessErrorMessage("WaitCommEvent()");
break;
}
}
else
{
bResult = ClearCommError(pSerialComm->m_hComm, &dwError, &comstat);
if (comstat.cbInQue == 0) continue;
}
//等待信号量
Event = WaitForMultipleObjects(3, pSerialComm->m_hEventArray, FALSE, INFINITE);

switch (Event)
{
case 0: //断开端口连接信号
{
pSerialComm->m_bThreadAlive = FALSE;
AfxEndThread(100);
break;
}
case 1: ////读数据信号
{
GetCommMask(pSerialComm->m_hComm, &CommEvent);
if (CommEvent & EV_RXCHAR || CommEvent & EV_CTS) ReceiveChar(pSerialComm, comstat);
break;
}
case 2: //写数据信号
{
break;
}
}
}
return 0;
}
读取的线程
huanyun 2003-11-19
  • 打赏
  • 举报
回复
CreateFile
ReadFile
WriteFile

就可以完成
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) return FALSE;

//设置超时延迟
m_CommTimeOuts.ReadIntervalTimeout = READINTERVALTIMEOUT;
m_CommTimeOuts.ReadTotalTimeoutMultiplier = READTOTALTIMEOUTMULTIPLIER;
m_CommTimeOuts.ReadTotalTimeoutConstant = READTOTALTIMEOUTCONSTANT;
m_CommTimeOuts.WriteTotalTimeoutMultiplier = WRITETOTALTIMEOUTMULTIPLIER;
m_CommTimeOuts.WriteTotalTimeoutConstant = WRITETOTALTIMEOUTCONSTANT;

//初始化端口缓冲区
SetupComm(m_hComm,m_nBufferSize,m_nBufferSize);

//设置超时延迟
if (SetCommTimeouts(m_hComm, &m_CommTimeOuts))
{
//指定启动事件
if (SetCommMask(m_hComm, m_dwCommEvents))
{
//获取端口状态
if (GetCommState(m_hComm, &m_DCB))
{
m_DCB.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!

//不进行流控制
m_DCB.fOutX = 0; //XON/XOFF Disable
m_DCB.fInX = 0; //XON/XOFF Disable

m_DCB.XonChar = XON ;
m_DCB.XoffChar = XOFF ;
//设置缓冲区
m_DCB.XonLim = (unsigned short)(COMBUFLEN*0.75);
m_DCB.XoffLim = (unsigned short)(COMBUFLEN*0.75);

//根据参数填充配置
if (BuildCommDCB(szBaud, &m_DCB))
{
//设置端口状态
if (!SetCommState(m_hComm, &m_DCB)) ProcessErrorMessage("SetCommState()");
}
else
ProcessErrorMessage("BuildCommDCB()");
}
else
ProcessErrorMessage("GetCommState()");
}
else
ProcessErrorMessage("SetCommMask()");
}
else
ProcessErrorMessage("SetCommTimeouts()");


打开串口的核心部分

16,551

社区成员

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

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

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