Реферат: Послідовні інтерфейси ПК
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all.resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1: public System: Windows: Forms: Form
{
public:
Form1 (void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1 ()
{
if (components)
{
delete components;
}
}
private: System:IO: Ports: SerialPort^ serialPort1;
private: System: Windows: Forms: ComboBox^ comboBox1;
private: System: Windows: Forms: Button^ button1;
private: System: Windows: Forms: Button^ button2;
private: System: Windows: Forms: OpenFileDialog^ openFileDialog1;
private: System: Windows: Forms: Button^ button3;
private: System: Windows: Forms: TextBox^ textBox1;
private: System: Windows: Forms: Label^ label1;
private: System: Windows: Forms: Label^ label2;
private: System: Windows: Forms: Label^ label3;
private: System: Windows: Forms: ComboBox^ comboBox2;
private: System: Windows: Forms: Label^ label4;
private: System: Windows: Forms: ProgressBar^ progressBar1;
private: System: ComponentModel:IContainer^ components;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System: ComponentModel: Container());
this->serialPort1 = (gcnew System:IO: Ports: SerialPort (this->components));
this->comboBox1 = (gcnew System: Windows: Forms: ComboBox());
this->button1 = (gcnew System: Windows: Forms: Button());
this->button2 = (gcnew System: Windows: Forms: Button());
this->openFileDialog1 = (gcnew System: Windows: Forms: OpenFileDialog());
this->button3 = (gcnew System: Windows: Forms: Button());
this->textBox1 = (gcnew System: Windows: Forms: TextBox());
this->label1 = (gcnew System: Windows: Forms: Label());
this->label2 = (gcnew System: Windows: Forms: Label());
this->label3 = (gcnew System: Windows: Forms: Label());
this->comboBox2 = (gcnew System: Windows: Forms: ComboBox());
this->label4 = (gcnew System: Windows: Forms: Label());
this->progressBar1 = (gcnew System: Windows: Forms: ProgressBar());
this->SuspendLayout();
//
// comboBox1
//
this->comboBox1->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->comboBox1->FormattingEnabled = true;
this->comboBox1->Location = System: Drawing: Point (176, 21);
this->comboBox1->Name = L «comboBox1»;
this->comboBox1->Size = System: Drawing: Size (97, 24);
this->comboBox1->TabIndex = 1;
//
// button1
//
this->button1->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->button1->Location = System: Drawing: Point (323, 71);
this->button1->Name = L «button1»;
this->button1->Size = System: Drawing: Size (136, 23);
this->button1->TabIndex = 2;
this->button1->Text = L «Прийняти файл»;
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System: EventHandler (this, &Form1:button1_Click);
//
// button2
//
this->button2->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->button2->Location = System: Drawing: Point (495, 70);
this->button2->Name = L «button2»;
this->button2->Size = System: Drawing: Size (127, 23);
this->button2->TabIndex = 3;
this->button2->Text = L «Надіслати файл»;
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System: EventHandler (this, &Form1:button2_Click);
//
// openFileDialog1
//
this->openFileDialog1->InitialDirectory = L «D:\\»;
this->openFileDialog1->RestoreDirectory = true;
this->openFileDialog1->Title = L «Select file»;
this->openFileDialog1->FileOk += gcnew System: ComponentModel: CancelEventHandler (this, &Form1:openFileDialog1_FileOk);
//
// button3
//
this->button3->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->button3->Location = System: Drawing: Point (593, 20);
this->button3->Name = L «button3»;
this->button3->Size = System: Drawing: Size (29, 24);
this->button3->TabIndex = 5;
this->button3->Text = L»…»;
this->button3->TextAlign = System: Drawing: ContentAlignment: TopCenter;
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System: EventHandler (this, &Form1:button3_Click);
//
// textBox1
//
this->textBox1->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->textBox1->Location = System: Drawing: Point (372, 21);
this->textBox1->Name = L «textBox1»;
this->textBox1->Size = System: Drawing: Size (215, 22);
this->textBox1->TabIndex = 6;
//
// label1
//
this->label1->AutoSize = true;
this->label1->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->label1->Location = System: Drawing: Point (26, 24);
this->label1->Name = L «label1»;
this->label1->Size = System: Drawing: Size (144, 16);
this->label1->TabIndex = 7;
this->label1->Text = L «Доступні COM порти:»;
this->label1->Click += gcnew System: EventHandler (this, &Form1:label1_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->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->label2->Location = System: Drawing: Point (320, 23);
this->label2->Name = L «label2»;
this->label2->Size = System: Drawing: Size (46, 16);
this->label2->TabIndex = 8;
this->label2->Text = L «Файл:»;
//
// label3
//
this->label3->AutoSize = true;
this->label3->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->label3->Location = System: Drawing: Point (26, 73);
this->label3->Name = L «label3»;
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 =».\\»;
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) {
}
};
}