アクセスカウンタ
Now loading...
最近のエントリー
アーカイブ
検索


リンク
Powered by
Movable Type3.35

2007年05月26日

SiliconLabs製CP2103のGPIO出力操作

便利ボードのためにCP2103のGPIOの出力を操作する簡単なソフトをWin32コンソールプログラムで作成した。
1.環境
WindowsXP
VisualC++Express
MicrosoftPlatformSDK
2.必要なもの:SiliconLabs社のアプリケーションノートAN223より下記ファイル。
CP210xRuntimeDLL.h
CP210xRuntimeDLL.def
CP210xRuntime.lib
CP210xRuntime.dll
3.ソースリスト
#include < windows.h >
#include < stdio.h >
#include < tchar.h >
#include "CP210xRuntimeDLL.h"

int execute_command( TCHAR cmd, LPBYTE data, TCHAR* port);

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR   portName[20];
    int     res;
    TCHAR   cmd;
    TCHAR*  pos;
    BYTE    result, outdata;

    if( argc < 3 ) {
        printf_s("CP2103GPIO COM1 r\nCP2103GPIO COM1 w 1000\n");
        return( 0 );
    }
    swprintf_s( portName, 20, L"\\\\.\\%c%c%c%c",
                    argv[1][0],argv[1][1],argv[1][2],argv[1][3]);
    cmd = *argv[2];
    switch( toupper( cmd )) {
        case 'R':
            res = execute_command( cmd, &result, portName );
            if( res == CP210x_SUCCESS ) {
                printf_s( "%x\n", result );
            }
            break;
        case 'W':
            pos = argv[3];
            outdata = 0;
            if( pos[0] == '1' ) outdata |= 0x08;
            if( pos[1] == '1' ) outdata |= 0x04;
            if( pos[2] == '1' ) outdata |= 0x02;
            if( pos[3] == '1' ) outdata |= 0x01;
            res = execute_command( cmd, &outdata, portName );
            break;
        default:
            printf_s( "CP2103GPIO COM1 r\nCP2103GPIO COM1 w 1000\n" );
            break;
    }
    return( res );
}

int execute_command( TCHAR cmd, LPBYTE data, TCHAR* port)
{
    HANDLE  hc;
    int     res;
    BYTE    readData;

    hc = CreateFile(    port,
                        GENERIC_READ | GENERIC_WRITE,
                        0,
                        NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                        NULL );
    if( hc == INVALID_HANDLE_VALUE ) {
        wprintf_s( L"Open Error:%s\n", port );
        res = 11;
    } else {
        switch( toupper( cmd )) {
            case 'R':
                res = CP210xRT_ReadLatch( hc, &readData );
                if( res == CP210x_SUCCESS ) {
                    *data = readData;
                    res = 0;
                } else {
                    printf_s( "Error %d\n", res );
                    res = 12;
                }
              break;
            case 'W':
                res = CP210xRT_WriteLatch( hc, 0x0f, *data );
                if( res == CP210x_SUCCESS ) {
                    res = 0;
                } else {
                    printf_s("Data %x\n",*data );
                    printf_s("Error %x\n", res );
                    res = 13;
                }
                break;
            default:
                res = 15;
                break;
        }
        CloseHandle( hc );
    }
    return( res );
}
4.使い方。(以下、対象ポートをCOM3とする)
GPIOの出力状態を読む。
CP2103GPIO.exe com3 r
GPIOの出力を変更する。(例:ビット3とビット2を1に、ビット1とビット0を0にする)
CP2103GPIO.exe com3 w 1100
これをバッチファイルなどで利用する。 Posted by やんもす at 2007年05月26日 23:56
コメント
コメントを投稿する


(非公開です。無記入でも可)

(公開されます。無記入でも可)

左の情報をクッキーに保存しますか?