なんか暇だったから書いてみた。
#pragma once
#include
#include
typedef std::queue ByteRingBuffer;
class SerialPort
{
HANDLE hComm;
BYTE testBuff[256];
DWORD index;
DWORD testSize;
UINT timerId;
ByteRingBuffer sendDataQueue;
std::vector counter;
LARGE_INTEGER freq;
public:
SerialPort(void);
~SerialPort(void);
void Write(BYTE sendData[], DWORD size);
void Write(ByteRingBuffer &sendData, DWORD elapsed);
private:
void cycleSend();
static void CALLBACK timerFunc(UINT uiID, UINT uiNo, DWORD dwUser, DWORD dwNo1, DWORD dwNo2);
};
#include "StdAfx.h"
#include "SerialPort.h"
#include
#pragma comment(lib ,"winmm.lib" ) //winmm.lib をリンクする
static SerialPort *obj;
SerialPort::SerialPort(void)
{
obj = this;
QueryPerformanceFrequency( &this->freq );
}
SerialPort::~SerialPort(void)
{
}
void SerialPort::Write(BYTE sendData[], DWORD size)
{
LARGE_INTEGER time;
QueryPerformanceCounter( &time );
this->counter.push_back( time );
for( int i = 0; i < size; i++)
{
TRACE("%02X", sendData[i]);
}
TRACE("\n");
}
void SerialPort::Write(ByteRingBuffer &sendData, DWORD elapsed)
{
// 引数のデータをメンバのデータにコピーする
while( sendData.empty() == false)
{
// XXX:
this->sendDataQueue.push( sendData.front() );
sendData.pop();
}
// 周期,1(固定), 呼び出しコールバック関数, ユーザーデータ, TIME_PERIODIC(固定:周期)
this->timerId = timeSetEvent(elapsed, 1, timerFunc, reinterpret_cast(this), TIME_PERIODIC);
}
void SerialPort::cycleSend()
{
// 送信バッファにデータがあるうちは送信
if( this->sendDataQueue.empty() == false )
{
// XXX:
this->Write( (BYTE *)sendDataQueue.front().c_str(), sendDataQueue.front().length() );
this->sendDataQueue.pop();
}
else
{
// 送信完了していたらタイマーを削除
timeKillEvent( this->timerId );
for( int i = 0; i < this->counter.size(); i++ )
{
TRACE("%dms\n", (DWORD)((this->counter[i+1].QuadPart - this->counter[i].QuadPart) * 1000 / this->freq.QuadPart) );
}
}
}
void SerialPort::timerFunc(UINT uiID, UINT uiNo, DWORD dwUser, DWORD dwNo1, DWORD dwNo2)
{
obj->cycleSend();
}
<実行>
ByteRingBuffer test;
test.push("1234");
test.push("5678");
test.push("ABCD");
test.push("EFGH");
this->serial.Write(test, 10);