| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 487 人关注过本帖
标题:帮忙看一个简单程序!
只看楼主 加入收藏
chen_li
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-11-16
收藏
 问题点数:0 回复次数:1 
帮忙看一个简单程序!

这是我写的一个简单的串口程序,具有异步读写功能,在VC6.0上运行时,总是出现读错误,实在找不出原因了,来求教!
// wwww.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "stdio.h"
//#include "mscomm.h"


#define BUFFER_SIZE 1024
//#define MSG(szMsg) MessageBox (NULL, szMsg , TEXT("Error"), MB_OK)


HANDLE hCom;
OVERLAPPED m_OverlappedRead, m_OverlappedWrite;

BOOL OpenSerial(LPCTSTR lpszPortNum);
DWORD WriteDataSerial(LPCVOID pdata, DWORD len);
DWORD ReadDataSerial(TCHAR ReceiveBuf[BUFFER_SIZE]);

void StrToHex(char *str,char*strhex);
void CloseSerial(void);


int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

long LastError=ERROR_SUCCESS;
LastError=OpenSerial("COM1");
if(LastError!=TRUE)
{
MessageBox(NULL,TEXT("open failed"),TEXT("error"),MB_OK);
return LastError;
}

BYTE cmd[]="Hello world";
LastError=WriteDataSerial(cmd,11);
if(!LastError)
{
MessageBox(NULL,TEXT("write failed"),TEXT("error"),MB_OK);
return 0;

}
char ReceiveBuf[1024];
memset(ReceiveBuf,0,sizeof(ReceiveBuf));
LastError=ReadDataSerial(ReceiveBuf);

if(!LastError)
{
MessageBox(NULL,TEXT("ReadDataSerial:read data failed"),TEXT("Error"), MB_OK);
return 0;
}

CloseSerial();
return TRUE;

}
BOOL OpenSerial(LPCTSTR lpszPortNum)
{
DCB dcb;
BOOL bSuccess;


hCom = CreateFile( lpszPortNum,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);

if (hCom ==INVALID_HANDLE_VALUE)
{

MessageBox (NULL, TEXT("OpenSerial():CreateFile() failed, invalid handle value!"), TEXT("Error"), MB_OK);

return FALSE;
}
memset(&m_OverlappedRead,0,sizeof(OVERLAPPED));
memset(&m_OverlappedWrite,0,sizeof(OVERLAPPED));


COMMTIMEOUTS CommTimeOuts;
GetCommTimeouts(hCom,&CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier=0;
CommTimeOuts.ReadTotalTimeoutConstant=0;
CommTimeOuts.WriteTotalTimeoutMultiplier=50;
CommTimeOuts.WriteTotalTimeoutConstant=2000;
if(!SetCommTimeouts(hCom,&CommTimeOuts))
{
MessageBox(NULL,TEXT("SetCommTimeOuts failed"),TEXT("error"),MB_OK);
return FALSE;
}
m_OverlappedRead. hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

m_OverlappedWrite. hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

GetCommState(hCom,&dcb);
dcb.BaudRate=9600;
dcb.ByteSize=1;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
SetCommState(hCom,&dcb);
/*if(!SetCommState(hCom,&dcb)||
!SetupComm(hCom,1000,1000)||
m_OverlappedRead.hEvent==NULL||
m_OverlappedWrite.hEvent==NULL)
{
DWORD dwError=GetLastError();
if(m_OverlappedRead.hEvent!=NULL) CloseHandle(m_OverlappedRead.hEvent);
if(m_OverlappedWrite.hEvent!=NULL) CloseHandle(m_OverlappedWrite.hEvent);
CloseHandle(hCom);
return FALSE;
}*/
return TRUE;
}



DWORD WriteDataSerial(LPCVOID pdata, DWORD len)
{

DWORD dwBytesWritten;
DWORD result;


if(hCom==INVALID_HANDLE_VALUE ) return FALSE;
result=WriteFile(hCom,pdata,len,&dwBytesWritten,&m_OverlappedWrite);
if(!result)
{
if (GetLastError() == ERROR_IO_PENDING)

{
WaitForSingleObject(m_OverlappedWrite.hEvent,1000);
GetOverlappedResult(hCom,&m_OverlappedWrite,&dwBytesWritten,FALSE);
return dwBytesWritten;
}

else
{
MessageBox(NULL,TEXT("write continue"),TEXT("error"),MB_OK);
return 0;
}


}

}

DWORD ReadDataSerial(TCHAR ReceiveBuf[BUFFER_SIZE])

{

COMSTAT CommStat;
DWORD dwError;
DWORD dwBytesRead;
DWORD bReadStatus;

if(hCom==INVALID_HANDLE_VALUE) return FALSE;

ClearCommError(hCom,&dwError,&CommStat);


bReadStatus=ReadFile(hCom,ReceiveBuf,256,&dwBytesRead,&m_OverlappedRead);

if(!bReadStatus)
{
if(GetLastError()==ERROR_IO_PENDING)
{ WaitForSingleObject(m_OverlappedRead.hEvent,1000);
GetOverlappedResult(hCom,&m_OverlappedRead,&dwBytesRead,FALSE);
return dwBytesRead;
}
else
return 0;
}

return dwBytesRead;
}

void CloseSerial(void)
{

CloseHandle(hCom);


}


搜索更多相关主题的帖子: include 
2005-11-16 21:46
aolong
Rank: 1
等 级:新手上路
帖 子:248
专家分:0
注 册:2005-10-11
收藏
得分:0 

不明白


奋斗之中,朦胧有你,一切已变的那么飘渺虚无...
2005-11-19 13:57
快速回复:帮忙看一个简单程序!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032895 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved