分享
 
 
 

异步串口通信 VC++

王朝vc·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

//MyComm.h 多机控制 异步串口通信

class CMyComm

{

public:

CMyComm();

virtual ~CMyComm();

void PreOpenSetupQueue(DWord dwInQueue, DWORD dwOutQueue);

// size of input buffer, size of output buffer

BOOL Open(int nPort, int nBaud);

//默认无校验,每个字节发送11个bit,异步方式。

//若设置校验后,校验错则字节被替换为0x7E

BOOL SetupQueue(DWORD dwInQueue, DWORD dwOutQueue);

// size of input buffer, size of output buffer

BOOL ResetParity(char Parity);//parity = 'N', 'O', 'E', 'M', 'S' 不区分大小写

//分别表示 no, odd, even, mark, space

// 在Open()前设置无效。

BOOL SendData(LPCVOID lpBuf, DWORD dwToWrite);

DWORD ReadData(LPVOID lpBuf, DWORD dwToRead);

void Close();

protected:

HANDLE m_hCom;

BOOL m_bOpened;

OVERLAPPED m_osReader;

OVERLAPPED m_osWriter;

DWORD m_dwInBuf;

DWORD m_dwOutBuf;

};

//////////////////////////////////////////////////////////

//MyComm.cpp

#include "stdafx.h"

#include "MyComm.h"

///////////////////////////////////////////

CMyComm::CMyComm()

{

m_bOpened = FALSE;

m_dwInBuf = 512;

m_dwOutBuf = 512;

m_hCom = NULL;

}

void CMyComm::PreOpenSetupQueue(DWORD dwInQueue, DWORD dwOutQueue)

{

m_dwInBuf = dwInQueue;

m_dwOutBuf = dwOutQueue;

}

BOOL CMyComm::Open(int nPort, int nBaud)

{

ASSERT(nPort > 0 nPort < 5 nBaud >= 110 nBaud <= 128000);

if( m_bOpened ) return TRUE;

char szPort[15];

char lpDef[15];

DCB dcb = {0};

dcb.DCBlength = sizeof(dcb);

wsprintf(szPort, "COM%d", nPort);

wsprintf(lpDef, "%d,n,8,1", nBaud);

m_hCom = CreateFile(szPort, GENERIC_READ GENERIC_WRITE,

0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);

if( m_hCom == INVALID_HANDLE_VALUE ) return FALSE;

FillMemory(&m_osReader, sizeof(OVERLAPPED), 0);

FillMemory(&m_osWriter, sizeof(OVERLAPPED), 0);

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

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

int byteUsedTime = 14400 / nBaud +1;

COMMTIMEOUTS timeouts = {20 + byteUsedTime, byteUsedTime, 1000, byteUsedTime , 20};

dcb.fParity = TRUE;

dcb.fErrorChar = TRUE;

dcb.ErrorChar = '~';

if( m_osReader.hEvent == NULL m_osWriter.hEvent == NULL

!SetCommTimeouts(m_hCom, &timeouts)

!BuildCommDCB(lpDef, &dcb) !SetupComm(m_hCom, m_dwInBuf, m_dwOutBuf)) {

if( m_osReader.hEvent != NULL )

CloseHandle( m_osReader.hEvent );

if( m_osWriter.hEvent != NULL )

CloseHandle( m_osWriter.hEvent );

CloseHandle( m_hCom );

return FALSE;

}

m_bOpened = TRUE;

return m_bOpened;

}

BOOL CMyComm::SetupQueue(DWORD dwInQueue, DWORD dwOutQueue)

{

if (m_hCom == NULL) return FALSE;

m_dwInBuf = dwInQueue;

m_dwOutBuf = dwOutQueue;

return SetupComm(m_hCom, m_dwInBuf, m_dwOutBuf);

}

BOOL CMyComm::ResetParity(char Parity)

{

if (m_hCom == NULL) return FALSE;

DCB dcb;

dcb.DCBlength = sizeof( DCB );

if (!GetCommState(m_hCom, &dcb)) return FALSE;

BYTE cParity;

Parity = tolower(Parity);

switch (Parity) {

case 'o':

cParity = 1;

break;

case 'e':

cParity = 2;

break;

case 'm':

cParity = 3;

break;

case 's':

cParity = 4;

break;

default:

cParity = 0;

break;

}

dcb.Parity = cParity;

return SetCommState(m_hCom, &dcb);

}

BOOL CMyComm::SendData(LPCVOID lpBuf, DWORD dwToWrite)

{

TRACE("SSSSSSSSSSSSS 00\n");

if( !m_bOpened m_hCom == NULL ) return FALSE;

DWORD dwWritten;

if (WriteFile(m_hCom, lpBuf, dwToWrite, &dwWritten, &m_osWriter)) return TRUE;

if (GetLastError() != ERROR_IO_PENDING) return FALSE;

GetOverlappedResult(m_hCom, &m_osWriter, &dwWritten, TRUE);

TRACE("SSSSSSSSSSSSS 11\n");

return (dwToWrite == dwWritten);

}

DWORD CMyComm::ReadData(LPVOID lpBuf, DWORD dwToRead)

{

TRACE("RRRRRRRRRRRR 00\n");

if( !m_bOpened m_hCom == NULL ) return 0;

DWORD dwRead;

if (ReadFile(m_hCom, lpBuf, dwToRead, &dwRead, &m_osReader) ) return dwRead;

if (GetLastError() != ERROR_IO_PENDING) return 0;

if (WaitForSingleObject(m_osReader.hEvent, INFINITE) != WAIT_OBJECT_0 )

return 0;

if (!GetOverlappedResult(m_hCom, &m_osReader, &dwRead, FALSE) )

return 0;

TRACE("RRRRRRRRRRRR 11\n");

return dwRead;

}

void CMyComm::Close()

{

if (m_osReader.hEvent != NULL) CloseHandle( m_osReader.hEvent );

if (m_osWriter.hEvent != NULL) CloseHandle( m_osWriter.hEvent );

if (m_hCom != NULL) CloseHandle( m_hCom );

m_bOpened = FALSE;

}

CMyComm::~CMyComm()

{

Close();

}

// main.cpp

char Buf[40];

int nArray[3];

CMyComn myCom;

UINT SendDataProc(LPVOID pParam);

void OnCommSendReceive()

{

myCom.PreOpenSetupQueue(12, 12);

if (!myCom.Open(2, 4800)) return;

//假如想改变校验位,在此位置,如:myCom.ResetParity('m');

FillMemory(Buf, 40, 0);

nArray[0] = 0;

nArray[1] = 17;

nArray[2] = 88888;

AfxBeginThread(SendDataProc, (LPVOID)(12));

TRACE("EEEEEEEE\n");

int nRead = myCom.ReadData(Buf, 12);

int* nA = (int*) Buf;

TRACE("AAAAAAAAAAAAAA %d, %d %d %d\n",nRead, nA[0], nA[1], nA[1]);

}

UINT SendDataProc(LPVOID pParam)

{

return myCom.SendData((LPVOID)nArray, (DWORD) pParam);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有