标题:那位大侠帮忙看一下程序
只看楼主
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:45
ElfDN
Rank: 4
等 级:贵宾
威 望:11
帖 子:291
专家分:0
注 册:2005-11-13
得分:0 

精神病患者,要人家帮忙还收钱


2005-11-17 10:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-33790-1-1.html




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

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