본문 바로가기

[Proramming]/C#

C# 을이용한 하드웨어 제어 - USB FIFO FT245 제어

C# 을이용한 하드웨어 제어 - USB FIFO FT245 제어

C#을 이용하면 윈도우 인터페이스는 쉽게 제작할 수 있다. FTDI사의 USB FIFO FT245제어를 C#으로 해 봤는데 생각보다 쉽고 간단하게 작성할 수 있다. FT2232, FT232도 동일하게 사용가능하다.




FT245 C# Programming Source Code

        //------------------------------------------------------------------------------
        //FT245 DLL 등록
        [DllImport("kernel32.dll")]
        extern static short QueryPerformanceCounter(ref long x);
        [DllImport("kernel32.dll")]
        extern static short QueryPerformanceFrequency(ref long x);


        // FT Open
        [DllImport("ftd2xx.dll")]
        unsafe private static extern uint FT_Open(Int16 DeviceNumber, uint* ftHandle);

        // FT Close
        [DllImport("ftd2xx.dll")]
        private static extern uint FT_Close(uint ftHandle);

        // FT_SetBitMode
        [DllImport("ftd2xx.dll")]
        private static extern uint FT_SetBitMode(uint ftHandle, Byte Mask, Byte Mode);

        // FT_GetBitMode    
        [DllImport("ftd2xx.dll")]
        unsafe private static extern uint FT_GetBitMode(uint ftHandle,
                      [MarshalAs(UnmanagedType.LPArray)] byte[] bdata);

        // FT_SetBaudRate
        [DllImport("ftd2xx.dll")]
        private static extern uint FT_SetBaudRate(uint ftHandle, uint BaudRate);

        // FT_Write
        [DllImport("ftd2xx.dll")]
        unsafe private static extern uint FT_Write(uint ftHandle,
                     [MarshalAs(UnmanagedType.LPArray)] byte[] bdata,
                     uint BufferSize, uint* BytesWriten);
        //------------------------------------------------------------------------------


        //FT245 초기화
        unsafe private void button1_Click(object sender, EventArgs e)
        {
            const uint FT_OK = 0;
            uint ftHandle;          

            m_ftStatus = FT_Open(0, &ftHandle);
            m_ftHandle = ftHandle;

            if (m_ftStatus != FT_OK)
            {
                return;   // 에러처리
            }

            // BitBang Mode
            m_ftStatus = FT_SetBitMode(m_ftHandle, 0xFF, 0x01);
            m_ftStatus = FT_SetBaudRate(m_ftHandle, 115200);

            //연결되면 Icon Image변경
            button1.Image = global::WindowsFormsApplication1.Properties.Resources.connect;
        }

        unsafe private void button2_Click(object sender, EventArgs e)
        {
            //데이터 출력 - LED On
            uint writesize;
            InOut_data[0] = 0x00;

            FT_Write(m_ftHandle, InOut_data, 1, &writesize);
        }

        unsafe private void button3_Click(object sender, EventArgs e)
        {
            //데이터 출력 - LED Off
            uint writesize;
            InOut_data[0] = 0xFF;

            FT_Write(m_ftHandle, InOut_data, 1, &writesize);
        }


FT245 USB제어 테스트 동영상

반응형