Створення програми передавача пакетних даних через COM порт

Інформація про навчальний заклад

ВУЗ:
Національний університет Львівська політехніка
Інститут:
Не вказано
Факультет:
КН
Кафедра:
Кафедра ЕОМ

Інформація про роботу

Рік:
2013
Тип роботи:
Лабораторна робота
Предмет:
Периферійні пристрої

Частина тексту файла (без зображень, графіків і формул):

Міністерство Освіти і Науки України Національний Університет «Львівська політехніка» кафедра ЕОМ Звіт про виконання лабораторних робіт №1-4 з предмету «Периферійні пристрої» на теми : «Створення програми передавача пакетних даних через COM порт» «Створення програми приймача пакетних даних» «Відтворення передача та прийому інформації через COM порт у графічному вигляді» «Налаштування портів на передачу то прийом інформації через COM порт» Мета : ознайомитись з процесом створення програм передавача та приймача даних через послідовний асинхронний інтерфейс RS-232C (COM порт).Вивчити процес формування та передачі даних через у графічному представленні через послідовний асинхронний інтерфейс RS-232C (COM порт).Ознайомитись з процесом налаштування основних параметрів прийому – передачі даних через послідовний асинхронний інтерфейс RS-232C (COM порт). Хід роботи : Створення програми передавача : Для створення програми передавача даних через COM порт нам потрібно створити такі елементи : поле для графічного представлення переданих даних ; текстове поле, в яке записується повідомлення ; текстове поле, в яке заносить швидкість передачі повідомлення; текстове поле з назвою порта ; текстове поле в якому представляється біт переданої інформації. Створення програми приймача даних: Для створення програми приймача даних через COM порт нам потрібно створити такі елементи : поле для графічного представлення прийнятих даних ; текстове поле, в яке виводиться повідомлення ; текстове поле, в яке заносить швидкість прийому повідомлення; текстове поле з назвою порта ; текстове поле в якому представляється біт прийнятої інформації. Налаштування портів на передачу і прийом інформації : Для налаштування портів на прийом і передачу інформації використаємо програму «Virtual Serial Ports Emulator». Щоб налаштувати 2 порта на передачу і прийом інформації потрібно виконати наступні кроки : запустити програму; вибрати «Устройство», далі «Создать»; далі вибрати «тип устройства» «Pair» , «Далее»; вибрати назви портів, які будуть між собою взаємодіяти; натиснути «Готово»; запустити симуляцію; Висновки : виконуючи цю лабораторну роботу ми означились з процесами створення програм передавача і приймача даних, а також процес налаштування портів для взаємодії між собою. Результати виконання програм передавачів і приймачів даних : На рис.№1 програма передавач даних : / Рис.№1 На рис.№2 програма приймач даних : / Рис.№2 Додаток : Текст програми передавача : using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO.Ports; using System.Threading; namespace PerPrustr { public partial class Form1 : Form { SerialPort spSender; string bitVal; string Message; byte[] btIn; public Form1() { InitializeComponent(); foreach (string str in SerialPort .GetPortNames ()) { senderBox.Items.Add(str); } mesBox.Text = "hello"; BaudBox.Text = "9600"; senderBox.Text = "COM2"; } private void button1_Click(object sender, EventArgs e) { this.pictureBox1.Refresh(); Refresh(); } private void Send_But_Click(object sender, EventArgs e) { pictureBox1.Refresh(); spSender = new SerialPort(senderBox.Text, Convert.ToInt32(BaudBox.Text), Parity.Odd, 7); Message = mesBox.Text; btIn = new byte[Message.Length]; for (int i = 0; i < Message.Length; i++) { btIn[i] = Convert.ToByte(Message[i]); } spSender.Open(); spSender.Write(Message); spSender.Close(); numbBox.Items.Clear(); for (int i = 0; i < Message .Length; i++) { numbBox.Items.Add(i); } numbBox.Text = "0"; } void DrawOnForm(byte[] bt,int symbNumb) { Graphics gr; gr = this.pictureBox1.CreateGraphics(); //-------- gr.DrawLine(new Pen(Color.Red, 2.5f), 20f, 20f, 20f, 180f); gr.DrawLine(new Pen(Color.Red, 2.5f), 20f, 180f, 60f, 180f); //-------- string tmp = Convert.ToString(bt[symbNumb ], 10); int tempInt = Convert.ToInt32(tmp, 10); tmp = Convert.ToString(tempInt, 2); while (tmp.Length < 7) { tmp += "0"; } float x = 60f; float y = 180f; char c='0'; // false == 0 int kilkOd = 0; for (int i = 0; i < tmp.Length ; i++) { if (tmp[i] == '1') { if (tmp[i]!=c) { gr.DrawLine(new Pen(Color.Green , 2.5f), x, y, x, y-160f); y -= 160f; gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x + 50f, y); x += 50f; } c = '1'; kilkOd++; } else { if (tmp[i]!=c) { gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x, y + 160f); y += 160f; gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x + 50f, y); x += 50f; } c = '0'; } } if (kilkOd % 2 == 0) { if (c == '0') { gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x, y + 160f); y += 160f; gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x + 50f, y); x += 50f; } c = '0'; } else { if (c == '0') { gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x, y - 160f); y -= 160f; gr.DrawLine(new Pen(Color.Blue , 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x + 50f, y); x += 50f; } } c ='1'; //-------- if (kilkOd %2 ==0) { gr.DrawLine(new Pen(Color.Black, 2.5f), x, y, x, y - 160f); y -= 160f; gr.DrawLine(new Pen(Color.Black, 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Black, 2.5f), x, y, x + 50f, y); x += 50f; } bitVal= tmp; } private void numbBox_SelectedIndexChanged(object sender, EventArgs e) { pictureBox1.Refresh(); DrawOnForm(btIn, Convert.ToInt32(numbBox.Text)); } } } Текст програми приймача : using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO.Ports; namespace TestPP2 { public partial class Form1 : Form { string str; string bitVal; byte[] bt; public Form1() { InitializeComponent(); str = ""; //serialPort1 = new System.IO.Ports.SerialPort("COM2", 9600, System.IO.Ports.Parity.Odd, 7); serialPort2 = new System.IO.Ports.SerialPort("COM3", 9600, System.IO.Ports.Parity.Odd, 7); serialPort2.DataReceived += new SerialDataReceivedEventHandler(ReceiveCom2); serialPort2.Open(); for (int i = 75; i <= 153600; i *= 2) { BaudBox.Items.Add(i.ToString()); } BaudBox.Text = "9600"; foreach (string s in SerialPort.GetPortNames()) { portBox.Items.Add(str); } portBox.Text = "COM3"; } void ReceiveCom2(object sender, SerialDataReceivedEventArgs e) { str = serialPort2.ReadExisting(); bt = new byte [str.Length ]; for (int i=0;i<str.Length;i++) { bt[i] = Convert .ToByte (str [i]); } // DrawOnForm(bt, 0); //numbBox.Items.Add("!!"); } private void button1_Click(object sender, EventArgs e) { } private void BaudBox_SelectedIndexChanged(object sender, EventArgs e) { serialPort2.BaudRate = Convert.ToInt32(BaudBox.Text); } private void portBox_SelectedIndexChanged(object sender, EventArgs e) { serialPort2.PortName = portBox.Text; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } void DrawOnForm(byte[] bt, int symbNumb) { Graphics gr; gr = this.pictureBox1.CreateGraphics(); //-------- gr.DrawLine(new Pen(Color.Red, 2.5f), 20f, 20f, 20f, 180f); gr.DrawLine(new Pen(Color.Red, 2.5f), 20f, 180f, 60f, 180f); //-------- string tmp = Convert.ToString(bt[symbNumb], 10); int tempInt = Convert.ToInt32(tmp, 10); tmp = Convert.ToString(tempInt, 2); while (tmp.Length < 7) { tmp += "0"; } float x = 60f; float y = 180f; char c = '0'; // false == 0 int kilkOd = 0; for (int i = 0; i < tmp.Length; i++) { if (tmp[i] == '1') { if (tmp[i] != c) { gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x, y - 160f); y -= 160f; gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x + 50f, y); x += 50f; } c = '1'; kilkOd++; } else { if (tmp[i] != c) { gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x, y + 160f); y += 160f; gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Green, 2.5f), x, y, x + 50f, y); x += 50f; } c = '0'; } } if (kilkOd % 2 == 0) { if (c == '0') { gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x, y + 160f); y += 160f; gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x + 50f, y); x += 50f; } c = '0'; } else { if (c == '0') { gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x, y - 160f); y -= 160f; gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Blue, 2.5f), x, y, x + 50f, y); x += 50f; } } c = '1'; //-------- if (kilkOd % 2 == 0) { gr.DrawLine(new Pen(Color.Black, 2.5f), x, y, x, y - 160f); y -= 160f; gr.DrawLine(new Pen(Color.Black, 2.5f), x, y, x + 50f, y); x += 50f; } else { gr.DrawLine(new Pen(Color.Black, 2.5f), x, y, x + 50f, y); x += 50f; } bitVal = tmp; } private void numBox_TextChanged(object sender, EventArgs e) { if (numBox.Text == "") { return; } if (Convert.ToInt32(numBox.Text) > str.Length-1) { numBox.Text = ""; return; } pictureBox1.Refresh(); DrawOnForm(bt, Convert.ToInt32(numBox.Text)); MessageBox.Text = str; bitBox.Text = bitVal; } } }
Антиботан аватар за замовчуванням

28.01.2013 17:09

Коментарі

Ви не можете залишити коментар. Для цього, будь ласка, увійдіть або зареєструйтесь.

Ділись своїми роботами та отримуй миттєві бонуси!

Нічого не вибрано
0%

Оголошення від адміністратора

Антиботан аватар за замовчуванням

Подякувати Студентському архіву довільною сумою

Admin

26.02.2023 12:38

Дякуємо, що користуєтесь нашим архівом!