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

2016/03/16-

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

公開APIのインターフェースクラス定義


        // HDL9BITDLL.dllのためのクラス定義
        public class HDLclass
        {
            //
            //------------------------------------------------------------------- 
            //-------------------------------------------------------------------
            // HDL9BITDLL.dll 使用 ここから
            //-------------------------------------------------------------------
            //-------------------------------------------------------------------
            //Returns the number of attached devices with matching IDs.
            //デバイス数取得
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_GetNumUartsA(
                Int16 productIDFilter,      //__in_opt  uint16_t productIDFilter,
                byte interfaceIDFilter,     //__in_opt  uint8_t interfaceIDFilter,
                ref Int32 numUarts          //__out     uint32l_t* numUarts
            );
            //-------------------------------------------------------------------
            //Opens the device specified by the index into the array of available
            //devices And returns a handle to it.
            //UARTオープン
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_OpenUartA(
                Int32 memberIndex,             //__in   uint32l_t memberIndex,
                Int16 productIDFilter,          //__in_opt      uint16_t productIDFilter,
                byte interfaceIDFilter,         //__in_opt      uint8_t interfaceIDFilter,
                ref IntPtr uartDeviceObject     //__out H9bHandle_t* uartDeviceObject
            );
            //-------------------------------------------------------------------
            //Returns the current USB Serial Number String.
            //デバイス固有番号(シリアル番号)取得
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_GetOtpSerialNumberString(
                IntPtr uartDeviceObject,        //__in  H9bHandle_t uartDeviceObject,
                ref byte stringLength,          //__out uint8_t* stringLength,
                byte[] serialString               //__out       char* serialString
            );
            //-------------------------------------------------------------------
            //Returns the currently configures data rate and character format
            //通信条件の読み込み 
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_GetLineCodingA(
                IntPtr uartDeviceObject,        //__in  H9bHandle_t uartDeviceObject,
                ref Int32 dataRate,             //__out uint32l_t* dataRate,
                ref Byte charFormat,            //__out uint8_t* charFormat,    
                ref Byte parityType,            //__out uint8_t* parityType,
                ref Byte dataBits,              //__out uint8_t* dataBits,
                ref Byte flowControl,           //__out uint8_t* flowControl,
                ref Byte gpioMode0              //__out uint8_t* gpioMode0
            );
            //-------------------------------------------------------------------
            //Configures the data rate and character format of a UART channel.
            //通信条件設定
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_SetLineCodingA(
                IntPtr uartDeviceObject,        //__in  H9bHandle_t uartDeviceObject,
                Int32 dataRate,                 //__in  uint32l_t dataRate,
                Byte charFormat,                //__in  uint8_t charFormat,    
                Byte parityType,                //__in  uint8_t parityType,
                Byte dataBits,                  //__in  uint8_t dataBits,
                Byte flowControl,               //__in  uint8_t flowControl,
                Byte gpioMode0                  //__in  uint8_t gpioMode0
                                                //  addressモード時 flowControl = 0x03
                                                //  それ以外は      flowControl = 0x01
                                                //  gpioMode0は0x39固定
            );
            //-------------------------------------------------------------------
            //Sets maximum allowable time for read and write operations.
            //送受信タイムアウト時間読み込み
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_GetTimeouts(
                IntPtr uartDeviceObject,    //__in      H9bHandle_t uartDeviceObject,
                ref Int32 writeTimeout,     //__in*     uint32l_t writeTimeout,
                ref Int32 readTimeout       //__in*     uint32l_t readTimeout
            );
            //-------------------------------------------------------------------
            //Sets maximum allowable time for read and write operations.
            //送受信タイムアウト時間設定
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_SetTimeouts(
                IntPtr uartDeviceObject,    //__in      H9bHandle_t uartDeviceObject,
                Int32 writeTimeout,         //__in      uint32l_t writeTimeout,
                Int32 readTimeout           //__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動作しきい値読み込み
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_GetUartRxFifoThreshold(
                IntPtr uartDeviceObject,        //__in  H9bHandle_t uartDeviceObject,
                ref Int16 rxFifoThreshold       //__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動作しきい値設定
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_SetUartRxFifoThreshold(
                IntPtr uartDeviceObject,     //__in     H9bHandle_t uartDeviceObject,
                Int16 rxFifoThreshold        //__in     uint16_t rxFifoThreshold
            );
            //-------------------------------------------------------------------
            //Close a device handle
            //UARTクローズ
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_CloseUart(
               IntPtr uartDeviceObject        //__in    H9bHandle_t uartDeviceObject
             );
            //-------------------------------------------------------------------
            //Returns the currently configured addresses used by the UART when
            //Address Match Mode is enabled.
            //AddressModeで使用する設定アドレス取得
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_GetUartAddress(
                IntPtr uartDeviceObject,        //__in  H9bHandle_t uartDeviceObject,
                ref Byte unicastAddress,        //__out uint8_t* unicastAddress,
                ref Byte multicastAddress       //__out uint8_t* multicastAddress
            );
            //-------------------------------------------------------------------
            //Sets the addresses the UART will respond to when Address Match Mode
            //is enabled.
            //AddressModeで使用する設定アドレス設定
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_SetUartAddress(
                IntPtr uartDeviceObject,    //__in      H9bHandle_t uartDeviceObject,
                Byte unicastAddress,        //__in      uint8_t unicastAddress,
                Byte multicastAddress       //__in      uint8_t multicastAddress
            );
            //-------------------------------------------------------------------
            //Reads data from a UART handle (USB IN transfers).
            //受信データ読み込み 
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_ReadUart(
                IntPtr uartDeviceObject,        //__in  H9bHandle_t uartDeviceObject,
                Int32 numberOfBytesToRead,      //__in  uint32l_t numberOfBytesToRead,
                byte[] byteBuffer,              //__out uint8_t* byteBuffer,
                ref Int32 numberOfBytesRead     //__out uint32l_t* numberOfBytesRead
            );
            //-------------------------------------------------------------------
            //Writes data to a UART handle (USB OUT transfers).
            //送信データ書き込み(送信) 
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_WriteUart(
                IntPtr uartDeviceObject,        //__in  H9bHandle_t uartDeviceObject,
                Int32 numberOfBytesToWrite,     //__in  uint32l_t numberOfBytesToWrite,
                byte[] byteBuffer,              // __in uint8_t* byteBuffer,
                ref Int32 numberOfBytesWritten  //_out  uint32l_t* numberOfBytesWritten
            );
            //-------------------------------------------------------------------
            //__indicates whether a UART channel is currently enabled for transmit and receive
            //送受信有効・無効設定確認
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_IsEnabled(
                IntPtr uartDeviceObject,    //__in      H9bHandle_t uartDeviceObject,
                ref Boolean enabled         //__out     boolean_t* enabled
            );
            //-------------------------------------------------------------------
            //Enables a UART channel for transmit and receive
            //送受信有効・無効設定 
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_EnableUart(
                IntPtr uartDeviceObject,    //__in      H9bHandle_t uartDeviceObject,
                Boolean enable              //__in      boolean_t enable
            );
            //-------------------------------------------------------------------
            //Flushes buffers and FIFOs in transmit, receive, or both directions.
            //送受信バッファを空にする
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_FlushBuffers(
                IntPtr uartDeviceObject,    //__in      H9bHandle_t uartDeviceObject,
                Boolean flushTransmit,      //__in      boolean_t flushTransmit,
                Boolean flushReceive        //__in      boolean_t flushReceive
            );
            //-------------------------------------------------------------------
            //Requests a UART to transmit a break signal.
            //ブレーク信号送信
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_SendBreak(
                IntPtr uartDeviceObject,    //__in      H9bHandle_t uartDeviceObject,
                Byte breakDuration          //__in      uint8_t breakDuration
            );
            //-------------------------------------------------------------------
            //Requests a UART to stop transmitting break.
            //ブレーク信号停止 
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_CancelBreak(
                IntPtr uartDeviceObject    //__in       H9bHandle_t uartDeviceObject,
            );
            //-------------------------------------------------------------------
            //Returns the current USB Manufacturer String.
            //製造メーカ名取得
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_GetOtpManufacturerString(
                IntPtr uartDeviceObject,        //__in  H9bHandle_t uartDeviceObject,
                ref byte stringLength,          //__out uint8_t* stringLength,
                byte[] manufacturingString      //__out char* manufacturingString
            );
            //-------------------------------------------------------------------
            //Returns the current USB Product String.
            //製品名称取得
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_GetOtpProductString(
                IntPtr uartDeviceObject,        //__in  H9bHandle_t uartDeviceObject,
                ref byte stringLength,          //__out uint8_t* stringLength,
                byte[] productString            //__out char* productString
            );

            //-------------------------------------------------------------------
            //Returns the release version of this API.
            //ファームウエアバージョン取得
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_GetSoftwareAPIAttributes(
                ref Byte majorVersion,          //__out uint8_t* majorVersion,
                ref Byte minorVersion,          //__out uint8_t* minorVersion,
                ref Boolean sReleaseVersion     //__out boolean_t* isReleaseVersion
            );
            //-------------------------------------------------------------------
            //__indicates whether a device handle is still open
            //オープン状況問い合わせ
            [DllImport("HDL9BITDLL.dll", CharSet = CharSet.Auto)]
            public static extern Int16 HDL9BIT_IsOpen(
                IntPtr uartDeviceObject,    //__in      H9bHandle_t uartDeviceObject,
                ref Boolean opened          //__out     boolean_t* opened
            );
            //------------------------------------------------------------------- 
            //-------------------------------------------------------------------
            // HDL9BITDLL.dll 使用 ここまで
            //-------------------------------------------------------------------
            //-------------------------------------------------------------------
        }

定数クラス定義


            //******************************************************************************
            //* API Parameter Encodings
            //******************************************************************************/
            ///*
            // * Number of data bits. Input to HDL9BIT_SetLineCoding() and
            // * output of HDL9BIT_GetLineCoding()
            // */
            public const Byte HDL9BIT_DATA_BITS_FIVE = 0;    //#define HDL9BIT_DATA_BITS_FIVE                   0x00
            public const Byte HDL9BIT_DATA_BITS_SIX = 1;     //#define HDL9BIT_DATA_BITS_SIX                            0x01
            public const Byte HDL9BIT_DATA_BITS_SEVEN = 2;   //#define HDL9BIT_DATA_BITS_SEVEN                  0x02
            public const Byte HDL9BIT_DATA_BITS_EIGHT = 3;   //#define HDL9BIT_DATA_BITS_EIGHT                  0x03
            public const Byte HDL9BIT_DATA_BITS_NINE = 4;    //#define HDL9BIT_DATA_BITS_NINE                   0x04
            //
            ///*
            // * Parity types. Input to HDL9BIT_SetLineCoding() and
            // * output of HDL9BIT_GetLineCoding()
            // */
            public const Byte HDL9BIT_PARITY_NONE = 0;   //#define HDL9BIT_PARITY_NONE                          0x00
            public const Byte HDL9BIT_PARITY_ODD = 1;    //#define HDL9BIT_PARITY_ODD                           0x01
            public const Byte HDL9BIT_PARITY_EVEN = 2;   //#define HDL9BIT_PARITY_EVEN                          0x02
            public const Byte HDL9BIT_PARITY_MARK = 3;   //#define HDL9BIT_PARITY_MARK                          0x03
            public const Byte HDL9BIT_PARITY_SPACE = 4;  //#define HDL9BIT_PARITY_SPACE                         0x04
            //
            ///*
            // * Character formats. Input to HDL9BIT_SetLineCoding() and
            // * output of HDL9BIT_GetLineCoding()
            // */
            public const Byte HDL9BIT_CHAR_FORMAT_STOP_SHORT = 0;   //#define HDL9BIT_CHAR_FORMAT_STOP_SHORT    0x00
            public const Byte HDL9BIT_CHAR_FORMAT_STOP_LONG = 1;    //#define HDL9BIT_CHAR_FORMAT_STOP_LONG     0x01
            //
            ///*
            // * Flow control types. Input to HDL9BIT_SetLineCoding() and
            // * output of HDL9BIT_GetLineCoding()
            // */
            public const Byte HDL9BIT_FLOW_CONTROL_NONE = 0;        //#define HDL9BIT_FLOW_CONTROL_NONE                 0x00
            public const Byte HDL9BIT_FLOW_CONTROL_HW_RTS_CTS = 1;  //#define HDL9BIT_FLOW_CONTROL_HW_RTS_CTS   0x01
            public const Byte HDL9BIT_FLOW_CONTROL_ADDRESS_MODE = 3;        //#define HDL9BIT_FLOW_CONTROL_ADDRESS_MODE         0x03
        }
 }

使用例

・全ソース
公開されている全てのAPIを使っているサンプルソースです。
//
//  HDL9BITDLL.dll 使用例
// HuMANDATA LTD. 2016-03-19 BY A.USHIRO
//


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices; //dll使用のために


namespace WindowsFormsApplication2
{


    public partial class HD9BITSAMPLE : Form
    {
        IntPtr HANHDL9BIT;
        Int16 productIDFilter;  //__in_opt      uint16_t productIDFilter,
        Int32 numUarts;          //__out        uint32l_t* numUarts
        bool isOpenUart;

        public HD9BITSAMPLE()
        {
            InitializeComponent();
        }
        // HDL9BITDLLの戻り値表示
        private void memo_ret(String msg, int ret)
        {
            richTextBox_memo.Focus();
            if (ret == HDL9BBITDEF.HDL9BIT_SUCCESS)
            {
                richTextBox_memo.AppendText( "*" + msg + " OK" + Environment.NewLine);
            }
            else
            {
                richTextBox_memo.AppendText("*" + msg + " ---- Error!" + Environment.NewLine);
            }

        }

        // 文字列表示
        private void memo(String msg)
        {
            richTextBox_memo.Focus();
            richTextBox_memo.AppendText( "[" + msg + "]" + Environment.NewLine);

        }

        //オープン処理
        private void button_devn_Click(object sender, EventArgs e)
        {
            int ret;
            Byte strlen8b = 0;
            Byte[] serialString = new Byte[512];
            Byte[] productString = new Byte[512];
            Byte[] manufacturingString = new Byte[512];
            Byte minorVersion = 0;
            Byte majorVersion = 0;
            Boolean isReleaseVersion = false;
            isOpenUart = false;

            string ss1;

            while (true)
            {

                //-------------------------------------------------------------------
                //Returns the release version of this API.
                //ファームウエアバージョン取得
                ret = HDLclass.HDL9BIT_GetSoftwareAPIAttributes(ref majorVersion, ref minorVersion, ref isReleaseVersion);
                memo_ret("HDL9BIT_GetSoftwareAPIAttributes", ret);
                memo("majorVersion=0x" + majorVersion.ToString("X2"));
                memo("minorVersion=0x" + minorVersion.ToString("X2"));
                //Returns the number of attached devices with matching IDs.
                //デバイス数取得
                productIDFilter = 0x4001;
                ret = HDLclass.HDL9BIT_GetNumUartsA(productIDFilter, (Byte)0, ref 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オープン
                label_devn.Text = "Device =" + numUarts.ToString("D");
                ret = HDLclass.HDL9BIT_OpenUartA(0, productIDFilter, (Byte)0, ref HANHDL9BIT);
                memo_ret("HDL9BIT_OpenUart", ret);
                if (ret != 0)
                {
                    break;
                }

                //-------------------------------------------------------------------
                //Returns the currently configures data rate and character format
                //通信条件の読み込み 
                Int32 dataRate = 0;             //__out uint32l_t* dataRate,
                Byte charFormat = 0;            //__out uint8_t* charFormat,    
                Byte parityType = 0;            //__out uint8_t* parityType,
                Byte dataBits = 0;              //__out uint8_t* dataBits,
                Byte flowControl = 0;           //__out uint8_t* flowControl,
                Byte gpioMode0 = 0;
                ret = HDLclass.HDL9BIT_GetLineCodingA(
                    HANHDL9BIT,        //__in   H9bHandle_t uartDeviceObject,
                    ref dataRate,     //__out   uint32l_t* dataRate,
                    ref charFormat,   //__out   uint8_t* charFormat,    
                    ref parityType,  //__out    uint8_t* parityType,
                    ref dataBits,     //__out   uint8_t* dataBits,
                    ref flowControl,  //__out   uint8_t* flowControl,
                    ref gpioMode0    //__out    uint8_t* gpioMode0
                 );
                memo_ret("HDL9BIT_GetLineCodingA", ret);
                //-------------------------------------------------------------------
                //Configures the data rate and character format of a UART channel.
                //通信条件設定
                dataBits = HDL9BBITDEF.HDL9BIT_DATA_BITS_NINE;   //'9bitモード指定
                parityType = HDL9BBITDEF.HDL9BIT_PARITY_NONE;
                dataRate = 12000000;
                flowControl = HDL9BBITDEF.HDL9BIT_FLOW_CONTROL_ADDRESS_MODE;
                gpioMode0 = 0x39;
                ret = HDLclass.HDL9BIT_SetLineCodingA(
                    HANHDL9BIT,        //__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 = dataRate.ToString("D");
                memo("dataRate = " + ss1);
                //-------------------------------------------------------------------
                //Sets maximum allowable time for read and write operations.
                //送受信タイムアウト時間読み込み
                Int32 writeTimeout = 0;
                Int32 readTimeout = 0;
                ret = HDLclass.HDL9BIT_GetTimeouts(
                    HANHDL9BIT,        //__in   H9bHandle_t uartDeviceObject,
                ref writeTimeout,      //__in*  uint32l_t writeTimeout,
                ref readTimeout        //__in*  uint32l_t readTimeout
                );
                writeTimeout = 500;
                readTimeout = 1000;
                //-------------------------------------------------------------------
                //Sets maximum allowable time for read and write operations.
                ret = HDLclass.HDL9BIT_SetTimeouts(
                    HANHDL9BIT,        //__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で使用する設定アドレス取得
                Byte unicastAddress = 0;        //__out uint8_t* unicastAddress,
                Byte multicastAddress = 0;      //__out uint8_t* multicastAddress
                ret = HDLclass.HDL9BIT_GetUartAddress(
                    HANHDL9BIT,        //__in   H9bHandle_t uartDeviceObject,
                     ref unicastAddress,        //__out uint8_t* unicastAddress,
                     ref 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 = HDLclass.HDL9BIT_SetUartAddress(
                    HANHDL9BIT,        //__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 = "0x"+ unicastAddress.ToString("X2");
                memo("unicastAddress = " + ss1);
                ss1 = "0x" + multicastAddress.ToString("X2");
                memo("multicastAddress = " + ss1);


                //-------------------------------------------------------------------
                //Returns the current setting for the number of bytes in the Rx FIFO
                //before flow control is asserted to the remote UART.
                //受信FIFO動作しきい値読み込み
                Int16 rxFifoThreshold = 0;      //__out uint16_t* rxFifoThreshold
                ret = HDLclass.HDL9BIT_GetUartRxFifoThreshold(
                    HANHDL9BIT,        //__in   H9bHandle_t uartDeviceObject,
                    ref 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 = HDLclass.HDL9BIT_SetUartRxFifoThreshold(
                    HANHDL9BIT,        //__in   H9bHandle_t uartDeviceObject,
                    rxFifoThreshold        //__in       uint16_t rxFifoThreshold
                );
                memo_ret("HDL9BIT_SetUartRxFifoThreshold", ret);
                ss1 = rxFifoThreshold.ToString("D");
                memo("rxFifoThreshold = " + ss1 );

                //-------------------------------------------------------------------
                //__indicates whether a device handle is still open
                //オープン状況問い合わせ
                ret = HDLclass.HDL9BIT_IsOpen(HANHDL9BIT, ref isOpenUart);
                memo_ret("HDL9BIT_IsOpen", ret);
                memo(" --Open Uart --");

                checkBox_isOpen.Checked = isOpenUart;
                //-------------------------------------------------------------------
                //Returns the current USB Serial Number String.
                //デバイス固有番号(シリアル番号)取得
                ret = HDLclass.HDL9BIT_GetOtpSerialNumberString(HANHDL9BIT, ref strlen8b, serialString);
                memo_ret("HDL9BIT_GetOtpSerialNumberString", ret);
                ss1 = System.Text.Encoding.ASCII.GetString(serialString);
                ss1 = ss1.Substring(0, ss1.IndexOf("\0")); memo(ss1);
                //-------------------------------------------------------------------
                //Returns the current USB Product String.
                //製品名称取得
                ret = HDLclass.HDL9BIT_GetOtpProductString(HANHDL9BIT, ref strlen8b, productString);
                memo_ret("HDL9BIT_GetOtpProductString", ret);
                ss1 = System.Text.Encoding.ASCII.GetString(productString);
                ss1 = ss1.Substring(0, ss1.IndexOf("\0")); memo(ss1);
                //-------------------------------------------------------------------
                //Returns the current USB Manufacturer String.
                //製造メーカ名取得
                ret = HDLclass.HDL9BIT_GetOtpManufacturerString(HANHDL9BIT, ref strlen8b, manufacturingString);
                memo_ret("HDL9BIT_GetOtpManufacturerString", ret);
                ss1 = System.Text.Encoding.ASCII.GetString(manufacturingString);
                ss1 = ss1.Substring(0, ss1.IndexOf("\0")); memo(ss1);
                //-------------------------------------------------------------------
                //Flushes buffers and FIFOs in transmit, receive, or both directions.
                //送受信バッファを空にする
                Boolean flushTransmit = true;
                Boolean flushReceive = true;
                ret = HDLclass.HDL9BIT_FlushBuffers(
                    HANHDL9BIT,    //__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 enabled = false;
                ret = HDLclass.HDL9BIT_IsEnabled(
                    HANHDL9BIT,    //__in       H9bHandle_t uartDeviceObject,
                     ref enabled   //__out      boolean_t* enabled
                 );
                memo_ret("HDL9BIT_IsEnabled", ret);
                //-------------------------------------------------------------------
                //Enables a UART channel for transmit and receive
                //送受信有効・無効設定 
                enabled = true;
                ret = HDLclass.HDL9BIT_EnableUart(
                    HANHDL9BIT,    //__in       H9bHandle_t uartDeviceObject,
                    enabled              //__in boolean_t enable
                 );
                memo_ret("HDL9BIT_EnableUart", ret);
                // loop end
                break;
            }
        }

        //クローズ
        private void button_close_Click(object sender, EventArgs e)
        {
            //-------------------------------------------------------------------
            //Close a device handle
            //UARTクローズ
            int ret;
            ret = HDLclass.HDL9BIT_CloseUart(HANHDL9BIT);
            memo_ret("HDL9BIT_CloseUart", ret);
            isOpenUart = false;
            checkBox_isOpen.Checked = isOpenUart;
        }

        // 起動時処理
        private void HD9BITSAMPLE_Load(object sender, EventArgs e)
        {

        }


        // BREAK ON
        private void button_break_on_Click(object sender, EventArgs e)
        {
            if (isOpenUart)
            {
                //-------------------------------------------------------------------
                //Requests a UART to transmit a break signal.
                //ブレーク信号送信
                int ret;
                ret = HDLclass.HDL9BIT_SendBreak(HANHDL9BIT, 0);
                memo_ret("HDL9BIT_SendBreak", ret);
            }
            else
            {
                memo("Openしてません");
            }
        }

        // BREAK OFF
        private void button_break_off_Click(object sender, EventArgs e)
        {
            if (isOpenUart)
            {
                //-------------------------------------------------------------------
                //Requests a UART to stop transmitting break.
                //ブレーク信号停止 
                int ret;
                ret = HDLclass.HDL9BIT_CancelBreak(HANHDL9BIT);
                memo_ret("HHDL9BIT_CancelBreak", ret);
            }
            else
            {
                memo("Openしてません");
            }
        }
        private void button_send_Click(object sender, EventArgs e)
        {
            while (true)
            {
                if (!isOpenUart)
                {
                    break;
                }

                int ret;
                byte[] byteBuffer = new byte[512];
                Int32 numberOfBytesToWrite;
                Int32 numberOfBytesWritten = 0;
                numberOfBytesToWrite = 10;
                // 相手アドレス
                Int32 address32 =    Convert.ToInt32(textBox_adress.Text, 16);
                Byte address = (Byte)(address32 & 0xff);
                //送信テキスト
                string s_text = textBox_stext.Text;
                byte[] byteBuffertemp = System.Text.Encoding.ASCII.GetBytes(s_text);
                int i = 0;
                byteBuffer[i++] = address;
                byteBuffer[i++] = 0x01;

                numberOfBytesToWrite = s_text.Length * 2 + 2;
                for ( int j= 0; i < s_text.Length *2 ; i+=2, j++)
                {
                    byteBuffer[i] = byteBuffertemp[j];
                    byteBuffer[i + 1] = 0;
                }
                //-------------------------------------------------------------------
                //Writes data to a UART handle (USB OUT transfers).
                //送信データ書き込み(送信) 
                ret = HDLclass.HDL9BIT_WriteUart(
                    HANHDL9BIT,               //__in    H9bHandle_t uartDeviceObject,
                    numberOfBytesToWrite,     //__in    uint32l_t numberOfBytesToWrite,
                    byteBuffer,               // __in   uint8_t* byteBuffer,
                    ref numberOfBytesWritten  //_out    uint32l_t* numberOfBytesWritten
                );
                memo_ret("HDL9BIT_WriteUart", ret);
                if (ret != HDL9BBITDEF.HDL9BIT_SUCCESS) break;
                //------------------------------------------------------------------
                // 受信
                //-------------------------------------------------------------------
                //Reads data from a UART handle (USB IN transfers).
                //受信データ読み込み 
                Int32 numberOfBytesToRead = 256;
                Int32 numberOfBytesRead = 0;
                memo(".....wait recieve..");
                ret = HDLclass.HDL9BIT_ReadUart(
                    HANHDL9BIT,               //__in    H9bHandle_t uartDeviceObject,
                    numberOfBytesToRead,      //__in    uint32l_t numberOfBytesToRead,
                    byteBuffer,              //__out    uint8_t* byteBuffer,
                    ref numberOfBytesRead     //__out   uint32l_t* numberOfBytesRead
                );
                if ((ret == HDL9BBITDEF.HDL9BIT_SUCCESS) ||
                    (ret == HDL9BBITDEF.HDL9BIT_ERR_UART_READ_FAIL_TIME_OUT))
                {
                    if (numberOfBytesRead != 0) { 
                        string rx_text;
                        rx_text = System.Text.Encoding.ASCII.GetString(byteBuffer);
                        rx_text = BitConverter.ToString(byteBuffer);
                        rx_text = rx_text.Substring(0, numberOfBytesRead * 3 - 1);
                       memo("RXD=" + rx_text);
                    }
                }
                else
                {

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

        }

        private void checkBox_isOpen_CheckedChanged(object sender, EventArgs e)
        {
            checkBox_isOpen.Checked = isOpenUart;
        }
弊社ではプログラミングに関数サポートを行う立場ではございませんが、USB-306を操作する上での具体的なご質問があればメールにてお問い合わせをお願いいたします。
 メールでのご質問はこちらへどうぞ

USB専用サイトのトップへ