Смекни!
smekni.com

Послідовні інтерфейси ПК (стр. 3 из 3)

this->label3->Size = System: Drawing: Size (139, 16);

this->label3->TabIndex = 9;

this->label3->Text = L «Швидкість передачі:»;

//

// comboBox2

//

this->comboBox2->Font = (gcnew System: Drawing: Font (L «Microsoft Sans Serif», 9.75F, System: Drawing: FontStyle: Regular, System: Drawing: GraphicsUnit: Point,

static_cast<System: Byte>(204)));

this->comboBox2->FormattingEnabled = true;

this->comboBox2->Items->AddRange (gcnew cli:array< System: Object^ >(8) {L «1200», L «2400», L «4800», L «9600», L «19200», L «38400»,

L «57600», L «115200»});

this->comboBox2->Location = System: Drawing: Point (176, 70);

this->comboBox2->Name = L «comboBox2»;

this->comboBox2->Size = System: Drawing: Size (97, 24);

this->comboBox2->TabIndex = 10;

//

// label4

//

this->label4->AutoSize = true;

this->label4->Font = (gcnew System: Drawing: Font (L «Microsoft Sans Serif», 9.75F, System: Drawing: FontStyle: Regular, System: Drawing: GraphicsUnit: Point,

static_cast<System: Byte>(204)));

this->label4->Location = System: Drawing: Point (320, 113);

this->label4->Name = L «label4»;

this->label4->Size = System: Drawing: Size (0, 16);

this->label4->TabIndex = 12;

//

// progressBar1

//

this->progressBar1->Location = System: Drawing: Point (29, 134);

this->progressBar1->Name = L «progressBar1»;

this->progressBar1->Size = System: Drawing: Size (593, 17);

this->progressBar1->TabIndex = 13;

//

// Form1

//

this->AutoScaleDimensions = System: Drawing: SizeF (6, 13);

this->AutoScaleMode = System: Windows: Forms: AutoScaleMode: Font;

this->ClientSize = System: Drawing: Size (652, 166);

this->Controls->Add (this->progressBar1);

this->Controls->Add (this->label4);

this->Controls->Add (this->comboBox2);

this->Controls->Add (this->label3);

this->Controls->Add (this->label2);

this->Controls->Add (this->label1);

this->Controls->Add (this->textBox1);

this->Controls->Add (this->button3);

this->Controls->Add (this->button2);

this->Controls->Add (this->button1);

this->Controls->Add (this->comboBox1);

this->Name = L «Form1»;

this->Text = L «COM Комутнікатор»;

this->Load += gcnew System: EventHandler (this, &Form1: Form1_Load);

this->ResumeLayout(false);

this->PerformLayout();

}

#pragma endregion

private: System: Void Form1_Load (System: Object^ sender, System: EventArgs^ e) {

array<String^>^ ports = serialPort1->GetPortNames();

int i = 0;

comboBox1->Items->Clear();

for (i = 0; i < ports->Length; i++) {

comboBox1->Items->Add (ports[i]);

}

comboBox1->SelectedIndex = 0;

comboBox2->SelectedIndex = 7;

}

private: System: Void button2_Click (System: Object^ sender, System: EventArgs^ e) {

String^ portName = (String^) comboBox1->SelectedItem;

array<unsigned char>^ buffer = gcnew array<unsigned char>(1000);

unsigned int bytesRead = 0;

if (! File: Exists (textBox1->Text)) {

MessageBox: Show («Такий файл не існує», «Помилка»,

MessageBoxButtons:OK, MessageBoxIcon: Error);

}

else {

int numBytesRead = 0;

FileStream^ f = gcnew FileStream (textBox1->Text, FileMode: Open, FileAccess: Read);

FileInfo^ fi = gcnew FileInfo (textBox1->Text);

unsigned int fileSize = fi->Length;

String^ fileName = fi->Name;

String^ fileSizeStr = «» + fileSize;

// for files only

array<wchar_t>^ b1 = fileName->ToCharArray();

label4->Text = «Передача файлу» + fileName +»…»;

serialPort1->PortName = portName;

serialPort1->BaudRate = Int32: Parse (comboBox2->Text);

serialPort1->WriteTimeout = 10000;

serialPort1->Open();

serialPort1->WriteLine(fileName);

serialPort1->WriteLine(fileSizeStr);

while (bytesRead < fileSize) {

numBytesRead = f->Read (buffer, 0,1000);

serialPort1->Write (buffer, 0, numBytesRead);

bytesRead += numBytesRead;

label4->Text = «Відіслано» + (bytesRead *100 /fileSize) + «%»;

progressBar1->Value = (bytesRead *100 /fileSize);

Update();

}

serialPort1->Close();

f->Close();

MessageBox: Show («Файл» + fileName + «успішне передано», «Пердача файлу», MessageBoxButtons:OK, MessageBoxIcon: Information);

progressBar1->Value = 0;

label4->Text = «»;

Update();

}

}

private: System: Void button1_Click (System: Object^ sender, System: EventArgs^ e) {

String^ fileName;

String^ fileSizeStr;

unsigned int fileSize;

String^ portName = (String^) comboBox1->SelectedItem;

label4->Text = «Очікування файлу…»;

serialPort1->PortName = portName;

serialPort1->BaudRate = Int32: Parse (comboBox2->Text);

serialPort1->ReadTimeout = 10000;

serialPort1->Open();

fileName = serialPort1->ReadLine();

fileSizeStr = serialPort1->ReadLine();

fileSize = Int32: Parse(fileSizeStr);

array<unsigned char>^ buffer = gcnew array<unsigned char>(1000);

unsigned int bytesRead = 0;

if (! Directory: Exists (». //files»)) {

Directory: CreateDirectory (». //files»);

}

if (File: Exists (». //files // » + fileName)) {

MessageBox: Show («Такий файл уже існує», «Помилка»,

MessageBoxButtons:OK, MessageBoxIcon: Error);

}

else {

int numBytesRead = 0;

FileStream^ w = gcnew FileStream («files // » + fileName, FileMode: OpenOrCreate, FileAccess: Write);

FileInfo^ fi = gcnew FileInfo («files // » + fileName);

// for files only

label4->Text = «Прийом файлу» + fileName +»…»;

while (bytesRead < fileSize) {

numBytesRead = serialPort1->Read (buffer, 0,1000);

w->Write (buffer, 0, numBytesRead);

bytesRead += numBytesRead;

label4->Text = «Отримано» + (bytesRead *100 /fileSize) + «%»;

progressBar1->Value = (bytesRead *100 /fileSize);

Update();

}

serialPort1->Close();

w->Close();

MessageBox: Show («Файл» + fileName + «успішне прийнято:» + fi->FullName, «Пердача файлу», MessageBoxButtons:OK, MessageBoxIcon: Information);

progressBar1->Value = 0;

label4->Text = «»;

Update();

serialPort1->Close();

}

}

private: System: Void openFileDialog1_FileOk (System: Object^ sender, System: ComponentModel: CancelEventArgs^ e) {

}

private: System: Void label1_Click (System: Object^ sender, System: EventArgs^ e) {

}

private: System: Void button3_Click (System: Object^ sender, System: EventArgs^ e) {

openFileDialog1->InitialDirectory =».&bsol;»;

openFileDialog1->Filter = «txt files(*.txt)|*.txt|All files (*.*)|*.*»;

openFileDialog1->FilterIndex = 2;

openFileDialog1->RestoreDirectory = true;

if (System: Windows: Forms: DialogResult:OK == openFileDialog1->ShowDialog(this)) {

textBox1->Text = openFileDialog1->FileName;

} else {

textBox1->Text = «»;

}

}

private: System: Void progressBar1_Click (System: Object^ sender, System: EventArgs^ e) {

}

};

}