USB-306対応DLL HDL9BITDLL.dllをC++Builder から操作する例

2016/03/29-

  当社の9bit通信対応 USB RS485/422絶縁型変換器(USB-306)対応の制御用DLL(HDL9BITDLL.dll)をC++Bulilderで利用する方法についての参考ページです。

公開APIのヘッダ(関数定義)


/******************************************************************************
* HDL9BITDLL.h
*
* API for interfacing to USB-306
*
******************************************************************************/

#pragma once

#ifndef _HDL9BIT_H
#define _HDL9BIT_H


#include <Windows.h>

#ifndef uint32l_t
typedef unsigned long uint32l_t;
#endif

#ifndef uint16_t
typedef unsigned short uint16_t;
#endif

#ifndef uint8_t
typedef unsigned char uint8_t;
#endif

#ifndef boolean_t
typedef BOOL boolean_t;
#endif


#ifndef H9bHandle_t
typedef void* H9bHandle_t;
#endif


/******************************************************************************
* API Functions
******************************************************************************/

//#define DECLSPECH

#ifdef DECLSPEC_EXPORThdl
 #define DECLSPECH __declspec(dllexport)
#else
 #define DECLSPECH __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C" {
#endif


/*
 * Returns the current USB Serial Number String.
 * デバイス固有番号取得
 */
DECLSPECH int WINAPI HDL9BIT_GetOtpSerialNumberString(
        __in    H9bHandle_t uartDeviceObject,
    __out       uint8_t* stringLength,
    __out       char* serialString
);

/*
 * Returns the number of attached devices with matching IDs.
 * デバイス総数を取得
 */
DECLSPECH int WINAPI HDL9BIT_GetNumUartsA(
        __in_opt        uint16_t productIDFilter,
        __in_opt        uint8_t interfaceIDFilter,
        __out   uint32l_t* numUarts
);


/*
 * Returns the currently configures data rate and character format
 * 通信条件の読み込み 
 * for a UART channel.
 */
DECLSPECH int WINAPI HDL9BIT_GetLineCodingA(
        __in    H9bHandle_t uartDeviceObject,
        __out   uint32l_t* dataRate,
        __out   uint8_t* charFormat,
        __out   uint8_t* parityType,
        __out   uint8_t* dataBits,
        __out   uint8_t* flowControl,
        __out   uint8_t* gpioMode0
);

      
/*
 * Configures the data rate and character format of a UART channel.
 * 通信条件設定
 */
DECLSPECH int WINAPI HDL9BIT_SetLineCodingA(
        __in    H9bHandle_t uartDeviceObject,
        __in    uint32l_t dataRate,
        __in    uint8_t charFormat,
        __in    uint8_t parityType,
        __in    uint8_t dataBits,
        __in    uint8_t flowControl,
        __in    uint8_t gpioMode0
        //  addressモード時 flowControl = 0x03
        //  それ以外は      flowControl = 0x01
        //  gpioMode0は0x39固定
);

/*
 * Returns the current timeout settings.
 * 送受信タイムアウト時間読み出し
 */
DECLSPECH int WINAPI HDL9BIT_GetTimeouts(
    __in        H9bHandle_t uartDeviceObject,
    __out       uint32l_t* writeTimeout,
    __out       uint32l_t* readTimeout
);

/*
 * Sets maximum allowable time for read and write operations.
 * 送受信タイムアウト時間設定
 */
DECLSPECH int WINAPI HDL9BIT_SetTimeouts(
    __in        H9bHandle_t uartDeviceObject,
    __in        uint32l_t writeTimeout,
    __in        uint32l_t readTimeout
);

/*
 * Returns the current setting for the number of bytes in the Rx FIFO
 * before flow control is asserted to the remote UART.
 * 受信FIFO動作しきい値読み込み
 */
DECLSPECH int WINAPI HDL9BIT_GetUartRxFifoThreshold(
        __in    H9bHandle_t uartDeviceObject,
        __out   uint16_t* rxFifoThreshold
);


/*
 * Sets how many bytes will be stored in the local UART's Rx FIFO
 * before flow control is asserted to the remote UART.
 * 受信FIFO動作しきい値設定
 */
DECLSPECH int WINAPI HDL9BIT_SetUartRxFifoThreshold(
        __in    H9bHandle_t uartDeviceObject,
        __in    uint16_t rxFifoThreshold
);


/*
 * Opens the device specified by the index into the array of available
 * devices and returns a handle to it.
 * UARTオープン
 */
DECLSPECH int WINAPI HDL9BIT_OpenUartA(
        __in    uint32l_t memberIndex,
        __in_opt        uint16_t productIDFilter,
        __in_opt        uint8_t interfaceIDFilter,
        __out   H9bHandle_t* uartDeviceObject
);


/*
 * Close a device handle
 * UARTクローズ
 */
DECLSPECH int WINAPI HDL9BIT_CloseUart(
    __in        H9bHandle_t uartDeviceObject
);

      
/*
 * Returns the currently configured addresses used by the UART when
 * Address Match Mode is enabled.
 * AddressModeで使用する設定アドレス取得
 */
DECLSPECH int WINAPI HDL9BIT_GetUartAddress(
    __in        H9bHandle_t uartDeviceObject,
    __out       uint8_t* unicastAddress,
    __out       uint8_t* multicastAddress
);
/*
 * Sets the addresses the UART will respond to when Address Match Mode
 * is enabled.
 * AddressModeで使用する設定アドレス設定
 */
DECLSPECH int WINAPI HDL9BIT_SetUartAddress(
    __in        H9bHandle_t uartDeviceObject,
    __in        uint8_t unicastAddress,
    __in        uint8_t multicastAddress
);


/*
 * Reads data from a UART handle (USB IN transfers).
 * 受信データ読み込み 
 */
DECLSPECH int WINAPI HDL9BIT_ReadUart(
    __in        H9bHandle_t uartDeviceObject,
    __in        uint32l_t numberOfBytesToRead,
    __out       uint8_t* byteBuffer,
    __out       uint32l_t* numberOfBytesRead
);


/*
 * Writes data to a UART handle (USB OUT transfers).
 * 送信データ書き込み(送信) 
 */
DECLSPECH int WINAPI HDL9BIT_WriteUart(
        __in    H9bHandle_t uartDeviceObject,
    __in        uint32l_t numberOfBytesToWrite,
        __in    uint8_t* byteBuffer,
        __out   uint32l_t* numberOfBytesWritten
);


/*
 * Indicates whether a UART channel is currently enabled for transmit and receive
 * 送受信有効・無効設定確認
 */
DECLSPECH int WINAPI HDL9BIT_IsEnabled(
    __in        H9bHandle_t uartDeviceObject,
    __out       boolean_t* enabled
);


/*
 * Enables a UART channel for transmit and receive
 * 送受信有効・無効設定 
 */
DECLSPECH int WINAPI HDL9BIT_EnableUart(
    __in        H9bHandle_t uartDeviceObject,
    __in        boolean_t enable
);

/*
 * Flushes buffers and FIFOs in transmit, receive, or both directions.
 * 送受信バッファを空にする
 */
DECLSPECH int WINAPI HDL9BIT_FlushBuffers(
    __in        H9bHandle_t uartDeviceObject,
    __in        boolean_t flushTransmit,
    __in        boolean_t flushReceive
);


/*
 * Requests a UART to transmit a break signal.
 * ブレーク信号送信
 */
DECLSPECH int WINAPI HDL9BIT_SendBreak(
    __in        H9bHandle_t uartDeviceObject,
    __in        uint8_t breakDuration
);


/*
 * Requests a UART to stop transmitting break.
 * ブレーク信号停止 
 */
DECLSPECH int WINAPI HDL9BIT_CancelBreak(
    __in        H9bHandle_t uartDeviceObject
);


/*
 * Returns the current USB Manufacturer String.
 * 製造メーカ名取得
 */
DECLSPECH int WINAPI HDL9BIT_GetOtpManufacturerString(
    __in        H9bHandle_t uartDeviceObject,
    __out       uint8_t* strlen,
    __out       char* manufacturingString
);

/*
 * Returns the current USB Product String.
 * 製品名称取得
 */
DECLSPECH int WINAPI HDL9BIT_GetOtpProductString(
    __in        H9bHandle_t uartDeviceObject,
    __out       uint8_t* stringLength,
    __out       char* productString
);


/*
 * Returns the release version of this API.
 * ファームウエアバージョン取得
 */
DECLSPECH int WINAPI HDL9BIT_GetSoftwareAPIAttributes(
    __out       uint8_t* majorVersion,
    __out       uint8_t* minorVersion,
    __out       boolean_t* isReleaseVersion
);

/*
 * Indicates whether a device handle is still open
 * オープン状況問い合わせ
 */
DECLSPECH int WINAPI HDL9BIT_IsOpen(
    __in        H9bHandle_t uartDeviceObject,
        __out   boolean_t* opened
);

/*
 * Causes a UART channel to be re-enumerated on the USB bus.
 * USBの再エニュメレート
 */
DECLSPECH int WINAPI HDL9BIT_ReEnumerate(
    __in        H9bHandle_t uartDeviceObject
);

#ifdef __cplusplus
}
#endif /* __cplusplus */

公開APIのヘッダ(定数定義)


/******************************************************************************
* String Type
******************************************************************************/

#define HDL9BIT_MAX_STRING_LENGTH                    260
#define HDL9BIT_MANUFACTURING_STRING_LENGTH                256
#define HDL9BIT_PRODUCT_STRING_LENGTH                              256
#define HDL9BIT_SERIAL_STRING_LENGTH                               256

typedef char HDL9BIT_DEVICE_STRING[HDL9BIT_MAX_STRING_LENGTH];
typedef char HDL9BIT_MANUFACTURING_STRING[HDL9BIT_MANUFACTURING_STRING_LENGTH];
typedef char HDL9BIT_PRODUCT_STRING[HDL9BIT_PRODUCT_STRING_LENGTH];
typedef char HDL9BIT_SERIAL_STRING[HDL9BIT_SERIAL_STRING_LENGTH];

/******************************************************************************
* API Return Codes
******************************************************************************/
#define HDL9BIT_SUCCESS                         0x00
#define HDL9BIT_ERR_DEVICE_NOT_FOUND                    0x01
#define HDL9BIT_ERR_INVALID_HANDLE                      0x02
#define HDL9BIT_ERR_HANDLE_IS_NOT_A_UART                0x03
#define HDL9BIT_ERR_INVALID_PARAMETER                   0x04
#define HDL9BIT_ERR_INVALID_REQUEST_LENGTH              0x05
#define HDL9BIT_ERR_UART_READ_FAIL                      0x10
#define HDL9BIT_ERR_UART_WRITE_FAIL                     0x11
#define HDL9BIT_ERR_UART_READ_FAIL_TIME_OUT             0x12
#define HDL9BIT_ERR_UART_WRITE_FAIL_TIME_OUT            0x13
#define HDL9BIT_ERR_USB_CTRL_PIPE_FAIL          0x14
#define HDL9BIT_ERR_USB_COMMUNICATION_ERROR             0x15
#define HDL9BIT_ERR_USB_DEVICE_NOT_SUPPORTED            0x16
#define HDL9BIT_ERR_GENERIC_FAIL                        0xFF

/******************************************************************************
* API Parameter Encodings
******************************************************************************/
/*
 * stringType definitions for HDL9BIT_GetStringByIndex()
 * and HDL9BIT_GetStringByHandle()
 */
#define HDL9BIT_STRING_TYPE_VID                 0x01
#define HDL9BIT_STRING_TYPE_PID                 0x02
#define HDL9BIT_STRING_TYPE_PATH                        0x03
#define HDL9BIT_STRING_TYPE_SERIAL                      0x04
#define HDL9BIT_STRING_TYPE_MANUFACTURER                0x05
#define HDL9BIT_STRING_TYPE_PRODUCT                     0x06

/*
 * Error Flags returned by HDL9BIT_GetUartAttributes()
 */
#define HDL9BIT_ERROR_FLAG_PARITY                       0x01
#define HDL9BIT_ERROR_FLAG_OVERFLOW                     0x02

/*
 * Number of data bits. Input to HDL9BIT_SetLineCoding() and
 * output of HDL9BIT_GetLineCoding()
 */
#define HDL9BIT_DATA_BITS_FIVE                  0x00
#define HDL9BIT_DATA_BITS_SIX                           0x01
#define HDL9BIT_DATA_BITS_SEVEN                 0x02
#define HDL9BIT_DATA_BITS_EIGHT                 0x03
#define HDL9BIT_DATA_BITS_NINE                  0x04

/*
 * Parity types. Input to HDL9BIT_SetLineCoding() and
 * output of HDL9BIT_GetLineCoding()
 */
#define HDL9BIT_PARITY_NONE                             0x00
#define HDL9BIT_PARITY_ODD                              0x01
#define HDL9BIT_PARITY_EVEN                             0x02
#define HDL9BIT_PARITY_MARK                             0x03
#define HDL9BIT_PARITY_SPACE                            0x04

/*
 * Character formats. Input to HDL9BIT_SetLineCoding() and
 * output of HDL9BIT_GetLineCoding()
 */
#define HDL9BIT_CHAR_FORMAT_STOP_SHORT          0x00
#define HDL9BIT_CHAR_FORMAT_STOP_LONG                   0x01

/*
 * Flow control types. Input to HDL9BIT_SetLineCodingA() and
 * output of HDL9BIT_GetLineCodingA()
 */
#define HDL9BIT_FLOW_CONTROL_NONE               0x00
#define HDL9BIT_FLOW_CONTROL_HW_RTS_CTS         0x01
#define HDL9BIT_FLOW_CONTROL_ADDRESS_MODE       0x03

#endif /* _HDL9BIT_H */

使用例

・全ソース
公開されている全てのAPIを使っているサンプルソースです。
// ---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
#include "HDL9BIT\HDL9BITDLL.h"

// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
boolean_t isOpenUart;
H9bHandle_t HDL9BIT_han;

// ---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) {
}

__fastcall TForm2::memo_ret(String msg, int ret) // HDL9BITDLLの戻り値表示
{
    String msg1;
    if (ret == HDL9BIT_SUCCESS) {
        msg1 = "*" + msg + " OK";
        Memo_monit->Lines->Add(msg1);
    }
    else {
        msg1 = "*" + msg + " ---- Error!";
        Memo_monit->Lines->Add(msg1);
    }
    Memo_monit->Perform(EM_SCROLLCARET, 0, 0);
}

__fastcall TForm2::memo(String msg) // memo 表示
{
    Memo_monit->Lines->Add("[" + msg + "]");
    Memo_monit->Perform(EM_SCROLLCARET, 0, 0);            //キャレット位置までスクロール

}

// ---------------------------------------------------------------------------
void __fastcall TForm2::Button_openClick(TObject *Sender) {
    int ret;
    uint8_t strlen8b;
    uint8_t serialString[512];
    uint8_t productString[512];
    uint8_t manufacturingString[512];
    uint8_t minorVersion;
    uint8_t majorVersion;
    boolean_t isReleaseVersion;
    AnsiString ss1;

    isOpenUart = false;
    minorVersion = 0;
    majorVersion = 0;
    isReleaseVersion = false;
    while (true) {
        // -------------------------------------------------------------------
        // Returns the release version of this API.
        // ファームウエアバージョン取得
        ret = HDL9BIT_GetSoftwareAPIAttributes(
            &majorVersion,      // __out        uint8_t* majorVersion,
            &minorVersion,      // __out        uint8_t* minorVersion,
            &isReleaseVersion   // __out        boolean_t* isReleaseVersion
            );
        memo_ret("HDL9BIT_GetSoftwareAPIAttributes", ret);
        ss1.printf("majorVersion= %d", majorVersion);
        memo( ss1 );
        ss1.printf("minorVersion= %d", minorVersion);
        memo( ss1 );
        // Returns the number of attached devices with matching IDs.
        // デバイス数取得
        uint16_t productIDFilter;
        uint32l_t numUarts;
        productIDFilter = 0x4001;
        ret = HDL9BIT_GetNumUartsA(
            productIDFilter,    // __in_opt     uint16_t productIDFilter,
            (uint8_t)0,         // __in_opt     uint8_t interfaceIDFilter,
            &numUarts           // __out        uint32l_t* numUarts
            );
        memo_ret("HDL9BIT_GetNumUartsA", ret);
        // -------------------------------------------------------------------
        // Opens the device specified by the index into the array of available
        // devices And returns a handle to it.
        // UARTオープン
        ss1.printf("%d", numUarts);
        label_devn->Caption = "Device =" + ss1;
        ret = HDL9BIT_OpenUartA(
            0,                  // __in uint32l_t memberIndex,
            productIDFilter,    // __in_opt     uint16_t productIDFilter,
            (uint8_t)0,            // __in_opt  uint8_t interfaceIDFilter,
            &HDL9BIT_han        // __out        H9bHandle_t* uartDeviceObject
            );
        memo_ret("HDL9BIT_OpenUart", ret);
        if (ret != HDL9BIT_SUCCESS) {
            break;
        }

        // -------------------------------------------------------------------
        // Returns the currently configures data rate and character format
        // 通信条件の読み込み
        uint32l_t dataRate; // __out uint32l_t* dataRate,
        uint8_t charFormat; // __out uint8_t* charFormat,
        uint8_t parityType; // __out uint8_t* parityType,
        uint8_t dataBits;   // __out uint8_t* dataBits,
        uint8_t flowControl; // __out uint8_t* flowControl,
        uint8_t gpioMode0;
        ret = HDL9BIT_GetLineCodingA(
            HDL9BIT_han,    // __in   H9bHandle_t uartDeviceObject,
            &dataRate,      // __out   uint32l_t* dataRate,
            &charFormat,    // __out   uint8_t* charFormat,
            &parityType,    // __out    uint8_t* parityType,
            &dataBits,      // __out   uint8_t* dataBits,
            &flowControl,   // __out   uint8_t* flowControl,
            &gpioMode0      // __out    uint8_t* gpioMode0
            );
        memo_ret("HDL9BIT_GetLineCodingA", ret);
        // -------------------------------------------------------------------
        // Configures the data rate and character format of a UART channel.
        // 通信条件設定
        dataBits = HDL9BIT_DATA_BITS_NINE; // '9bitモード指定
        parityType = HDL9BIT_PARITY_NONE;
        dataRate = 1200000;
        flowControl = HDL9BIT_FLOW_CONTROL_ADDRESS_MODE;
        gpioMode0 = 0x39;
        ret = HDL9BIT_SetLineCodingA(
            HDL9BIT_han,    // __in   H9bHandle_t uartDeviceObject,
             dataRate,      // __in   uint32l_t dataRate,
            charFormat,     // __in   uint8_t charFormat,
            parityType,     // __in   uint8_t parityType,
            dataBits,       // __in   uint8_t dataBits,
            flowControl,    // __in   uint8_t flowControl,
            gpioMode0       // __in   uint8_t gpioMode0
                 //  addressモード時 flowControl = 0x03
                 //  それ以外は      flowControl = 0x01
                 //  gpioMode0は0x39固定
            );
        memo_ret("HDL9BIT_SetLineCodingA", ret);
        ss1.printf("dataRate = %d", dataRate);
        memo(ss1);
        // -------------------------------------------------------------------
        // Sets maximum allowable time for read and write operations.
        // 送受信タイムアウト時間読み込み
        uint32l_t writeTimeout;
        uint32l_t readTimeout;
        ret = HDL9BIT_GetTimeouts(
            HDL9BIT_han,    // __in   H9bHandle_t uartDeviceObject,
            &writeTimeout,  // __in*  uint32l_t writeTimeout,
            &readTimeout    // __in*  uint32l_t readTimeout
            );
        writeTimeout = 500;
        readTimeout = 2000;
        // -------------------------------------------------------------------
        // Sets maximum allowable time for read and write operations.
        ret = HDL9BIT_SetTimeouts(
                HDL9BIT_han,    // __in   H9bHandle_t uartDeviceObject,
            writeTimeout,       // __in   uint32l_t writeTimeout,
            readTimeout         // __in   uint32l_t readTimeout
            );
        memo_ret("HDL9BIT_HDL9BIT_GetTimeouts", ret);
        // -------------------------------------------------------------------
        // Returns the currently configured addresses used by the UART when
        // Address Match Mode is enabled.
        // AddressModeで使用する設定アドレス取得
        uint8_t unicastAddress = 0; // __out uint8_t* unicastAddress,
        uint8_t multicastAddress = 0; // __out uint8_t* multicastAddress
        ret = HDL9BIT_GetUartAddress(
            HDL9BIT_han,        // __in   H9bHandle_t uartDeviceObject,
            &unicastAddress,    // __out uint8_t* unicastAddress,
            &multicastAddress   // __out uint8_t* multicastAddress
            );
        memo_ret("HDL9BIT_GetUartAddress", ret);
        // -------------------------------------------------------------------
        // Sets the addresses the UART will respond to when Address Match Mode
        // is enabled.
        // AddressModeで使用する設定アドレス設定
        unicastAddress = 0x55; // __out   uint8_t* unicastAddress,
        multicastAddress = 0x80; // __out   uint8_t* multicastAddress
        ret = HDL9BIT_SetUartAddress(
            HDL9BIT_han,        // __in   H9bHandle_t uartDeviceObject,
            unicastAddress,     // __in       uint8_t unicastAddress,
            multicastAddress    // __in       uint8_t multicastAddress
            );
        memo_ret("HDL9BIT_SetUartAddress", ret);
        memo_ret("HDL9BIT_SetUartRxFifoThreshold", ret);
        ss1.printf("unicastAddress = %02X", unicastAddress);
        memo(ss1);
        ss1.printf("multicastAddress = %02X", multicastAddress);
        memo(ss1);
        // -------------------------------------------------------------------
        // Returns the current setting for the number of bytes in the Rx FIFO
        // before flow control is asserted to the remote UART.
        // 受信FIFO動作しきい値読み込み
        uint16_t rxFifoThreshold; // __out uint16_t* rxFifoThreshold
        ret = HDL9BIT_GetUartRxFifoThreshold(
            HDL9BIT_han,        // __in   H9bHandle_t uartDeviceObject,
            &rxFifoThreshold    // __out   uint16_t* rxFifoThreshold
            );
        memo_ret("HDL9BIT_GetUartRxFifoThreshold", ret);
        // -------------------------------------------------------------------
        // Sets how many bytes will be stored in the local UART's Rx FIFO
        // before flow control is asserted to the remote UART.
        // 受信FIFO動作しきい値設定
        rxFifoThreshold = 450; // __out     uint16_t* rxFifoThreshold
        ret = HDL9BIT_SetUartRxFifoThreshold(
            HDL9BIT_han,    // __in   H9bHandle_t uartDeviceObject,
            rxFifoThreshold // __in       uint16_t rxFifoThreshold
            );
        memo_ret("HDL9BIT_SetUartRxFifoThreshold", ret);
        ss1.printf("rxFifoThreshold = %d", rxFifoThreshold);
        memo(ss1);

        // -------------------------------------------------------------------
        // __indicates whether a device handle is still open
        // オープン状況問い合わせ
        ret = HDL9BIT_IsOpen(
            HDL9BIT_han,    // __in     H9bHandle_t uartDeviceObject,
            &isOpenUart     // __out    boolean_t* opened
            );

        memo_ret("HDL9BIT_IsOpen", ret);
        memo(" --Open Uart --");

        CheckBox_isOpen->Checked = isOpenUart;
        // -------------------------------------------------------------------
        // Returns the current USB Serial Number String.
        // デバイス固有番号(シリアル番号)取得
        ret = HDL9BIT_GetOtpSerialNumberString(
            HDL9BIT_han,    // __in     H9bHandle_t uartDeviceObject,
            &strlen8b,      // __out    uint8_t* stringLength,
            serialString    // __out    char* serialString
            );
        memo_ret("HDL9BIT_GetOtpSerialNumberString", ret);
        ss1.printf("%s", serialString);
        memo(ss1);
        // -------------------------------------------------------------------
        // Returns the current USB Product String.
        // 製品名称取得
        ret = HDL9BIT_GetOtpProductString(
            HDL9BIT_han,    // __in     H9bHandle_t uartDeviceObject,
            &strlen8b,      // __out    uint8_t* stringLength,
            productString   // __out    char* productString
            );
        memo_ret("HDL9BIT_GetOtpProductString", ret);
        ss1.printf("%s", productString);
        memo(ss1);
        // -------------------------------------------------------------------
        // Returns the current USB Manufacturer String.
        // 製造メーカ名取得
        ret = HDL9BIT_GetOtpManufacturerString(
            HDL9BIT_han,        // __in H9bHandle_t uartDeviceObject,
            &strlen8b,          // __out        uint8_t* strlen,
            manufacturingString // __out        char* manufacturingString
            );

        memo_ret("HDL9BIT_GetOtpManufacturerString", ret);
        ss1.printf("%s", manufacturingString);
        memo(ss1);
        // -------------------------------------------------------------------
        // Flushes buffers and FIFOs in transmit, receive, or both directions.
        // 送受信バッファを空にする
        boolean_t flushTransmit = true;
        Boolean flushReceive = true;
        ret = HDL9BIT_FlushBuffers(
            HDL9BIT_han,    // __in       H9bHandle_t uartDeviceObject,
            flushTransmit,  // __in       boolean_t flushTransmit,
            flushReceive    // __in       boolean_t flushReceive
            );
        memo_ret("HDL9BIT_FlushBuffers", ret);

        // -------------------------------------------------------------------
        // __indicates whether a UART channel is currently enabled for transmit and receive
        // 送受信有効・無効設定確認
        boolean_t enabled = false;
        ret = HDL9BIT_IsEnabled(
            HDL9BIT_han, // __in       H9bHandle_t uartDeviceObject,
            &enabled    // __out      boolean_t* enabled
            );
        memo_ret("HDL9BIT_IsEnabled", ret);
        // -------------------------------------------------------------------
        // Enables a UART channel for transmit and receive
        // 送受信有効・無効設定
        enabled = true;
        ret = HDL9BIT_EnableUart(
            HDL9BIT_han,    // __in       H9bHandle_t uartDeviceObject,
            enabled         // __in boolean_t enable
            );
        memo_ret("HDL9BIT_EnableUart", ret);

        break;
        // loop end
    }
}
// ---------------------------------------------------------------------------
void __fastcall TForm2::Button_BRK_onClick(TObject *Sender)            // BREAK ON
{
     if (isOpenUart)
    {
        //-------------------------------------------------------------------
        //Requests a UART to transmit a break signal.
        //ブレーク信号送信
        int ret;
        ret = HDL9BIT_SendBreak(
            HDL9BIT_han,    // __in       H9bHandle_t uartDeviceObject,
            0               //__in      uint8_t breakDuration
            );
        memo_ret("HDL9BIT_SendBreak", ret);
    }
    else
    {
        memo("Openしてません");
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button_BRK_offClick(TObject *Sender)  // BREAK OFF
{
    if (isOpenUart)
    {
        //-------------------------------------------------------------------
        //Requests a UART to stop transmitting break.
        //ブレーク信号停止
        int ret;
        ret = HDL9BIT_CancelBreak(
             HDL9BIT_han    // __in       H9bHandle_t uartDeviceObject,
        );
        memo_ret("HHDL9BIT_CancelBreak", ret);
    }
    else
    {
        memo("Openしてません");
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button_closeClick(TObject *Sender)    //クローズ
{
    //-------------------------------------------------------------------
    //Close a device handle
    //UARTクローズ
    int ret;
    ret = HDL9BIT_CloseUart(
        HDL9BIT_han    // __in       H9bHandle_t uartDeviceObject,
    );
    memo_ret("HDL9BIT_CloseUart", ret);
    isOpenUart = false;
    CheckBox_isOpen->Checked = isOpenUart;
}
//---------------------------------------------------------------------------

void __fastcall TForm2::CheckBox_isOpenClick(TObject *Sender)
{
    CheckBox_isOpen->Checked = isOpenUart;
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button_sendClick(TObject *Sender)
{
    while (true)
    {
        if (!isOpenUart)
        {
            break;
        }

        int ret;
        int i;
        int len;
        int address16;
        AnsiString ss1;
        uint8_t address;
        uint8_t byteBuffer[1024];
        uint32l_t numberOfBytesToWrite;
        uint32l_t numberOfBytesWritten = 0;
        numberOfBytesToWrite = 10;
        // 相手アドレス
        ss1 = "0x"+ Edit_adrs->Text;
        address16 = ss1.ToInt();
        address  = (uint8_t)(address16 & 0xff);
        //送信テキスト
        AnsiString s_text = Edit_stext->Text;
        uint8_t byteBuffertemp[512];
        len  = s_text.Length();
        memcpy( byteBuffertemp, s_text.c_str(), len);

        i = 0;
        byteBuffer[i++] = address;
        byteBuffer[i++] = 0x01;

        numberOfBytesToWrite = len * 2 + 2;
        for ( int j= 0; i <  len * 2; i+=2, j++)
        {
            byteBuffer[i] = byteBuffertemp[j];
            byteBuffer[i + 1] = 0;
        }
        //-------------------------------------------------------------------
        //Writes data to a UART handle (USB OUT transfers).
        //送信データ書き込み(送信)
        ret = HDL9BIT_WriteUart(
            HDL9BIT_han,                // __in       H9bHandle_t uartDeviceObject,
            numberOfBytesToWrite,       //__in    uint32l_t numberOfBytesToWrite,
            byteBuffer,                 // __in   uint8_t* byteBuffer,
            &numberOfBytesWritten       //_out    uint32l_t* numberOfBytesWritten
        );
        memo_ret("HDL9BIT_WriteUart", ret);
        if (ret != HDL9BIT_SUCCESS) break;
        //------------------------------------------------------------------
        // 受信
        //-------------------------------------------------------------------
        //Reads data from a UART handle (USB IN transfers).
        //受信データ読み込み
        uint32l_t numberOfBytesToRead;
        uint32l_t numberOfBytesRead;
        numberOfBytesToRead = 256;
        numberOfBytesRead = 0;
        memo(".....wait recieve..");
        ret = HDL9BIT_ReadUart(
            HDL9BIT_han,            // __in       H9bHandle_t uartDeviceObject,
            numberOfBytesToRead,    //__in    uint32l_t numberOfBytesToRead,
            byteBuffer,             //__out    uint8_t* byteBuffer,
            &numberOfBytesRead      //__out   uint32l_t* numberOfBytesRead
        );
        if ((ret == HDL9BIT_SUCCESS) ||
            (ret == HDL9BIT_ERR_UART_READ_FAIL_TIME_OUT))
        {
            if (numberOfBytesRead != 0) {
                AnsiString rx_text,ss1;
                rx_text = "";
                for( i = 0; i < numberOfBytesRead; i++ ){
                    ss1.printf("%02X ", byteBuffer[i]);
                    rx_text = rx_text + ss1;
                }
                memo("RXD=" + rx_text);
            }
        }
        else
        {

        }
        memo(".....return");
        // loop end
        break;
    }

}
//---------------------------------------------------------------------------
弊社ではプログラミングに関数サポートを行う立場ではございませんが、USB-306を操作する上での具体的なご質問があればメールにてお問い合わせをお願いいたします。
 メールでのご質問はこちらへどうぞ

USB専用サイトのトップへ