I keep receiving this "The type or namespace name 'CASsEventHandl er' could
not be found (are you missing a using directive or an assembly reference?)"
message in two particular lines, and I've tried everything...
Could anyone please paste this and tell me what I'm doing wrong ? I use
SerialPort.zip, which can be downloaded at
http://www.gotdotnet.com/Community/U...4-dfe325097c69
Thanks very much !
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.IO.Ports ;
using System.IO;
using System.Text;
using System.Threadin g;
namespace CASs.NIInstrume nts
{
/// <summary>
/// Simple generic CASs Reader Interface.
/// Provides methods to open and close communication with the device
/// and request reads from the device.
/// </summary>
public interface ICASsReader : IDisposable
{
/// <summary>
/// Opens a connection to the CASs Reader
/// </summary>
void Open();
/// <summary>
/// Closes a connection to the CASs Reader
/// </summary>
void Close();
/// <summary>
/// Forces the CASs reader to make a single read of an ID CAS
/// <returns>
/// Returns a string indicating the ID of the CAS that was read
/// or string.Empty if no CAS was read
/// </returns>
/// </summary>
string ReadOne();
/// <summary>
/// Causes the CASs Reader to enter a continuous reading state
/// </summary>
void ReadContinuous( );
/// <summary>
/// Causes the CASs Reader to exit from a continuous reading state
/// </summary>
void StopReadContinu os();
/// <summary>
/// This event signals that data is received from the CASs Reader
/// </summary>
event CASsEventHandle r CASsEvent;
}
/// <summary>
/// NI Instruments specific implementation of the ICASsReader Interface
/// </summary>
public class NIInstrumentsCA SsReader : ICASsReader
{
private const int MaxReadBytes = 128;
#region Private Member Fields
private SerialPort serialPort;
private Thread continuousReadT hread;
private ThreadStart readThreadStart ;
#endregion Private Member Fields
#region Public Constructors
public NIInstrumentsCA SsReader()
{
Initialize();
}
#endregion
#region ICASsReader Members
public event CASsEventHandle r CASsEvent;
public void Open()
{
// Check if serial port is already open
if( !serialPort.IsO pen )
{
serialPort.Open ();
if (serialPort.IsO pen)
{
// Make reader awake (get ready now command)
serialPort.Writ e(Command.Start , 0, Commands.Start. Lenght);
Thread.Sleep(10 00);
serialPort.Disc ardInBuffer();
serialPort.Disc ardOutBuffer();
}
}
}
public void Close()
{
CleanUpContinuo usReadThread();
serialPort.Clos e();
}
public string ReadOne()
{
byte[] buffer;
string CAS;
CleanUpContinuo usReadThread();
serialPort.Disc ardInBuffer();
serialPort.Disc ardOutBuffer();
WriteCommand(Co mmands.ReadOne) ;
buffer = ReadResponse();
// Check the response
if (buffer == Responses.NoRea d)
{
CAS = string.Empty;
}
else
{
CAS = StripCASFromRes ponse(buffer);
}
return CAS;
}
public void ReadContinuous( )
{
CleanUpContinuo usReadThread();
serialPort.Disc ardInBuffer();
serialPort.Disc ardOutBuffer();
continuousReadT hread = new Thread(readThre adStart);
continuousReadT hread.Start();
WriteCommand(Co mmands.ReadCont inuousNormal);
}
public void StopReadContinu ous()
{
CleanUpContinuo usReadThread();
serialPort.Disc ardInBuffer();
serialPort.Disc ardOutBuffer();
WriteCommand(Co mmands.Start);
Thread.Sleep(10 00);
serialPort.Disc ardInBuffer();
serialPort.Disc ardOutBuffer();
}
#endregion
#region IDisposable Members
public void Dispose()
{
CleanUpContinuo usReadThread();
serialPort.Disp ose();
}
#endregion
#region Private Member Functions
private void Initialize()
{
// Create a new serial port
serialPort = new SerialPort();
if (serialPort.IsO pen)
{
serialPort.Clos e();
}
// Set all important data
serialPort.Baud Rate = 9600;
serialPort.Data Bits = 8;
serialPort.Enco ding = Encoding.ASCII;
serialPort.Pari ty = Parity.None;
serialPort.Port Name = "COM1";
serialPort.Stop Bits = StopBits.One;
serialPort.Rece ivedBytesThresh old = 2;
readThreadStart = new ThreadStart(Con tinuousReadProc );
continuousReadT hread = null;
}
private byte[] ReadResponse()
{
byte[] buffer = new byte[MaxReadBytes];
int offset = 0;
bool responseReceive d = false;
int readval;
for (int i = 0; i < MaxReadBytes; i++)
buffer[i] = 0;
if (serialPort.IsO pen)
{
try
{
while (!responseRecei ved && offset < MaxReadBytes)
{
readval = serialPort.Read (buffer, offset, MaxReadBytes - offset);
if (readval >= 0)
{
offset += readval;
}
if ((offset > 1) &&
(buffer[0] != (byte)ASCII.SOH ))
{
// Didn't get an SOH
// Response is invalid, just bail out !
offset = MaxReadBytes + 1;
responseReceive d = false;
}
if ((offset > 1) &&
(offset >= buffer[1] + 3))
{
// Received entire response
serialPort.Disc ardInBuffer();
serialPort.Disc ardOutBuffer();
responseReceive d = true;
}
}
if (!responseRecei ved)
{
// Some error occurred and we didn't get the response
// Just clear out the buffer, and discard the serialPort buffers...
for (int i = 0; i < MaxReadBytes; i++)
buffer[i] = 0;
serialPort.Read Available();
serialPort.Disc ardInBuffer();
serialPort.Disc ardOutBuffer();
}
}
catch (ObjectDisposed Exception e)
{
throw new ApplicationExce ption("ReadResp onse failed. The serial port
object is disposed", e);
}
catch (InvalidOperati onException e)
{
throw new ApplicationExce ption("ReadResp onse failed. The serial port is
closed", e);
}
catch (IOException e)
{
throw new ApplicationExce ption("ReadResp onse failed. An IO exception
occurred", e);
}
}
return buffer;
}
private void WriteCommand(by te[] command)
{
if (serialPort.IsO pen)
{
try
{
serialPort.Writ e(command, 0, command.Length) ;
}
catch (ObjectDisposed Exception e)
{
throw new ApplicationExce ption("Write " +
RawHexEncoding. GetString(comma nd) + " failed. The serial port object is
disposed", e);
}
catch (InvalidOperati onException e)
{
throw new ApplicationExce ption("Write " +
RawHexEncoding. GetString(comma nd) + " failed. The serial port is closed", e);
}
catch (IOException e)
{
throw new ApplicationExce ption("Write " +
RawHexEncoding. GetString(comma nd) + " failed. An IO exception occurred", e);
}
}
}
private void ContinuousReadP roc()
{
byte[] buffer;
string CAS;
while(true)
{
buffer = ReadResponse();
// Check the response
if (buffer == Responses.NoRea d)
{
CAS = string.Empty;
}
else
{
CAS = StripCASFromRes ponse(buffer);
}
FireCASsEvent (CAS);
}
}
private void FireCASsEvent(s tring ID)
{
CASsEventArgs args = new CASsEventArgs(I D);
if (CASsEvent != null)
{
CASsEvent(this, args);
}
}
private string StripCASFromRes ponse(byte[] response)
{
if ((response.Leng th < 4) ||
(response[0] != ((byte)ASCII.SO H)) ||
(response[2] == 0x03) ||
((response[2]&0x03) > 0) ||
(!ValidCRC(resp onse)) )
{
return string.Empty;
}
ArrayList list = new ArrayList(respo nse.Length - 3);
for (int i = response[1] + 1; i > 2; i--)
{
list.Add(respon se[i]);
}
return RawHexEncoding. GetString(((byt e[])list.ToArray(t ypeof(byte))));
}
private bool ValidCRC(byte[] bytes)
{
BitArray crc = new BitArray(8, false);
bool test;
if (bytes[1] + 2 > bytes.Length + 2)
{
return false;
}
for (int i = 1; i <= bytes[1] + 1; i++)
{
byte[] b = new byte[1];
b[0] = bytes[i];
BitArray ba = new BitArray(b);
crc = crc.Xor(ba);
}
test = ((bytes[bytes[1] + 2]& 0x1) > 0);
if (test != crc[0])
return false;
test = ((bytes[bytes[1] + 2]& 0x2) > 0);
if (test != crc[1])
return false;
test = ((bytes[bytes[1] + 2]& 0x4) > 0);
if (test != crc[2])
return false;
test = ((bytes[bytes[1] + 2]& 0x8) > 0);
if (test != crc[3])
return false;
test = ((bytes[bytes[1] + 2]& 0x10) > 0);
if (test != crc[4])
return false;
test = ((bytes[bytes[1] + 2]& 0x20) > 0);
if (test != crc[5])
return false;
test = ((bytes[bytes[1] + 2]& 0x40) > 0);
if (test != crc[6])
return false;
test = ((bytes[bytes[1] + 2]& 0x80) > 0);
if (test != crc[7])
return false;
return true;
}
private void CleanUpContinuo usReadThread()
{
// Kill the ContinuousReadT hread if it is executing
if (continuousRead Thread != null &&
continuousReadT hread.IsAlive)
{
continuousReadT hread.Abort();
continuousReadT hread = null;
}
}
#endregion Private Member Functions
}
}