⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rs232u_lib.cpp

📁 Texas Instruments的TUSB3410转串口电路测试应用程序的源码。
💻 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 + -