乙個好用的串列埠類(收發自如)

2021-08-05 20:51:18 字數 2682 閱讀 7017

個串列埠通訊的類

/**/

#ifndef _commutils_h__

#define _commutils_h__

class commutils

;#endif

/**/

#include "stdafx.h"

#include "commutils.h"

#include "stdio.h"

const int read_timeout = 500;

commutils::commutils()

commutils::~commutils()

bool commutils::opencom(int port)

char szport[10];

sprintf(szport,"com%d",port);

hcomm = createfile( szport,

generic_read|generic_write,

0,null,

open_existing,

null);

if (hcomm == invalid_handle_value) return false;

if (!setupcomm(hcomm, 1024, 512)) return false;

commtimeouts commtimeouts;

commtimeouts.readintervaltimeout = maxdword;

commtimeouts.readtotaltimeoutconstant =0;

commtimeouts.readtotaltimeoutmultiplier =0;

commtimeouts.writetotaltimeoutconstant =0;

commtimeouts.writetotaltimeoutmultiplier=0;

if (!setcommtimeouts(hcomm, &commtimeouts)) return false;

readovready.hevent = createevent(null,true,false,null);

writeovready.hevent =createevent(null,true,false,null);

security_attributes sa;

sa.nlength=sizeof(security_attributes);

sa.lpsecuritydescriptor=null;

sa.binherithandle=true;

dcb dcb;

getcommstate(hcomm, &dcb);

dcb.fbinary = true;

dcb.fparity = true;

dcb.baudrate = cbr_9600; // 波特率 9600

dcb.bytesize = 8; // 8 位資料位

dcb.parity = noparity; // 無奇偶校驗

dcb.stopbits = onestopbit; // 1 個停止位

if (!setcommstate(hcomm, &dcb )) return false;

bopencom = true;

return bopencom;

}bool commutils::writecom(unsigned char *sendchar, int sendsize)

else

switch(resd)

default:

return false;

break;}}

return true;

}void commutils::closecom()

bool commutils::readcom(unsigned char * receivedata, dword& receivelength)

if(receivelength == 0) return false;

receivedata[receivelength] = 0;

dword dwread;

dword dwres = waitforsingleobject(readovready.hevent, read_timeout);

switch(dwres)

return true;

}以下為使用方法:

// 1. 包含標頭檔案,定義變數

#include "commutils.h"

commutils thecomm;

unsigned char data[1024];

unsigned long len = 0;

// 2. 開啟串列埠,設定接收定時器

thecomm.opencom(1); // 開啟com1口

settimer(1, 50, 0);

// 3. 傳送資料

thecomm.writecom(data, len);

// 4. 接收資料處理

void cuartdlg::ontimer(uint nidevent) }}

cdialog::ontimer(nidevent);

}

乙個操作串列埠的類

using system using system.runtime.interopservices namespace serial m public string portnum public int baudrate public byte bytesize public byte parity...

乙個好用的日期時間類 c

class cdatetime endif include include datetime.h using namespace std cdatetime cdatetime m millisecondflag true else localtime r m ttime,m sttime endi...

PHP parseurl 乙個好用的函式

parse url 解析 url,返回其組成部分。array parse url string url 本函式解析乙個 url 並返回乙個關聯陣列,包含在 url 中出現的各種組成部分。本函式不是用來驗證給定 url 的合法性的,只是將其分解為下面列出的部分。不完整的 url 也被接受,parse ...