62 lines
1.2 KiB
C#
62 lines
1.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.IO.Ports;
|
|||
|
|
using System.Threading;
|
|||
|
|
|
|||
|
|
namespace Nanji_Island
|
|||
|
|
{
|
|||
|
|
class Comm
|
|||
|
|
{
|
|||
|
|
public delegate void EventHandle(byte[] readbuffer);
|
|||
|
|
public event EventHandle DataReceived;
|
|||
|
|
|
|||
|
|
public SerialPort serialport;
|
|||
|
|
Thread thread;
|
|||
|
|
volatile bool _keepReading;
|
|||
|
|
|
|||
|
|
public Comm()
|
|||
|
|
{
|
|||
|
|
serialport = new SerialPort();
|
|||
|
|
thread = null;
|
|||
|
|
_keepReading = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool IsOpen
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return serialport.IsOpen;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void StartReading()
|
|||
|
|
{
|
|||
|
|
if (!_keepReading)
|
|||
|
|
{
|
|||
|
|
_keepReading = true;
|
|||
|
|
thread = new Thread(new ThreadStart(ReadPort));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void StopReading()
|
|||
|
|
{
|
|||
|
|
if (_keepReading)
|
|||
|
|
{
|
|||
|
|
_keepReading = false;
|
|||
|
|
thread.Join();
|
|||
|
|
thread = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Readport()
|
|||
|
|
{
|
|||
|
|
while (_keepReading)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|