G
Guest
I keep receiving this "The type or namespace name 'CASsEventHandler' 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/...mpleGuid=b06e30f9-1301-4cc6-ac14-dfe325097c69
Thanks very much !
using System;
using System.Collections;
using System.ComponentModel;
using System.IO.Ports;
using System.IO;
using System.Text;
using System.Threading;
namespace CASs.NIInstruments
{
/// <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 StopReadContinuos();
/// <summary>
/// This event signals that data is received from the CASs Reader
/// </summary>
event CASsEventHandler CASsEvent;
}
/// <summary>
/// NI Instruments specific implementation of the ICASsReader Interface
/// </summary>
public class NIInstrumentsCASsReader : ICASsReader
{
private const int MaxReadBytes = 128;
#region Private Member Fields
private SerialPort serialPort;
private Thread continuousReadThread;
private ThreadStart readThreadStart;
#endregion Private Member Fields
#region Public Constructors
public NIInstrumentsCASsReader()
{
Initialize();
}
#endregion
#region ICASsReader Members
public event CASsEventHandler CASsEvent;
public void Open()
{
// Check if serial port is already open
if( !serialPort.IsOpen )
{
serialPort.Open();
if (serialPort.IsOpen)
{
// Make reader awake (get ready now command)
serialPort.Write(Command.Start, 0, Commands.Start.Lenght);
Thread.Sleep(1000);
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
}
}
}
public void Close()
{
CleanUpContinuousReadThread();
serialPort.Close();
}
public string ReadOne()
{
byte[] buffer;
string CAS;
CleanUpContinuousReadThread();
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
WriteCommand(Commands.ReadOne);
buffer = ReadResponse();
// Check the response
if (buffer == Responses.NoRead)
{
CAS = string.Empty;
}
else
{
CAS = StripCASFromResponse(buffer);
}
return CAS;
}
public void ReadContinuous()
{
CleanUpContinuousReadThread();
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
continuousReadThread = new Thread(readThreadStart);
continuousReadThread.Start();
WriteCommand(Commands.ReadContinuousNormal);
}
public void StopReadContinuous()
{
CleanUpContinuousReadThread();
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
WriteCommand(Commands.Start);
Thread.Sleep(1000);
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
}
#endregion
#region IDisposable Members
public void Dispose()
{
CleanUpContinuousReadThread();
serialPort.Dispose();
}
#endregion
#region Private Member Functions
private void Initialize()
{
// Create a new serial port
serialPort = new SerialPort();
if (serialPort.IsOpen)
{
serialPort.Close();
}
// Set all important data
serialPort.BaudRate = 9600;
serialPort.DataBits = 8;
serialPort.Encoding = Encoding.ASCII;
serialPort.Parity = Parity.None;
serialPort.PortName = "COM1";
serialPort.StopBits = StopBits.One;
serialPort.ReceivedBytesThreshold = 2;
readThreadStart = new ThreadStart(ContinuousReadProc);
continuousReadThread = null;
}
private byte[] ReadResponse()
{
byte[] buffer = new byte[MaxReadBytes];
int offset = 0;
bool responseReceived = false;
int readval;
for (int i = 0; i < MaxReadBytes; i++)
buffer = 0;
if (serialPort.IsOpen)
{
try
{
while (!responseReceived && 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;
responseReceived = false;
}
if ((offset > 1) &&
(offset >= buffer[1] + 3))
{
// Received entire response
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
responseReceived = true;
}
}
if (!responseReceived)
{
// 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 = 0;
serialPort.ReadAvailable();
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
}
}
catch (ObjectDisposedException e)
{
throw new ApplicationException("ReadResponse failed. The serial port
object is disposed", e);
}
catch (InvalidOperationException e)
{
throw new ApplicationException("ReadResponse failed. The serial port is
closed", e);
}
catch (IOException e)
{
throw new ApplicationException("ReadResponse failed. An IO exception
occurred", e);
}
}
return buffer;
}
private void WriteCommand(byte[] command)
{
if (serialPort.IsOpen)
{
try
{
serialPort.Write(command, 0, command.Length);
}
catch (ObjectDisposedException e)
{
throw new ApplicationException("Write " +
RawHexEncoding.GetString(command) + " failed. The serial port object is
disposed", e);
}
catch (InvalidOperationException e)
{
throw new ApplicationException("Write " +
RawHexEncoding.GetString(command) + " failed. The serial port is closed", e);
}
catch (IOException e)
{
throw new ApplicationException("Write " +
RawHexEncoding.GetString(command) + " failed. An IO exception occurred", e);
}
}
}
private void ContinuousReadProc()
{
byte[] buffer;
string CAS;
while(true)
{
buffer = ReadResponse();
// Check the response
if (buffer == Responses.NoRead)
{
CAS = string.Empty;
}
else
{
CAS = StripCASFromResponse(buffer);
}
FireCASsEvent (CAS);
}
}
private void FireCASsEvent(string ID)
{
CASsEventArgs args = new CASsEventArgs(ID);
if (CASsEvent != null)
{
CASsEvent(this, args);
}
}
private string StripCASFromResponse(byte[] response)
{
if ((response.Length < 4) ||
(response[0] != ((byte)ASCII.SOH)) ||
(response[2] == 0x03) ||
((response[2]&0x03) > 0) ||
(!ValidCRC(response)) )
{
return string.Empty;
}
ArrayList list = new ArrayList(response.Length - 3);
for (int i = response[1] + 1; i > 2; i--)
{
list.Add(response);
}
return RawHexEncoding.GetString(((byte[])list.ToArray(typeof(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;
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 CleanUpContinuousReadThread()
{
// Kill the ContinuousReadThread if it is executing
if (continuousReadThread != null &&
continuousReadThread.IsAlive)
{
continuousReadThread.Abort();
continuousReadThread = null;
}
}
#endregion Private Member Functions
}
}
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/...mpleGuid=b06e30f9-1301-4cc6-ac14-dfe325097c69
Thanks very much !
using System;
using System.Collections;
using System.ComponentModel;
using System.IO.Ports;
using System.IO;
using System.Text;
using System.Threading;
namespace CASs.NIInstruments
{
/// <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 StopReadContinuos();
/// <summary>
/// This event signals that data is received from the CASs Reader
/// </summary>
event CASsEventHandler CASsEvent;
}
/// <summary>
/// NI Instruments specific implementation of the ICASsReader Interface
/// </summary>
public class NIInstrumentsCASsReader : ICASsReader
{
private const int MaxReadBytes = 128;
#region Private Member Fields
private SerialPort serialPort;
private Thread continuousReadThread;
private ThreadStart readThreadStart;
#endregion Private Member Fields
#region Public Constructors
public NIInstrumentsCASsReader()
{
Initialize();
}
#endregion
#region ICASsReader Members
public event CASsEventHandler CASsEvent;
public void Open()
{
// Check if serial port is already open
if( !serialPort.IsOpen )
{
serialPort.Open();
if (serialPort.IsOpen)
{
// Make reader awake (get ready now command)
serialPort.Write(Command.Start, 0, Commands.Start.Lenght);
Thread.Sleep(1000);
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
}
}
}
public void Close()
{
CleanUpContinuousReadThread();
serialPort.Close();
}
public string ReadOne()
{
byte[] buffer;
string CAS;
CleanUpContinuousReadThread();
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
WriteCommand(Commands.ReadOne);
buffer = ReadResponse();
// Check the response
if (buffer == Responses.NoRead)
{
CAS = string.Empty;
}
else
{
CAS = StripCASFromResponse(buffer);
}
return CAS;
}
public void ReadContinuous()
{
CleanUpContinuousReadThread();
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
continuousReadThread = new Thread(readThreadStart);
continuousReadThread.Start();
WriteCommand(Commands.ReadContinuousNormal);
}
public void StopReadContinuous()
{
CleanUpContinuousReadThread();
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
WriteCommand(Commands.Start);
Thread.Sleep(1000);
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
}
#endregion
#region IDisposable Members
public void Dispose()
{
CleanUpContinuousReadThread();
serialPort.Dispose();
}
#endregion
#region Private Member Functions
private void Initialize()
{
// Create a new serial port
serialPort = new SerialPort();
if (serialPort.IsOpen)
{
serialPort.Close();
}
// Set all important data
serialPort.BaudRate = 9600;
serialPort.DataBits = 8;
serialPort.Encoding = Encoding.ASCII;
serialPort.Parity = Parity.None;
serialPort.PortName = "COM1";
serialPort.StopBits = StopBits.One;
serialPort.ReceivedBytesThreshold = 2;
readThreadStart = new ThreadStart(ContinuousReadProc);
continuousReadThread = null;
}
private byte[] ReadResponse()
{
byte[] buffer = new byte[MaxReadBytes];
int offset = 0;
bool responseReceived = false;
int readval;
for (int i = 0; i < MaxReadBytes; i++)
buffer = 0;
if (serialPort.IsOpen)
{
try
{
while (!responseReceived && 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;
responseReceived = false;
}
if ((offset > 1) &&
(offset >= buffer[1] + 3))
{
// Received entire response
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
responseReceived = true;
}
}
if (!responseReceived)
{
// 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 = 0;
serialPort.ReadAvailable();
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
}
}
catch (ObjectDisposedException e)
{
throw new ApplicationException("ReadResponse failed. The serial port
object is disposed", e);
}
catch (InvalidOperationException e)
{
throw new ApplicationException("ReadResponse failed. The serial port is
closed", e);
}
catch (IOException e)
{
throw new ApplicationException("ReadResponse failed. An IO exception
occurred", e);
}
}
return buffer;
}
private void WriteCommand(byte[] command)
{
if (serialPort.IsOpen)
{
try
{
serialPort.Write(command, 0, command.Length);
}
catch (ObjectDisposedException e)
{
throw new ApplicationException("Write " +
RawHexEncoding.GetString(command) + " failed. The serial port object is
disposed", e);
}
catch (InvalidOperationException e)
{
throw new ApplicationException("Write " +
RawHexEncoding.GetString(command) + " failed. The serial port is closed", e);
}
catch (IOException e)
{
throw new ApplicationException("Write " +
RawHexEncoding.GetString(command) + " failed. An IO exception occurred", e);
}
}
}
private void ContinuousReadProc()
{
byte[] buffer;
string CAS;
while(true)
{
buffer = ReadResponse();
// Check the response
if (buffer == Responses.NoRead)
{
CAS = string.Empty;
}
else
{
CAS = StripCASFromResponse(buffer);
}
FireCASsEvent (CAS);
}
}
private void FireCASsEvent(string ID)
{
CASsEventArgs args = new CASsEventArgs(ID);
if (CASsEvent != null)
{
CASsEvent(this, args);
}
}
private string StripCASFromResponse(byte[] response)
{
if ((response.Length < 4) ||
(response[0] != ((byte)ASCII.SOH)) ||
(response[2] == 0x03) ||
((response[2]&0x03) > 0) ||
(!ValidCRC(response)) )
{
return string.Empty;
}
ArrayList list = new ArrayList(response.Length - 3);
for (int i = response[1] + 1; i > 2; i--)
{
list.Add(response);
}
return RawHexEncoding.GetString(((byte[])list.ToArray(typeof(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;
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 CleanUpContinuousReadThread()
{
// Kill the ContinuousReadThread if it is executing
if (continuousReadThread != null &&
continuousReadThread.IsAlive)
{
continuousReadThread.Abort();
continuousReadThread = null;
}
}
#endregion Private Member Functions
}
}