472,779 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

"are you missing a using directive or an assembly reference?" prob

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/U...4-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[i] = 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[i] = 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[i]);
}
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[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 CleanUpContinuousReadThread()
{
// Kill the ContinuousReadThread if it is executing
if (continuousReadThread != null &&
continuousReadThread.IsAlive)
{
continuousReadThread.Abort();
continuousReadThread = null;
}
}

#endregion Private Member Functions
}
}

Jul 21 '05 #1
1 21286
Junior <Ju****@discussions.microsoft.com> wrote:
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...


Well, where are you expecting the delegate 'CASsEventHandler' to be
declared? You haven't declared it in the code you've posted.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jacek Dziedzic | last post by:
Is it valid to use a "using namespace foo" (as opposed to using foo::bar which I'm sure is legal) within a class declaration? My compiler rejects it, but I've been told it's valid. Can anyone...
0
by: Oliver | last post by:
hi - I have a written a "Serviced Component" which works fine when declared as: but I am seeing an 'Access Denied' exception when I declare it as: this is probably something to do with...
4
by: Philipp Sumi | last post by:
Hello all I have a thread that performs some simple I/O within a loop that runs on a separate thread. I used the using keyword to ensure the writer's disposal: using (writer) { while...
7
by: Clint Herron | last post by:
Howdy! I posted this question on CSharpCorner.com, but then realized I should probably post it on a more active newsgroup. This will be my only cross-post. I'm creating a game engine, and...
1
by: Junior | last post by:
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...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
1
by: BobPaul | last post by:
I'm following code out of a howto book and this is really bugging me. This header file was created by VStudio 6.0 when I did a "Right Click: Add Member Function" CLine is a class I wrote (per the...
3
by: VRSki | last post by:
Hello, Is there any way to use "using" for aliasing in the global scope? The example below works fine in the context of a given file, but in the different file MyByte alias is unknown. Is...
2
by: Peter Webb | last post by:
Sorry, stupid newbie question. I have VS2008, and I downloaded and installed the DirectX SDK. I want to access the DirectX classes from C#, but I can't see how to make C# aware of them. ...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.