?? rs232u_lib.cpp
字號:
/* Copyright (C) Jungo 2003 - 2004 */
#include <windows.h>
#include "wdlib.h"
#include "rs232u_lib.h"
#define MIN(a, b) ((a) < (b))? (a) : (b)
#define READ_TIMEOUT 200
#define WRITE_TIMEOUT 2000
#pragma pack(push)
#pragma pack(1)
struct ReadWriteReport
{
unsigned char reportid;
unsigned char bytes;
unsigned char data[MAX_REPORT_LEN-2];
};
struct BaudRateReport
{
unsigned char reportid;
unsigned char speed;
};
struct FlowControlReport
{
unsigned char reportid;
unsigned char type;
};
#pragma pack(pop)
char *szBaudRate[SPEEDS_NUM] = {"Default:9600", "4800", "9600", "19200", "38400", "57600"};
WDL_STATUS RS232U_SetHWFlowControl(WDL_HANDLE hDev, BOOL fActivate)
{
FlowControlReport rep;
rep.reportid = 0;
rep.type = (BYTE)(fActivate ? 0xB : 0xA);
return WDL_SetFeature(hDev, 0, (u8 *)&rep, sizeof(rep));
}
WDL_STATUS RS232U_SetSpeed(WDL_HANDLE hDev, unsigned char iSpeed)
{
BaudRateReport rep;
rep.reportid = 0;
rep.speed = iSpeed;
return WDL_SetFeature(hDev, 0, (u8 *)&rep, sizeof(rep));
}
WDL_STATUS RS232U_Write(WDL_HANDLE hDev, void *buf, unsigned int iBytes)
{
WDL_STATUS status = WDL_SUCCESS;
unsigned int iBytesWritten = 0;
while (iBytesWritten < iBytes)
{
ReadWriteReport rep;
rep.reportid = 0;
rep.bytes = (BYTE)(MIN(iBytes - iBytesWritten, MAX_REPORT_LEN-2));
memcpy(rep.data, (char *)buf + iBytesWritten, rep.bytes);
status = WDL_Write(hDev, 0, (unsigned char *)&rep, sizeof(rep), WRITE_TIMEOUT);
if (status)
break;
iBytesWritten += rep.bytes;
}
return status;
}
WDL_STATUS RS232U_Read(WDL_HANDLE hDev, void *buf, unsigned int *piBytesRead)
{
ReadWriteReport rep;
unsigned int uiBytes;
WDL_STATUS status = WDL_Read(hDev, 0, (unsigned char *)&rep, sizeof(rep),
&uiBytes, READ_TIMEOUT);
if (status)
return status;
if (rep.bytes > uiBytes - 2)
// MAX_REPORT_LEN is not big enough
return WDL_FAIL;
memcpy(buf, &rep.data, rep.bytes);
*piBytesRead = rep.bytes;
return WDL_SUCCESS;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -