| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.IO.Ports;
- namespace WpfApplication19
- {
- public class OBInterface
- {
- SerialPort _sp;
- public OBInterface(SerialPort sp)
- {
- _sp = sp;
- DataSender sender = new DataSender(_sp);
- DataReceiver receiver = new DataReceiver(_sp);
-
- Thread pro = new Thread(new ThreadStart(sender.ThreadRun));
- Thread con = new Thread(new ThreadStart(receiver.ThreadRun));
-
- try
- {
- pro.Start();
- con.Start();
- pro.Join();
- con.Join();
- }
- catch (ThreadStateException e)
- {
- Console.WriteLine(e);
- }
- catch (ThreadInterruptedException e)
- {
- Console.WriteLine(e);
- }
- }
- }
- public class DataSender
- {
- SerialPort _serialPort;
- public DataSender(SerialPort sp)
- {
- _serialPort = sp;
- }
- public void ThreadRun()
- {
- while(_serialPort.IsOpen){
- Console.WriteLine("Sending");
- Thread.Sleep(500);
- }
- }
- }
- public class DataReceiver
- {
- SerialPort _serialPort;
- public DataReceiver(SerialPort sp)
- {
- _serialPort = sp;
- }
- public void ThreadRun()
- {
- while (_serialPort.IsOpen)
- {
- Console.WriteLine("Reading");
- Thread.Sleep(500);
- }
- }
- }
- class Connector
- {
- bool _readerFlag = false;
- public void ReadFromComPort()
- {
- lock (this)
- {
- if (!_readerFlag)
- {
- try
- {
- Monitor.Wait(this);
- }
- catch (SynchronizationLockException e)
- {
- Console.WriteLine(e);
- }
- catch (ThreadInterruptedException e)
- {
- Console.WriteLine(e);
- }
- }
- Console.WriteLine("Reader");
- }
- }
- public void WriteToComPort()
- {
-
- }
- }
- }
|