C++ Builder CEから電源の制御をしてみる IndyのTIdTCPClientを使います
単純なやりとりなので、練習台には好都合です。
まずフォームは、

こんな感じです。TIdTCPClientとTLabelとTButtonを一個載せます。TIdLogDebugはオプションです。データの流れをチェックするのには便利です。sslでない時に使うのは珍しいかもしれませんけど。Unit1.cppは、
#include <vcl.h>
#pragma hdrstop
#include <System.SysUtils.hpp>
#include <System.Classes.hpp>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
void GetStatus()
{
Form1->IdTCPClient1->Host = "192.168.0.211"; // Server IP
Form1->IdTCPClient1->Port = 10002; // Server Port
Form1->IdTCPClient1->Connect();
AnsiString command = "POWR????";
static char CR = '\x0d';
command += CR;
try {
Form1->IdTCPClient1->IOHandler->Write(command);
TIdBytes receivedData;
Form1->IdTCPClient1->IOHandler->ReadBytes(receivedData, -1, false);
String result = TEncoding::UTF8->GetString(receivedData);
char first;
first = *(result.c_str());
if( first == '0' )
Form1->Label1->Caption = "OFF";
else if( first == '1' )
Form1->Label1->Caption = "ON";
else
Form1->Label1->Caption = "Unknown";
} __finally {
Form1->IdTCPClient1->Disconnect();
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
GetStatus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ToggleClick(TObject *Sender)
{
IdTCPClient1->Host = "192.168.0.211"; // Server IP
IdTCPClient1->Port = 10002; // Server Port
IdTCPClient1->Connect();
AnsiString command;
static char CR = '\x0d';
if( Label1->Caption == "ON" )
command = "POWR0 ";
else
command = "POWR1 ";
command += CR;
try {
IdTCPClient1->IOHandler->Write(command);
TIdBytes receivedData;
IdTCPClient1->IOHandler->ReadBytes(receivedData, -1, false);
String result = TEncoding::UTF8->GetString(receivedData);
} __finally {
IdTCPClient1->Disconnect();
}
GetStatus();
}
です。起動時に現在のLC-45W5の電源状態を拾います。前記事のコマンドだと、“POWR????”をAquosに送ります。TCPパケットで、AquosのIPと設定ポートへ、返ってきた文字(列)が’0’ならば、電源オフ状態、’1’ならば電源オン状態です。その状態をLabel1に書いて、“Toggle”ボタンを押すと、状態を切り替えます。つまりON->OFFないしOFF->ONとします。動いてる状態は、

こんな感じです。今は”ON”なので、”Toggle”を押すと”OFF”になります。ですが、わかりにくいので、Toggleのキャプションを変更先のONやOFFにしましょうかね。

“OFF”を押すと電源オフ。

“ON”を押すと電源オン。
この方が分かりやすいかな。このために少しコードを変更しました。
#include <vcl.h>
#pragma hdrstop
#include <System.SysUtils.hpp>
#include <System.Classes.hpp>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
void GetStatus()
{
Form1->IdTCPClient1->Host = "192.168.0.211"; // Server IP
Form1->IdTCPClient1->Port = 10002; // Server Port
Form1->IdTCPClient1->Connect();
AnsiString command = "POWR????";
static char CR = '\x0d';
command += CR;
try {
Form1->IdTCPClient1->IOHandler->Write(command);
TIdBytes receivedData;
Form1->IdTCPClient1->IOHandler->ReadBytes(receivedData, -1, false);
String result = TEncoding::UTF8->GetString(receivedData);
char first;
first = *(result.c_str());
if( first == '0' ){
Form1->Label1->Caption = "OFF";
Form1->Toggle->Caption = "ON";
}
else if( first == '1' ){
Form1->Label1->Caption = "ON";
Form1->Toggle->Caption = "OFF";
}
else
Form1->Label1->Caption = "Unknown";
} __finally {
Form1->IdTCPClient1->Disconnect();
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
GetStatus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ToggleClick(TObject *Sender)
{
IdTCPClient1->Host = "192.168.0.211"; // Server IP
IdTCPClient1->Port = 10002; // Server Port
IdTCPClient1->Connect();
AnsiString command;
static char CR = '\x0d';
if( Label1->Caption == "ON" ){
command = "POWR0 ";
Toggle->Caption = "OFF";
}
else {
command = "POWR1 ";
Toggle->Caption = "ON";
}
command += CR;
try {
IdTCPClient1->IOHandler->Write(command);
TIdBytes receivedData;
IdTCPClient1->IOHandler->ReadBytes(receivedData, -1, false);
String result = TEncoding::UTF8->GetString(receivedData);
} __finally {
IdTCPClient1->Disconnect();
}
GetStatus();
}
電源のON/OFFだけなんですけどね。機能を色々と盛り込むと、純正のアプリ、”TVリモコン”

と同様なことがPCからできますけどね。やろうと思えば。
コメント