473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 21352
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.