private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
/* Form閉じる */
this->Close();
}
//
// LED
//
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
OutputPacketBuffer[0] = 0; // Report ID
OutputPacketBuffer[1] = 0x31; // LED制御コマンド
OutputPacketBuffer[2] = 0x31; // LED1アドレス
if(button3->BackColor == Color::Green)
OutputPacketBuffer[3] = 0x31; // On制御
else
OutputPacketBuffer[3] = 0x30; // Off制御
/* コマンド送信 */
WriteFile(WriteHandle, &OutputPacketBuffer, 65, &BytesWritten, 0);
/* 状態送信要求 */
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
/* 状態色変更 */
if(InputPacketBuffer[3] == 0x31) // オンか?
button3->BackColor =Color:: Red; // オンなら赤
else
button3->BackColor = Color::Green; // オフなら緑
}
//
// カメラデータをGet
//
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
String^ temp_s;
char* temp_c0;
char temp_c1;
char temp_c2;
char temp_c3;
read_ok = FALSE;
temp_s = textBox2->Text;
// MessageBox::Show(temp_s);
if ( temp_s->Length != 2 )
{
MessageBox::Show("正しい値を入力してください。",
"エラー",
MessageBoxButtons::OK,
MessageBoxIcon::Exclamation );
}
else
{
// marshal_context ^ context = gcnew marshal_context();
// const char* str = context->marshal_as(temp_s);
// delete context;
temp_c0 = systemStringToChar(temp_s);
temp_c1 = temp_c0[0];
temp_c2 = temp_c0[1];
temp_c3 = temp_c0[2];
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("db1 temp_c1 =" + temp_c1);
System::Diagnostics::Debug::WriteLine("db1 temp_c2 =" + temp_c2);
System::Diagnostics::Debug::WriteLine("db1 temp_c3 =" + temp_c3);
#endif
if( !Uri::IsHexDigit( temp_c1 ) || !Uri::IsHexDigit( temp_c2 ))
{
MessageBox::Show("正しい値を入力してください。",
"hexエラー",
MessageBoxButtons::OK,
MessageBoxIcon::Exclamation );
}
else
{
temp_c1 = toHex(temp_c0);
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("temp_c1 = "+temp_c1.ToString());
#endif
OutputPacketBuffer[0] = 0; // Report ID
OutputPacketBuffer[1] = 0x32; // cam コマンド ic 0
OutputPacketBuffer[2] = 0x31; // cam getコマンド ic 1
OutputPacketBuffer[3] = temp_c1; // get address ic 2
/* コマンド送信 */
WriteFile(WriteHandle, &OutputPacketBuffer, 65, &BytesWritten, 0);
/* 状態送信要求 */
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
/* 状態色変更 */
if (InputPacketBuffer[1] == 0x32 && InputPacketBuffer[2] == 0x31 )
{
read_ok = TRUE;
textBox3->Text = "get cam data add =";
temp_c1 = InputPacketBuffer[3];
// textBox3->Text += temp_c1.ToString("X");
textBox3->Text += System::String::Format ("{0:X02}",temp_c1);
textBox3->Text += ", cam data =";
temp_c1 = InputPacketBuffer[4];
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("returnd temp_c1 = "+temp_c1.ToString());
#endif
// textBox3->Text += temp_c1.ToString("X");
textBox3->Text += System::String::Format ("{0:X02}",temp_c1);
}
else
{
textBox3->Text = "not found data";
}
}
}
}
//
//
//
private: int check_hex(char ch) {
if('0' <= ch && ch <= '9') { return ch - '0'; }
else if('a' <= ch && ch <= 'z') { return ch - 'a' + 10; }
else if('A' <= ch && ch <= 'Z') { return ch - 'A' + 10; }
else return -1;
}
private: char toHex(char* s) {
int len = strlen(s);
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("chr len ="+len.ToString());
#endif
int i = 0;
int j = 0;
unsigned char l = 0;
while(len > 0) {
i = check_hex(s[j]);
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("hex comp ="+i.ToString());
#endif
if ( i < 0 )
{
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("hex sub error");
#endif
}
j++;
l = l * 16 + i;
len--;
}
return l;
}
private: char* systemStringToChar(System::String^ systemStr)
{
using namespace System;
using namespace System::Runtime::InteropServices;
// 文字コードは、環境に合わせる(普通はUTF-8)
// int len = Syetem::text::Encoding::getEncoding("UTF-8")->GetbyteCount(systemStr);
int len = systemStr->Length;
if( len > 0 ){
char* rtnSts = new char[len+1];
memset(rtnSts, 0x00, sizeof(char)*len+1);
const char* buf = static_cast((Marshal::StringToHGlobalAnsi(systemStr)).ToPointer());
// 取得した文字列をコピー
strncpy_s(rtnSts, len+1, buf, _TRUNCATE);
// メモリ開放
Marshal::FreeHGlobal(IntPtr((void*)buf));
return rtnSts;
}
return NULL;
}
//
//
//
private: System::Void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
//
// cam data out
//
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
String^ temp_s;
char* temp_c0;
char temp_c1;
char temp_c2;
char temp_c3;
if (!read_ok)
{
MessageBox::Show("先に読み込みをして下さい。",
"エラー",
MessageBoxButtons::OK,
MessageBoxIcon::Exclamation );
return;
}
temp_s = textBox2->Text;
// MessageBox::Show(temp_s);
if ( temp_s->Length != 2 )
{
MessageBox::Show("正しい値を入力してください。",
"エラー",
MessageBoxButtons::OK,
MessageBoxIcon::Exclamation );
}
else
{
temp_c0 = systemStringToChar(temp_s);
temp_c1 = temp_c0[0];
temp_c2 = temp_c0[1];
temp_c3 = temp_c0[2];
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("db1 temp_c1 =" + temp_c1);
System::Diagnostics::Debug::WriteLine("db1 temp_c2 =" + temp_c2);
System::Diagnostics::Debug::WriteLine("db1 temp_c3 =" + temp_c3);
#endif
if( !Uri::IsHexDigit( temp_c1 ) || !Uri::IsHexDigit( temp_c2 ))
{
MessageBox::Show("正しい値を入力してください。",
"hexエラー",
MessageBoxButtons::OK,
MessageBoxIcon::Exclamation );
}
else
{
temp_c1 = toHex(temp_c0);
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("temp_c1 = "+temp_c1.ToString());
#endif
OutputPacketBuffer[0] = 0; // Report ID
OutputPacketBuffer[1] = 0x33; // cam コマンド ic 0
OutputPacketBuffer[2] = 0x31; // cam getコマンド ic 1
OutputPacketBuffer[3] = temp_c1; // write address ic 2
temp_s = textBox4->Text;
// MessageBox::Show(temp_s);
if ( temp_s->Length != 2 )
{
MessageBox::Show("正しい値を入力してください。",
"エラー",
MessageBoxButtons::OK,
MessageBoxIcon::Exclamation );
}
else
{
temp_c0 = systemStringToChar(temp_s);
temp_c1 = temp_c0[0];
temp_c2 = temp_c0[1];
temp_c3 = temp_c0[2];
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("db1 temp_c1 =" + temp_c1);
System::Diagnostics::Debug::WriteLine("db1 temp_c2 =" + temp_c2);
System::Diagnostics::Debug::WriteLine("db1 temp_c3 =" + temp_c3);
#endif
if( !Uri::IsHexDigit( temp_c1 ) || !Uri::IsHexDigit( temp_c2 ))
{
MessageBox::Show("正しい値を入力してください。",
"hexエラー",
MessageBoxButtons::OK,
MessageBoxIcon::Exclamation );
}
else
{
temp_c1 = toHex(temp_c0);
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("temp_c1 = "+temp_c1.ToString());
#endif
OutputPacketBuffer[4] = temp_c1; // write data ic 3
/* コマンド送信 */
WriteFile(WriteHandle, &OutputPacketBuffer, 65, &BytesWritten, 0);
/* 状態送信要求 */
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
/* 状態色変更 */
if (InputPacketBuffer[1] != 0x33 || InputPacketBuffer[2] != 0x31 )
{
textBox5->Text = "return error data";
}
else
{
textBox5->Text = "write cam data add =";
temp_c1 = InputPacketBuffer[3];
textBox5->Text += System::String::Format ("{0:X02}",temp_c1);
textBox5->Text += ", cam data =";
temp_c1 = InputPacketBuffer[4];
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("returnd temp_c1 = "+temp_c1.ToString());
#endif
textBox5->Text += System::String::Format ("{0:X02}",temp_c1);
}
} //hex
} // length
}
}
}
//
// チェックボタン
//
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) {
int i_temp;
switch (comboBox1->SelectedIndex)
{
case 0:
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("select 1");
#endif
OutputPacketBuffer[0] = 0; // Report ID
OutputPacketBuffer[1] = 0x34; // cam コマンド ic 0
OutputPacketBuffer[2] = 0x31; // cam getコマンド ic 1
/* コマンド送信 */
WriteFile(WriteHandle, &OutputPacketBuffer, 65, &BytesWritten, 0);
/* 状態送信要求 */
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
/* 状態色変更 */
if (InputPacketBuffer[1] == 0x34 && InputPacketBuffer[2] == 0x31 )
{
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("returnd");
#endif
i_temp = InputPacketBuffer[3] * 256 + InputPacketBuffer[4];
textBox6->Text = "get cam data H count =";
// textBox6->Text += System::String::Format ("{0:X02}",InputPacketBuffer[3]);
// textBox6->Text += System::String::Format ("{0:X02}",InputPacketBuffer[4]);
textBox6->Text += i_temp.ToString();
}
else
{
textBox6->Text = "not found data";
}
break;
case 1:
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("select 2");
#endif
break;
default:
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("not select");
#endif
break;
}
}
//
// 画像取込
//
private: System::Void button7_Click(System::Object^ sender, System::EventArgs^ e) {
// button7->BackColor = Color::Green; // オフなら緑
capture_cam();
// button7->ResetBackColor();
}
private: void capture_cam() {
this->Cursor = System::Windows::Forms::Cursors::WaitCursor;
OutputPacketBuffer[0] = 0; // Report ID
OutputPacketBuffer[1] = 0x35; // cam コマンド ic 0
OutputPacketBuffer[2] = 0x31; // cam getコマンド ic 1
/* コマンド送信 */
WriteFile(WriteHandle, &OutputPacketBuffer, 65, &BytesWritten, 0);
/* 状態送信要求 */
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
/* 状態色変更 */
if (InputPacketBuffer[1] == 0x35 && InputPacketBuffer[2] == 0x31)
{
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("returnd");
#endif
}
else
{
button7->BackColor = Color::Red; //
}
/* 状態送信要求 */
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
if (InputPacketBuffer[1] == 0x35 && InputPacketBuffer[2] == 0x32)
{
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("returnd get pic");
#endif
}
else
{
button7->BackColor = Color::Red; //
}
this->Cursor = System::Windows::Forms::Cursors::Default;
}
//
//
//
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
timer1->Enabled = FALSE;
capture_cam();
capture_data();
timer1->Enabled = TRUE;
}
//
// 画像転送
//
private: System::Void button8_Click(System::Object^ sender, System::EventArgs^ e) {
capture_data();
}
private: void capture_data() {
int ix_v;
int ix_h;
int ix_hp; // 画面描画用
BOOL First_flg;
int ix_hex;
String^ temp_s;
int sw1;
unsigned char temp_R1;
unsigned char temp_G1;
unsigned char temp_B1;
unsigned char temp_R2;
// unsigned char temp_G2;
// unsigned char temp_B2;
Color temp_col;
unsigned char col_fact;
this->Cursor = System::Windows::Forms::Cursors::WaitCursor;
//
// QVGA 544 * 408
//
//ピクチャボックスと同じ大きさのBitmapを作成
//PictureBoxと同じ大きさのBitmapクラスを作成する。
if ( pictureBox1->Image == nullptr )
{
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("pictureBox1->Image is NULL");
#endif
}
else
{
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("pictureBox1->Image is not NULL");
#endif
}
Bitmap^ bmp = gcnew Bitmap(pictureBox1->Width, pictureBox1->Height);
pictureBox1->Image = bmp;
//ピクチャボックスからGraphicsの作成
Graphics^ g = Graphics::FromImage(pictureBox1->Image);
//ピクチャボックスの背景色でクリアする。
g->Clear(pictureBox1->BackColor);
System::IO::StreamWriter^ temp_file = gcnew System::IO::StreamWriter("Camdata_Hex_Test.txt");
temp_file->Write("カメラからのデータ\r\n");
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("write test");
#endif
col_fact = 8;
First_flg = TRUE;
for ( ix_v = 0; ix_v < 360; ix_v++ )
{ // loop V
temp_s = "";
ix_hp = 0;
for ( ix_h = 0; ix_h < 16; ix_h++ ) // 480*2/58-->16+32
{ // loop H
OutputPacketBuffer[0] = 0; // Report ID
OutputPacketBuffer[1] = 0x35; // cam コマンド ic 0
OutputPacketBuffer[2] = 0x33; // cam getコマンド ic 1
if ( First_flg )
{
OutputPacketBuffer[3] = 0x00; // cam getコマンド ic 2
First_flg = FALSE;
}
else OutputPacketBuffer[3] = 0x01; // cam getコマンド ic 2
/* コマンド送信 */
WriteFile(WriteHandle, &OutputPacketBuffer, 65, &BytesWritten, 0);
/* 状態送信要求 */
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
/* 状態色変更 */
if (InputPacketBuffer[1] == 0x35 && InputPacketBuffer[2] == 0x33)
{
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("returnd get");
// #endif
sw1 = 0;
for ( ix_hex = 4; ix_hex < 62; ix_hex++ ) // 58+4
{ // loop buf
temp_s += System::String::Format ("{0:X02}",InputPacketBuffer[ix_hex]);
sw1++;
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("sw1 ="+sw1);
// #endif
if ( sw1 == 1 )
{
temp_R2 = InputPacketBuffer[ix_hex] / 16;
temp_R1 = InputPacketBuffer[ix_hex] - ( temp_R2 * 16 );
// カラーの設定のみ
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("temp_R1 ="+temp_R1+" temp_G1 ="+temp_G1);
// #endif
}
else if ( sw1 == 2 )
{
temp_G1 = InputPacketBuffer[ix_hex] / 16;
temp_B1 = InputPacketBuffer[ix_hex] - ( temp_G1 * 16 );
// temp_G1 = temp_G1 * 8 + temp_G2;
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("temp_B1 ="+temp_B1+" temp_R2 ="+temp_R2);
// #endif
// カラーの設定 描画
temp_col = Color::FromArgb( 255, temp_R1*col_fact, temp_G1*col_fact, temp_B1*col_fact );
bmp->SetPixel( ix_hp, ix_v, temp_col );
ix_hp++;
sw1 = 0;
}
else
{
sw1 = 0;
// temp_G2 = InputPacketBuffer[ix_hex] / 16;
// temp_B2 = InputPacketBuffer[ix_hex] - ( temp_G2 * 16 );
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("temp_G2 ="+temp_G2+" temp_B2 ="+temp_B2);
// #endif
// カラーの設定 描画
// temp_col = Color::FromArgb( 255, temp_R2*16, temp_G2*16, temp_B2*16 );
// bmp->SetPixel( ix_hp, ix_v, temp_col );
// ix_hp++;
}
} // loop buf
// temp_s += "\r\n";
// temp_file->Write(temp_s);
// temp_s = "";
}
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("cam data get");
// #endif
} // loop H
OutputPacketBuffer[0] = 0; // Report ID
OutputPacketBuffer[1] = 0x35; // cam コマンド ic 0
OutputPacketBuffer[2] = 0x33; // cam getコマンド ic 1
OutputPacketBuffer[3] = 0x02; // cam getコマンド ic 2
/* コマンド送信 */
WriteFile(WriteHandle, &OutputPacketBuffer, 65, &BytesWritten, 0);
/* 状態送信要求 */
ReadFile(ReadHandle, &InputPacketBuffer, 65, &BytesRead, 0);
/* 状態色変更 */
if (InputPacketBuffer[1] == 0x35 && InputPacketBuffer[2] == 0x33)
{
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("returnd get2");
// #endif
sw1 = 0;
for ( ix_hex = 4; ix_hex < 36; ix_hex++ ) // 32+4
{ // loop buf
temp_s += System::String::Format ("{0:X02}",InputPacketBuffer[ix_hex]);
sw1++;
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("sw1 ="+sw1);
// #endif
if ( sw1 == 1 )
{
temp_R2 = InputPacketBuffer[ix_hex] / 16;
temp_R1 = InputPacketBuffer[ix_hex] - ( temp_R2 * 16 );
// カラーの設定のみ
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("temp_R1 ="+temp_R1+" temp_G1 ="+temp_G1);
// #endif
}
else if ( sw1 == 2 )
{
temp_G1 = InputPacketBuffer[ix_hex] / 16;
temp_B1 = InputPacketBuffer[ix_hex] - ( temp_G1 * 16 );
// temp_G1 = temp_G1 * 8 + temp_G2;
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("temp_B1 ="+temp_B1+" temp_R2 ="+temp_R2);
// #endif
// カラーの設定 描画
temp_col = Color::FromArgb( 255, temp_R1*col_fact, temp_G1*col_fact, temp_B1*col_fact );
bmp->SetPixel( ix_hp, ix_v, temp_col );
ix_hp++;
sw1 = 0;
}
else
{
sw1 = 0;
// temp_G2 = InputPacketBuffer[ix_hex] / 16;
// temp_B2 = InputPacketBuffer[ix_hex] - ( temp_G2 * 16 );
// #ifdef _DEBUG
// System::Diagnostics::Debug::WriteLine("temp_G2 ="+temp_G2+" temp_B2 ="+temp_B2);
// #endif
// カラーの設定 描画
// temp_col = Color::FromArgb( 255, temp_R2*16, temp_G2*16, temp_B2*16 );
// bmp->SetPixel( ix_hp, ix_v, temp_col );
// ix_hp++;
}
} // loop buf
// temp_s += "\r\n";
// temp_file->Write(temp_s);
// temp_s = "";
}
temp_s += "\r\n";
temp_file->Write(temp_s );
temp_s = "";
} // loop V
temp_file->Close();
textBox7->Text = "";
for ( ix_hex = 0; ix_hex < 30; ix_hex++ )
{
textBox7->Text += System::String::Format ("{0:X02}",InputPacketBuffer[ix_hex]);
}
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("picture write test");
#endif
this->Cursor = System::Windows::Forms::Cursors::Default;
}
private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
#ifdef _DEBUG
System::Diagnostics::Debug::WriteLine("paint picture");
#endif
// e->Graphics->DrawLine( System::Drawing::Pens::Red, 0, 0,
// pictureBox1->Right - pictureBox1->Left, pictureBox1->Bottom - pictureBox1->Top );
}
//
// 連続取込
//
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) {
if ( timer_get )
{
timer_get = FALSE;
button9->ResetBackColor();
timer1->Enabled = FALSE;
}
else
{
timer_get = TRUE;
button9->BackColor = Color::Green; // オフなら緑
timer1->Interval = 1000;
timer1->Enabled = TRUE;
}
}
};
}
|