//visual studio 2008 c++/cli windowsフォームアプリケーション #pragma once #include #define MAXY 8000 namespace uv { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// /// Form1 の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// public ref class Form1 : public System::Windows::Forms::Form { delegate void mySetTextCallback(String^ text); public: Form1(void) { InitializeComponent(); listBox1->Items->Clear(); array^ serialPorts = nullptr; serialPorts = System::IO::Ports::SerialPort::GetPortNames(); for each(String^ port in serialPorts) { listBox1->Items->Add(port); } serialPort1->BaudRate = 9600; //9600bps serialPort1->Parity = System::IO::Ports::Parity::None; //パリティなし serialPort1->DataBits = 8; //データ長 8ビット serialPort1->StopBits = System::IO::Ports::StopBits::One; //ストップビット 1 serialPort1->NewLine = "\r\n";//ReadLine(),WriteLine()が末尾と判断する値の設定 Graphics^ graphics = Graphics::FromImage(pictureBox1->InitialImage); Image^ image = gcnew Bitmap(pictureBox1->Size.Width, pictureBox1->Size.Height, graphics); Graphics^ graphics2 = Graphics::FromImage(image); graphics2->Clear(Color::White); pictureBox1->Image = image; pen = gcnew Pen(Color::Blue); white = gcnew Pen(Color::White); red = gcnew Pen(Color::Red); gray = gcnew Pen(Color::Gainsboro); lasty=299; timer1->Interval = 5000;//5秒 timerflag=true; label6->Text = Convert::ToString(MAXY); label5->Text = Convert::ToString(MAXY*3/4); label4->Text = Convert::ToString(MAXY/2); label10->Text = Convert::ToString(MAXY/4); } private: System:: Void Form1_FormClosed(System::Object^ sender, System::Windows::Forms::FormClosedEventArgs^ e) { if(serialPort1->IsOpen==false){return;} this->runflag=0; serialPort1->Write("s\r"); this->serialPort1->DataReceived -= gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived); serialPort1->Close(); } protected: /// /// 使用中のリソースをすべてクリーンアップします。 /// ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button2; private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::Label^ label2; private: System::Windows::Forms::PictureBox^ pictureBox1; protected: private: int runflag; private: int lasty; private: bool timerflag; Pen^ pen; Pen^ white; Pen^ red; private: System::Windows::Forms::Label^ label3; private: System::Windows::Forms::Label^ label4; private: System::Windows::Forms::Label^ label5; private: System::Windows::Forms::Label^ label6; private: System::Windows::Forms::Label^ label7; private: System::Windows::Forms::Label^ label8; private: System::Windows::Forms::Label^ label9; private: System::Windows::Forms::Label^ label10; Pen^ gray; private: System:: Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) { SetText(serialPort1->ReadExisting()); } void SetText(String^ text) { int a,y; double b; if (this->textBox1->InvokeRequired) //invoke メソッドを呼び出す必要があるなら //別スレッドからの呼び出しであるなら { mySetTextCallback^ d = gcnew mySetTextCallback(this,&uv::Form1::SetText); //デリゲート関数へのインスタンスを生成し、SetText()関数へ割り当てる。 this->BeginInvoke(d,gcnew String(text)); //BeginInvokeにしないとserialPort1->Close()のときフリーズする // this->Invoke(d,gcnew String(text)); //デリゲートの非同期実行機能により、メインスレッドの //textBox1テキストボックスへ、受信時刻とは時間差をもって書き込む } else { if(this->runflag==0){return;} array^ sep = gcnew array{'\n'}; array^ TextArr = text->Split(sep,StringSplitOptions::RemoveEmptyEntries); textBox1->Text="hoge"; for each(String^ resline in TextArr) { resline=resline->Replace("\r",""); resline=resline->Replace("\n",""); // if(resline->Length==6){ a=Convert::ToInt32(resline->Substring(0,6),16); if(a>0x1fffff){a-=0x400000;} if(a<0){a=0;} b=a; b=floor(b*330000/0x1fffff); textBox1->Text=b.ToString(); //画像処理 b=299-b*300/MAXY;//300をMAXYに変換 y=(int)b; PointF ulCorner = PointF(-1,0); Image^ image = (Image^)pictureBox1->Image->Clone(); Graphics^ graphics = Graphics::FromImage(image); graphics->DrawImage(image,ulCorner); if(timerflag){ graphics->DrawLine(gray,639,0,639,299); timerflag=false; }else{ graphics->DrawLine(white,639,0,639,299); } graphics->DrawLine(pen,638,lasty,639,y); pictureBox1->Image = image; lasty=y; // } } } } private: System::IO::Ports::SerialPort^ serialPort1; private: System::Windows::Forms::ListBox^ listBox1; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::Timer^ timer1; protected: private: System::ComponentModel::IContainer^ components; private: /// /// 必要なデザイナ変数です。 /// #pragma region Windows Form Designer generated code /// /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 /// void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components)); this->listBox1 = (gcnew System::Windows::Forms::ListBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->button2 = (gcnew System::Windows::Forms::Button()); this->label1 = (gcnew System::Windows::Forms::Label()); this->label2 = (gcnew System::Windows::Forms::Label()); this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); this->timer1 = (gcnew System::Windows::Forms::Timer(this->components)); this->label3 = (gcnew System::Windows::Forms::Label()); this->label4 = (gcnew System::Windows::Forms::Label()); this->label5 = (gcnew System::Windows::Forms::Label()); this->label6 = (gcnew System::Windows::Forms::Label()); this->label7 = (gcnew System::Windows::Forms::Label()); this->label8 = (gcnew System::Windows::Forms::Label()); this->label9 = (gcnew System::Windows::Forms::Label()); this->label10 = (gcnew System::Windows::Forms::Label()); (cli::safe_cast(this->pictureBox1))->BeginInit(); this->SuspendLayout(); // // serialPort1 // this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived); // // listBox1 // this->listBox1->FormattingEnabled = true; this->listBox1->ItemHeight = 12; this->listBox1->Location = System::Drawing::Point(345, 12); this->listBox1->Name = L"listBox1"; this->listBox1->Size = System::Drawing::Size(64, 40); this->listBox1->TabIndex = 0; // // button1 // this->button1->Location = System::Drawing::Point(499, 12); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(80, 40); this->button1->TabIndex = 1; this->button1->Text = L"start"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // textBox1 // this->textBox1->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 36, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(128))); this->textBox1->Location = System::Drawing::Point(12, 10); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(237, 55); this->textBox1->TabIndex = 2; this->textBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Right; // // button2 // this->button2->Location = System::Drawing::Point(415, 12); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(78, 40); this->button2->TabIndex = 3; this->button2->Text = L"connect"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); // // label1 // this->label1->AutoSize = true; this->label1->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(128))); this->label1->Location = System::Drawing::Point(249, 44); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(84, 21); this->label1->TabIndex = 4; this->label1->Text = L"μW/cm"; // // label2 // this->label2->AutoSize = true; this->label2->Location = System::Drawing::Point(323, 41); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(11, 12); this->label2->TabIndex = 5; this->label2->Text = L"2"; // // pictureBox1 // this->pictureBox1->Location = System::Drawing::Point(14, 74); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(640, 300); this->pictureBox1->TabIndex = 6; this->pictureBox1->TabStop = false; // // timer1 // this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick); // // label3 // this->label3->AutoSize = true; this->label3->Location = System::Drawing::Point(660, 363); this->label3->Name = L"label3"; this->label3->Size = System::Drawing::Size(11, 12); this->label3->TabIndex = 7; this->label3->Text = L"0"; // // label4 // this->label4->AutoSize = true; this->label4->Location = System::Drawing::Point(660, 223); this->label4->Name = L"label4"; this->label4->Size = System::Drawing::Size(29, 12); this->label4->TabIndex = 8; this->label4->Text = L"3000"; // // label5 // this->label5->AutoSize = true; this->label5->Location = System::Drawing::Point(660, 148); this->label5->Name = L"label5"; this->label5->Size = System::Drawing::Size(29, 12); this->label5->TabIndex = 9; this->label5->Text = L"4500"; // // label6 // this->label6->AutoSize = true; this->label6->Location = System::Drawing::Point(660, 73); this->label6->Name = L"label6"; this->label6->Size = System::Drawing::Size(29, 12); this->label6->TabIndex = 10; this->label6->Text = L"6000"; // // label7 // this->label7->AutoSize = true; this->label7->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(128))); this->label7->Location = System::Drawing::Point(716, 81); this->label7->Name = L"label7"; this->label7->Size = System::Drawing::Size(11, 11); this->label7->TabIndex = 12; this->label7->Text = L"2"; // // label8 // this->label8->AutoSize = true; this->label8->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast(128))); this->label8->Location = System::Drawing::Point(668, 86); this->label8->Name = L"label8"; this->label8->Size = System::Drawing::Size(54, 13); this->label8->TabIndex = 11; this->label8->Text = L"μW/cm"; // // label9 // this->label9->AutoSize = true; this->label9->Location = System::Drawing::Point(587, 59); this->label9->Name = L"label9"; this->label9->Size = System::Drawing::Size(69, 12); this->label9->TabIndex = 13; this->label9->Text = L"← 5sec →"; // // label10 // this->label10->AutoSize = true; this->label10->Location = System::Drawing::Point(660, 297); this->label10->Name = L"label10"; this->label10->Size = System::Drawing::Size(29, 12); this->label10->TabIndex = 14; this->label10->Text = L"1500"; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(729, 385); this->Controls->Add(this->label10); this->Controls->Add(this->label9); this->Controls->Add(this->label7); this->Controls->Add(this->label8); this->Controls->Add(this->label6); this->Controls->Add(this->label5); this->Controls->Add(this->label4); this->Controls->Add(this->label3); this->Controls->Add(this->pictureBox1); this->Controls->Add(this->label2); this->Controls->Add(this->label1); this->Controls->Add(this->button2); this->Controls->Add(this->textBox1); this->Controls->Add(this->button1); this->Controls->Add(this->listBox1); this->Name = L"Form1"; this->Text = L"Form1"; this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form1::Form1_FormClosed); (cli::safe_cast(this->pictureBox1))->EndInit(); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { //接続 if(serialPort1->IsOpen==true){return;} if(listBox1->SelectedIndex==-1){return;} String^ cs=listBox1->SelectedItem->ToString(); serialPort1->PortName = cs; serialPort1->Open(); serialPort1->DtrEnable = true; //DTR(Data Terminal Ready) データ端末レディ serialPort1->RtsEnable = true; //RTS(Request To Send) 送信要求 serialPort1->ReadTimeout = 500; //500mse 読み込みタイムアウト時間 serialPort1->WriteTimeout = 500; //500msec書き込みタイムアウト時間 serialPort1->Write("c\r"); } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { //開始 if(serialPort1->IsOpen==false){return;} String^ strSend = "c"; if (!strSend->EndsWith("\r")) strSend += "\r"; //改行が無かったら \rを追加する this->runflag=1; serialPort1->Write(strSend); timer1->Enabled = true; } private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { //タイマー割り込み timerflag=true;//5秒毎に縦線を引く } }; }