⑥サンプルプログラム(サーバ側)

#include
#include
#include

using namespace std;

int main()
{
HANDLE hComm;
char buff[16];
DWORD size;
COMMTIMEOUTS ctmo;

DWORD dwErrors; /* エラー情報 */
COMSTAT ComStat; /* デバイスの状態 */
DWORD dwCount; /* 受信データのバイト数 */
char* pszBuf; /* 読み出しデータバッファ */
DWORD dwRead; /* ポートから読み出したバイト数 */
DCB dcb;

hComm = CreateFile(("COM1"),
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);

GetCommState(hComm,&dcb);

dcb.BaudRate=9600;
dcb.ByteSize=8;
dcb.Parity=EVENPARITY;
dcb.fParity=TRUE;
dcb.StopBits=ONESTOPBIT;

SetCommState(hComm, &dcb);

GetCommTimeouts(hComm,&ctmo);
ctmo.ReadIntervalTimeout = 1000; // 文字読み込みの間の時間
ctmo.ReadTotalTimeoutMultiplier = 1000; // Read :文字数に対する乗数
ctmo.ReadTotalTimeoutConstant = 1000; // Read :ミリ秒単位での定数
ctmo.WriteTotalTimeoutMultiplier = 1000; // Write:文字数に対する乗数
ctmo.WriteTotalTimeoutConstant = 1000; // Write:ミリ秒単位での定数
SetCommTimeouts(hComm,&ctmo);

ClearCommError(hComm, &dwErrors, &ComStat);
while(ComStat.cbInQue==0){
ClearCommError(hComm, &dwErrors, &ComStat);
}

dwCount=ComStat.cbInQue;
printf("dwcount:%d\n",dwCount);

memset(buff,NULL,sizeof(buff) );


ReadFile(hComm,buff,dwCount, &size, NULL);

cout << buff << endl;

getchar();
CloseHandle(hComm);

return 0;
}