|
|
当サイトは、玄箱PRO (KURO-BOX/Pro)を中心とした組み込み、Linuxと電子工作を扱っています。
会社案内
情報セキュリティおよび個人情報の取り扱いについて コメントとトラックバックは、spamを予防するために、編集担当が公開の作業をするまで非公開になっています。コメントはそれぞれ投稿した人のものです。 |
| 送信データのフォーマット | ||
| バイト列 | 内容 | 実際に使われる値(XPort) |
| 0 | 指定するコマンド | 設定確認(0x10)、出力設定(0x1a)など |
| 1 | パラメータ1の1番目 | CP3(bit2) CP2(bit1) CP1(bit0) |
| 2 | パラメータ1の2番目 | ダミー |
| 3 | パラメータ1の3番目 | ダミー |
| 4 | パラメータ1の4番目 | ダミー |
| 5 | パラメータ2の1番目 | ダミー |
| 6 | パラメータ2の2番目 | ダミー |
| 7 | パラメータ2の3番目 | ダミー |
| 8 | パラメータ2の4番目 | ダミー |
| GPIO control protocol | |
| Byte 0 | Command Types |
| 10h | Get functions |
| 11h | Get directions (input or output) |
| 12h | Get active levels (high active or low active) |
| 13h | Get current states (active or not active) |
| 19h | Set directions |
| 1Ah | Set active levels |
| 1Bh | Set current states |
kurobox-pro:/home/k-wada/program/xport/LED# ls Makefile onoff onoff.c onoff.o kurobox-pro:/home/k-wada/program/xport/LED# ./onoff 192.168.1.22 0 <0> :1a: :0: :0: :0: :0: kurobox-pro:/home/k-wada/program/xport/LED# kurobox-pro:/home/k-wada/program/xport/LED# ./onoff 192.168.1.22 1 <1> :1a: :7: :0: :0: :0: kurobox-pro:/home/k-wada/program/xport/LED#コマンド(onoff)の後に IP アドレスを続けて、最後に 0 (オン)か 1 (オフ)を指定します。必要なデータを送出後にはステータスを表示させています。
/* XPort LED on/off 2008-4-10 */ #include紹介している3本の汎用端子は複数の機能を持っていますから、まず始めに例のデバイス・インストーラ(Device Installer)を使って設定をしなくてはなりません。ここでは LED を点灯させるわけですから"出力"に設定します。ところが、最新の Device Installer はこの設定が簡単にはできない様子です。現時点でのバージョンは 4.1.0.14 となっています。#include #include #include #include static unsigned char status[] = {0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; static unsigned char on[] = {0x1a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; static unsigned char off[] = {0x1a, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00}; //static unsigned char mesg[] = {0x01}; #define PROT_NO 30704 /* ポート番号 */ #define BUF_MAX 5 /* 通信バッファ・サイズ */ static struct sockaddr_in sv_addr; /* サーバ・アドレス */ static struct hostent *sv_ip; /* サーバIPアドレス */ static int sid; /* ソケット識別子 */ static char buf[BUF_MAX]; /* 通信バッファ */ /* クライアント・メイン */ int main(int argc, char *argv[]) { int rtn; /* 返却値 */ int i; for(i = 0; i < BUF_MAX; i++) { buf[i] = 0xff; } /* コネクション型ソケットの作成 (socket) */ sid = socket(AF_INET, SOCK_STREAM, 0); if (sid < 0) { perror("cl:socket"); exit(1); } /* サーバのIPアドレスを取得 */ sv_ip = gethostbyname(argv[1]); if (sv_ip == NULL) { perror("cl:gethostbyname"); exit(1); } /* ソケットの接続要求 (connect) */ bzero((char *)&sv_addr, sizeof(sv_addr)); sv_addr.sin_family = AF_INET; sv_addr.sin_port = htons(PROT_NO); memcpy((char *)&sv_addr.sin_addr, (char *)sv_ip->h_addr, sv_ip->h_length); rtn = connect(sid, &sv_addr, sizeof(sv_addr)); if (rtn < 0) { perror("cl:connect"); close(sid); exit(1); } // printf("%c\n", *argv[2]); if (*argv[2] == '1') { send(sid, on, sizeof(on), 0); printf("<1>\n"); } if (*argv[2] == '0') { send(sid, off, sizeof(off), 0); printf("<0>\n"); } if (*argv[2] == '9') { send(sid, status, sizeof(status), 0); printf("<9>\n"); } // printf("\n"); /* メッセージ通信処理 (send/recv) */ rtn = recv(sid, buf, BUF_MAX, 0); if (rtn < 0) perror("cl:recv"); for(i = 0; i < BUF_MAX; i++) { printf(":%x:\n", (unsigned char)buf[i]); if(i == 6) { printf("-------------\n"); } } /* ソケットの開放 (shutdown/close) */ rtn = shutdown(sid, 2); if (rtn < 0) perror("cl:shutdown"); close(sid); exit(0); }
このブログ記事を参照しているブログ一覧: XPort活用 「その2」《パトライト編》 前編
このブログ記事に対するトラックバックURL: http://www.eleki-jack.com/mt/mt-tb.cgi/1620
おすすめ書籍 |
![]() |
コメントする