|
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Threading;
namespace LocalTimeReader
{
class LocalTimeReaderPort: ConnectedSerialPort
{
public enum PortStatus
{
NotConnected,
ConnectionEstablished,
ConnectionIdle,
CheckConnected
}
private bool newMessage;
private Message receivedMessage;
private MainForm parent;
private Queue queue1 = new Queue();
private Queue sendMessageQueue;
private Thread sendMessageThread;
private ManualResetEvent sendEvent = new ManualResetEvent(false);
private Queue queue2 = new Queue();
private Queue receiveMessageQueue;
private Thread receiveMessageThread;
private ManualResetEvent receivedEvent = new ManualResetEvent(false);
private PortStatus status;
private System.Threading.Timer acknowledgeTimer = null;
public LocalTimeReaderPort(MainForm prt)
{
parent = prt;
newMessage = true;
sendMessageThread = new Thread(this.SendMessageThread);
sendMessageQueue = Queue.Synchronized(queue1);
sendMessageThread.Start();
receiveMessageThread = new Thread(this.ReceiveMessageThread);
receiveMessageQueue = Queue.Synchronized(queue2);
receiveMessageThread.Start();
status = PortStatus.NotConnected;
acknowledgeTimer = new System.Threading.Timer(new TimerCallback(acknowledgeTimer_Elapsed), null, 1000, 1000);
}
public override bool OpenPort(string port)
{
bool open = base.OpenPort(port);
if (!this.CtsHolding)
{
this.Close();
return false;
}
return open;
}
protected override void ConnectedSerialPort_PinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
{
base.ConnectedSerialPort_PinChanged(sender, e);
if (!this.IsOpen)
return;
if (this.CtsHolding)
return;
MessageBox.Show("Communication with all devices has been lost.\n Check cables and restart all devices.",
"Communication Closed", MessageBoxButtons.OK, MessageBoxIcon.Information);
try
{
this.DiscardOutBuffer();
this.ClosePort();
}
catch
{
}
}
private void acknowledgeTimer_Elapsed(object sender)
{
if (status == PortStatus.ConnectionEstablished)
status = PortStatus.ConnectionIdle;
else if (status == PortStatus.ConnectionIdle || status == PortStatus.NotConnected)
{
if (status == PortStatus.ConnectionIdle)
status = PortStatus.CheckConnected;
SendAcknowledge();
}
else if (status == PortStatus.CheckConnected)
{
acknowledgeTimer.Change(Timeout.Infinite, Timeout.Infinite);
MessageBox.Show("Communication lost - Timeout on acknowledge request",
"Communication Closed", MessageBoxButtons.OK, MessageBoxIcon.Information);
try
{
this.DiscardOutBuffer();
this.ClosePort();
}
catch
{
}
}
}
private void StartReceivedMessage(Message receivedMessage)
{
receiveMessageQueue.Enqueue(receivedMessage);
receivedEvent.Set();
}
private void ReceiveMessageThread()
{
while (true)
{
while (receiveMessageQueue.Count > 0)
{
ReceivedMessage((Message)receiveMessageQueue.Dequeue());
}
receivedEvent.WaitOne();
receivedEvent.Reset();
}
}
private void ReceivedMessage(Message receivedMessage)
{
if (receivedMessage.Code == 0x03)
status = PortStatus.ConnectionEstablished;
if (receivedMessage.Code == 0x01)
parent.ReceivedAcknowledgeRequest();
if (status == PortStatus.NotConnected)
return;
if (receivedMessage.Code == 0x05)
parent.ReceivedTime(receivedMessage.Index, receivedMessage.LocalTime, receivedMessage.RemoteTime);
}
protected override void ProcessDataReceived(object sender, EventArgs e)
{
int totalBytes = this.BytesToRead;
if (totalBytes == 0)
return;
int byteCount = 0;
while (byteCount < totalBytes)
{
if (newMessage)
{
if (status != PortStatus.NotConnected)
status = PortStatus.ConnectionEstablished;
receivedMessage = new Message((byte)this.ReadByte());
if (receivedMessage.Status == Message.MessageStatus.InProgress)
newMessage = false;
else
StartReceivedMessage(receivedMessage);
}
else
{
receivedMessage.Add((byte)this.ReadByte());
if (receivedMessage.Status != Message.MessageStatus.InProgress)
{
newMessage = true;
StartReceivedMessage(receivedMessage);
}
}
byteCount++;
}
}
public void SendAcknowledge()
{
byte[] dataBytes = new byte[1];
dataBytes[0] = 0x02;
StartSendMessage(dataBytes);
}
public void SendTimeRequest(byte index, long time)
{
byte[] dataBytes = new byte[10];
dataBytes[0] = 0x04;
dataBytes[1] = index;
(BitConverter.GetBytes(time)).CopyTo(dataBytes, 2);
StartSendMessage(dataBytes);
}
private void StartSendMessage(byte[] data)
{
sendMessageQueue.Enqueue(data);
sendEvent.Set();
}
private void SendMessageThread()
{
while (true)
{
while (sendMessageQueue.Count > 0)
{
SendMessage((byte[])sendMessageQueue.Dequeue());
}
sendEvent.WaitOne();
sendEvent.Reset();
}
}
private void SendMessage(byte[] data)
{
try
{
this.Write(data, 0, data.Length);
}
catch
{
MessageBox.Show("Failed To Send Message", "Timeout", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}
public void ClosePort()
{
receiveMessageThread.Abort();
while (sendMessageQueue.Count != 0);
Thread.Sleep(100);
if (this.IsOpen)
this.DiscardOutBuffer();
sendMessageThread.Abort();
if (this.IsOpen)
this.Close();
parent.PortClosing();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Doug graduated from Deakin University Geelong, Victoria, Australia with a Bachelor of Engineering.
Early in his career, Doug worked on an automated system for testing telephone lines. This system used a network of DEC PDP11s. The software was written in Oregon Pascal with an event driven structure. This early involvement in event driven structures proved to be an invaluable stepping stone into Windows programming some years latter.
Doug completed a Graduate Diploma in Education at La Trobe University to become a qualified secondary school Mathematics and Physics teacher. Subsequent IT contracts were with universities. One such contract was to add functionality to MSN Messenger.
In recent times Doug has been working on database and Android applications.