Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO.Ports;
- using System.Threading;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- volatile bool _devam1=false, _devam2=false,durum=true;
- //
- public Form1()
- {
- InitializeComponent();
- CheckForIllegalCrossThreadCalls = false;
- serialPort1.PortName = "COM3";
- serialPort1.BaudRate = 9600;
- serialPort1.DataBits = 8;
- serialPort2.PortName = "COM5";
- serialPort2.BaudRate = 9600;
- serialPort2.DataBits = 8;
- }
- //
- private void button1_Click(object sender, EventArgs e)
- {
- }
- //
- private void button2_Click(object sender, EventArgs e)
- {
- if (!serialPort1.IsOpen) { serialPort1.Open(); }
- _devam1 = true;
- (new Thread(new ThreadStart(this.Read1))).Start();
- if (!serialPort2.IsOpen) { serialPort2.Open(); }
- _devam2 = true;
- (new Thread(new ThreadStart(this.Read2))).Start();
- //
- public void Read1()
- {
- while (_devam1)
- {
- try
- {
- string message1 = serialPort1.ReadExisting();
- if (_devam1) { this.richTextBox1.Text += message1; }
- }
- catch (TimeoutException) { }
- }
- }
- //
- public void Read2()
- {
- while (_devam2)
- {
- try
- {
- string message2 = serialPort2.ReadExisting();
- if (_devam2) { this.richTextBox2.Text += message2;}
- }
- catch (TimeoutException) { }
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if(durum){
- _devam1 = true;
- _devam2 = true;
- }
- if(!durum){
- _devam1 = false;
- _devam2 = false;
- }
- durum = !durum;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement