I have the following code in which I use events to read on eth or
serial port when data commes in. The ASCHFProtLib library is a third
party library that I have to use as it is. Unfortunately this version
of my code does not work. It seems that the events are not triggering
when there is a data on any of the eth or serial ports. Basicaly all
the events handler functions (OnMsgRecv and similar) are not executed.
I have included a piece of code that was provided by the library
author. In this code I noticed that the events handlers where defined
as private static void. However, modifying my code to reflect this,
could not solve the problem. I am not very familliar with C#
Any help will be appreciated.
Thanks Guy
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// THE CODE BELOW DOES NOT WORK
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ASCHFProtLib;
namespace IDTrak
{
public partial class IDTrak : Form
{
private static string _IPAdd;
private static int _Port;
private HFProt m_ASCProt;
private static string _SerialPort;
private static int _BaudRate = 38400;
private string stCommand;
private int iNumberOfTags;
private int iTagsDisplayloopCounter;
private string stUid;
private static bool boolConnectedEth = false;
private static bool boolConnectedSerial = false;
private static string stCommand_InventoryAnntenna_1 = "M01";
// Inventory
private static string stCommand_SetPollingOn =
"P08701";// =Polling mode is ON
private static string stCommand_SetBaudRate = "P001";
// default is 0xC0=19200
private static string stCommand_SetPollingOff =
"P08700";// =Polling mode is OFF
private static string stCommand_SetMaxScanRepeat = "P081";
//P08205 (default is 0x06)
private static string stCommand_SetMaxScanFail = "P082";
//P08205 (default is 0x05)
private static string stCommand_SetTimeSlots = "P02A";
//P02A10 =16 Times Slots
private static string stCommand_ReadWithQuietMode =
"P08001";// =read with quiet mode
private static string stCommand_ReadWithoutQuietMode=
"P08000";// (default)
private static string stCommand_SetRfPowerLevel = "P0D2";
//0x00 to 0x28 default is 0x14 =4watts
private static string stCommand_SwitchRfPowerOn =
"P09001";//0x00 default
private static string stCommand_SwitchRfPowerOff =
"P09000";//
private static string stCommand_RequestReaderParameters = "F0";
private static string stCommand_GetReaderVersion = "V0";
// Reader Version
// bool exit = false;
// int index;
// string rstr;
public IDTrak()
{
try
{
InitializeComponent();
_IPAdd = this.tBIpAddress.GetPureIPAddress();
_Port = Convert.ToInt32(this.tBPort.Text.ToString());
this.cBSerialPort.SelectedIndex = 0;
iTagsDisplayloopCounter = 0;
this.cBSerialPort.SelectedIndex = 0;
// this.gBEth.Enabled = false;
// Modifcations for GPI Version of the software.
//this.cbBReaderSelect.SelectedIndex = 0;
this.lReader.Enabled = false;
this.cbBReaderSelect.Enabled = false;
this.gBCommands.Enabled = false;
// this.lParam.Enabled = false;
//this.cBParam.Enabled = false;
this.toolStripStatusLabel1.Text = "Not connected";
m_ASCProt = new ASCHFProtLib.HFProt();
InitializeEthInterface();
this.tbOutput.AppendText("Eth interface initialized
\r\n");
InitializeRS232Interface();
this.tbOutput.AppendText("RS232 interface initialized
\r\n");
}
catch
{
MessageBox.Show("Error in interface initialization",
"IDTrak", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
}
private bool InitReaderRs232()
{ // This has to be modified
bool result = true;
/*
this.tbOutput.AppendText("Initializing the reader" +
"\r\n");
stCommand = "H0"; // Heartbeat
result = m_ASCProt.SendRS232(stCommand);
stCommand = "V0"; // Version
result = m_ASCProt.SendRS232(stCommand);
stCommand = "P0FF01"; // Set anything
result = m_ASCProt.SendRS232(stCommand);
*/
return result;
}
private void InitializeEthInterface() {
m_ASCProt.OnEthConnect += new
ASCHFProtLib.HFProt.EthConnect(OnConnect);
m_ASCProt.OnEthAscMsg += new
ASCHFProtLib.HFProt.MsgEthReceived(OnMsgEthRecv);
// m_ASCProt.OnEthAscMsg += new
ASCHFProtLib.HFProt.MsgEthReceived(processMessage) ;
}
private void InitializeRS232Interface()
{
this.m_ASCProt.OnRS232AscMsg += new
ASCHFProtLib.HFProt.MsgRS232Received(OnMsgRecv);
this.m_ASCProt.OnRS232MsgErr += new
ASCHFProtLib.HFProt.RS232MsgError(OnRS232MsgErr);
}
private void OnRS232MsgErr(object sender, int err)
{
this.tbOutput.AppendText("Error Received on Serial Port: "
+ _SerialPort + " " + err.ToString() + "\r\n");
}
private void CloseRS232Interface()
{
m_ASCProt.CloseRSCom();
m_ASCProt.OnRS232AscMsg -= new
ASCHFProtLib.HFProt.MsgRS232Received(OnMsgRecv);
m_ASCProt = null;
}
private void CloseEthInterface()
{
m_ASCProt.CloseSocket();
m_ASCProt.OnEthConnect -= new
ASCHFProtLib.HFProt.EthConnect(OnConnect);
m_ASCProt.OnEthAscMsg -= new
ASCHFProtLib.HFProt.MsgEthReceived(OnMsgEthRecv);
m_ASCProt = null;
}
/*
private void OnMsgRecv(object sender, string msg)
{
this.tbOutput.AppendText("Received on Serial Port: " +
_SerialPort + " " + msg + "\r\n");
}
*/
private static void OnMsgRecv(object sender, string msg) {
MessageBox.Show("This is a message", "IDTrak",
MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}
private void OnConnect(object sender, bool connect)
{
if (connect)
{
this.tbOutput.AppendText("IPAdd:" + _IPAdd + " Port: "
+ _Port.ToString() + " connected!" + "\r\n");
}
else {
this.tbOutput.AppendText("IPAdd:" + _IPAdd + " Port: "
+ _Port.ToString() + "Disconnected!" + "\r\n");
}
}
//private void processMessage(string msg)
private void processMessage(object sender, string msg)
{
int index = msg.IndexOf("m");
int iTotnumberOfTags;
if (index == 0){
//8F
this.iNumberOfTags = Convert.ToInt32(msg.Substring(3,
2),16);
if (this.iNumberOfTags != 143)
{
this.listView1.Items.Clear();
iTotnumberOfTags = this.iNumberOfTags;
this.tbOutput.AppendText("Total Number of tags: " +
iNumberOfTags.ToString() + "\r\n");
this.listView1.BeginUpdate();
for (int i = 0; i < iTotnumberOfTags; i++)
{
stUid = msg.Substring(5 + (i * 16), 16);
ListViewItem listItem = new
ListViewItem((i+1).ToString());
listItem.SubItems.Add(stUid);
this.listView1.Items.Add(listItem);
}
//Call EndUpdate when you finish adding items to
the ListView.
this.listView1.EndUpdate();
iTotnumberOfTags = 0;
}
else {// the List contains 15 UID and more are still to
come
this.listView1.BeginUpdate();
for (int i = 0; i < 15; i++)
{
stUid = msg.Substring(5 + (i * 16), 16);
ListViewItem listItem = new ListViewItem((i +
this.iTagsDisplayloopCounter).ToString());
listItem.SubItems.Add(stUid);
this.listView1.Items.Add(listItem);
}
this.listView1.EndUpdate();
this.iTagsDisplayloopCounter += 15;
//Carefull!! This will not work if the inventory
was sent on another antenna
stCommand = stCommand_InventoryAnntenna_1;
m_ASCProt.SendSocket(stCommand); // send the
command again to retrieve
// the remaining
UIDs
}
}
else {
this.tbOutput.AppendText("Something bad" +
iNumberOfTags.ToString() + "\r\n");
}
}
private void OnMsgEthRecv(object sender, string msg)
{
// processMessage(msg);
this.tbOutput.AppendText("RecvEth: " + msg + "\r\n");
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tBIpAddress_TextChanged(object sender, EventArgs
e)
{
}
private void toolStripStatusLabel1_Click(object sender,
EventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender,
EventArgs e)
{
}
private void btConnectDisconnectEth_Click(object sender,
EventArgs e)
{
if (!boolConnectedEth) // Not yet connected: Connect
{
this.tbOutput.AppendText("OpenEth: " + "Connecting..."
+ "\r\n");
if (!m_ASCProt.OpenSocket(_IPAdd, _Port))// unable to
connect
{
this.toolStripStatusLabel1.Text = "Not connected";
boolConnectedEth = false;
this.gBCommands.Enabled = false;
}
else // successfull connection
{
this.toolStripStatusLabel1.Text = "Connected";
this.btConDisconnectEth.Text = "Disconnect";
this.gBCommands.Enabled = true;
boolConnectedEth = true;
this.tBIpAddress.Enabled = false;
this.lIpAddress.Enabled = false;
this.gBRS232.Enabled = false;
this.tBPort.Enabled = false;
this.lPort.Enabled = false;
}
}
else // already connected: Disconnect
{
boolConnectedEth = false;
m_ASCProt.CloseSocket();
// m_ASCProt.CloseRSCom();
this.toolStripStatusLabel1.Text = "Disconnected";
this.gBCommands.Enabled = false;
this.gBRS232.Enabled = true;
this.btConDisconnectEth.Text = "Connect";
this.lIpAddress.Enabled = true;
this.lPort.Enabled = true;
this.tBIpAddress.Enabled = true;
this.tBPort.Enabled = true;
}
}
private void IDTrak_Load(object sender, EventArgs e)
{
}
private void IDTrak_FormClosed(object sender,
FormClosedEventArgs e)
{
}
private void IDTrak_FormClosing(object sender,
FormClosingEventArgs e)
{
try
{
//CloseEthInterface();
if (boolConnectedEth)
{
CloseEthInterface();
}
else if (boolConnectedSerial)
{
CloseRS232Interface();
}
else
{
this.tbOutput.AppendText("Error!!: No communication
interface to close \r\n");
}
}
catch
{ }
}
private void btReader_Click(object sender, EventArgs e)
{
}
private void tBCommand_MouseHover(object sender, EventArgs e)
{
// The tool tips should be displayed
}
private void bSend_Click(object sender, EventArgs e)
{
int selectedIndex = this.cBCommand.SelectedIndex;
this.listView1.Items.Clear();
iTagsDisplayloopCounter = 0;
//this.listView1.Items[i].SubItems.Add(stUid);
//Add a switch statement here to select the command
//Convert.ToInt32(this.tBPort.Text.ToString());
if (selectedIndex >= 0) // List item selected: do nothing
since
{ //stCommand is already set properly
}
else // No list item selected: The user migh have entered a
new command
{
stCommand = this.cBCommand.Text.ToString() +
cBParam.Text.ToString();
}
this.tbOutput.AppendText("Command to send " + stCommand + "
\r\n");
if (boolConnectedEth)
{
m_ASCProt.SendSocket(stCommand);
}
else if (boolConnectedSerial)
{
m_ASCProt.SendRS232(stCommand);
}
else
{
this.tbOutput.AppendText("Error!! No communication
interface ready \r\n");
}
}
private void tBCommand_TextChanged(object sender, EventArgs e)
{
}
private void tBCommand_KeyPress(object sender,
KeyPressEventArgs e)
{
// if (e.Equals(Keys.Return)) {
// stCommand = this.tBCommand.Text.ToString();
// m_ASCProt.SendSocket(stCommand);
// }
}
private void btConnectDisconnectRS232_Click(object sender,
EventArgs e)
{
if (!boolConnectedSerial)
{// Not yet connected: Connect
this.tbOutput.AppendText("OpenCom: " + _SerialPort + "
Connecting..." + "\r\n");
if (!m_ASCProt.OpenRSCom(_BaudRate, _SerialPort)) //
unable to connect
{
this.gBCommands.Enabled = false;
this.tbOutput.AppendText("Connecting to " +
_SerialPort + " @ " + _BaudRate + " Baud failled \r\n");
this.toolStripStatusLabel1.Text = "Not connected";
}
else // successfull connection
{
this.tbOutput.Clear();
this.tbOutput.AppendText("Serial Port " +
_SerialPort + " @ " + _BaudRate + "Baud is up and OK.\r\nNow
initializing the reader... \r\n");
if (!InitReaderRs232())
{
this.tbOutput.AppendText("Reader Initialization
failled" + "\r\n");
}
else {
this.tbOutput.AppendText("Reader Initialization
is Ok" + "\r\n");
boolConnectedSerial = true;
this.toolStripStatusLabel1.Text = "Connected";
this.gBCommands.Enabled = true;
this.gBEth.Enabled = false;
this.lSerialPort.Enabled = false;
this.cBSerialPort.Enabled = false;
this.btConDisconnectRS232.Text = "Disconnect";
}
}
}
else
{ // already connected: Disconnect
boolConnectedSerial = false;
//m_ASCProt.CloseSocket();
m_ASCProt.CloseRSCom();
this.toolStripStatusLabel1.Text = "Disconnected";
this.gBCommands.Enabled = false;
this.gBEth.Enabled = true;
this.btConDisconnectRS232.Text = "Connect";
this.lSerialPort.Enabled = true;
this.cBSerialPort.Enabled = true;
}
}
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void cbBReaderSelect_SelectedIndexChanged(object
sender, EventArgs e)
{
}
private void cBCommand_SelectedIndexChanged(object sender,
EventArgs e)
{
int selectedIndex = this.cBCommand.SelectedIndex;
switch (selectedIndex) {
case 0:
stCommand = stCommand_InventoryAnntenna_1;
this.lParam.Enabled = false;
this.cBParam.Enabled = false;
break;
case 1:
stCommand = stCommand_SetPollingOn;
this.lParam.Enabled = false;
this.cBParam.Enabled = false;
break;
case 2:
stCommand = stCommand_SetPollingOff;
this.lParam.Enabled = false;
this.cBParam.Enabled = false;
break;
case 3:
this.cBParam.SelectedIndex = 3;
stCommand = stCommand_SetMaxScanFail +
this.cBParam.SelectedItem.ToString();
this.lParam.Enabled = true;
this.cBParam.Enabled = true;
break;
case 4:
this.cBParam.SelectedIndex = 4;
stCommand = stCommand_SetTimeSlots +
this.cBParam.SelectedItem.ToString();
this.lParam.Enabled = true;
this.cBParam.Enabled = true;
break;
case 5:
stCommand = stCommand_ReadWithQuietMode;
this.lParam.Enabled = false;
this.cBParam.Enabled = false;
break;
case 6:
stCommand = stCommand_ReadWithoutQuietMode;
this.lParam.Enabled = false;
this.cBParam.Enabled = false;
break;
case 7:
this.cBParam.SelectedIndex = 7; //P08106 (default
is 0x06)
stCommand = stCommand_SetMaxScanRepeat +
this.cBParam.SelectedItem.ToString();
this.lParam.Enabled = true;
this.cBParam.Enabled = true;
break;
case 8:
this.cBParam.SelectedIndex = 8; // default is
0xC0=19200
stCommand = stCommand_SetBaudRate +
this.cBParam.SelectedItem.ToString();
this.lParam.Enabled = true;
this.cBParam.Enabled = true;
break;
case 9:
this.cBParam.SelectedIndex = 9; //0x00 to 0x28
default is 0x14 =4watts
stCommand = stCommand_SetRfPowerLevel +
this.cBParam.SelectedItem.ToString();
this.lParam.Enabled = true;
this.cBParam.Enabled = true;
break;
case 10:
stCommand = stCommand_SwitchRfPowerOn; //P09001
default
this.lParam.Enabled = false;
this.cBParam.Enabled = false;
break;
case 11:
stCommand = stCommand_SwitchRfPowerOff; //P09000
this.lParam.Enabled = false;
this.cBParam.Enabled = false;
break;
case 12:
this.cBParam.SelectedIndex = 12;
stCommand = stCommand_RequestReaderParameters +
this.cBParam.SelectedItem.ToString();
this.lParam.Enabled = true;
this.cBParam.Enabled = true;
break;
case 13:
stCommand = stCommand_GetReaderVersion;
this.lParam.Enabled = false;
this.cBParam.Enabled = false;
break;
default:
// stCommand ;
this.lParam.Enabled = false;
this.cBParam.Enabled = false;
break;
}
}
private void cBSerialPort_SelectedIndexChanged(object sender,
EventArgs e)
{
_SerialPort = this.cBSerialPort.SelectedItem.ToString();
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// END OF THE CODE THAT DOES NOT WORK
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// THE CODE BELOW DOES WORK!!!!
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using ASCHFProtLib;
namespace TestConApp_ASCHFDll
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static string _IPAdd;
static int _Port;
static string _Comport;
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
bool exit = false;
int index;
string rstr;
HFProt m_ASCProt;
try
{
_Comport = string.Empty;
Console.WriteLine("Console Test Application using
ASCHFProtLib.dll");
Console.WriteLine("10.11.2005\n");
Console.WriteLine("Type Exit to close!!!\n\n");
m_ASCProt = new ASCHFProtLib.HFProt();
m_ASCProt.OnRS232AscMsg += new
ASCHFProtLib.HFProt.MsgRS232Received(OnMsgRecv);
m_ASCProt.OnEthConnect += new
ASCHFProtLib.HFProt.EthConnect(OnConnect);
m_ASCProt.OnEthAscMsg += new
ASCHFProtLib.HFProt.MsgEthReceived(OnMsgEthRecv);
Console.WriteLine("Enter 'Open COM1' to open COM Port
1.");
Console.WriteLine("Enter 'Open IP:192.168.100.101
Port:3001' to open Ethernet Port.");
Console.WriteLine("Enter 'Close COM' to close COM
Port.");
Console.WriteLine("Enter 'Close Eth' to close Eth
Port.");
while (!exit)
{
rstr = Console.ReadLine().ToUpper();
if (rstr.IndexOf("EXIT") == 0)
exit = true;
else if (rstr.IndexOf("OPEN") == 0)
{
index = rstr.IndexOf("COM");
if (index 0)
{
_Comport = rstr.Substring(index);
if (m_ASCProt.OpenRSCom(19200, _Comport))
{
Console.WriteLine(_Comport + " : 19200
Boud connected!");
}
else
Console.WriteLine(_Comport + " could
not be opened!");
}
else
{
index = rstr.IndexOf("IP:");
if (index 0)
{
_IPAdd = rstr.Substring(index + 3,
rstr.IndexOf(' ', index + 3) - (index + 3));
_Port =
Convert.ToInt32(rstr.Substring(rstr.IndexOf("PORT: ") +
"PORT:".Length));
if (!m_ASCProt.OpenSocket(_IPAdd,
_Port))
Console.WriteLine("Error openning
port!\n");
}
else
Console.WriteLine("Wrong
command!!!\n");
}
}
else if (rstr.IndexOf("CLOSE") == 0)
{
if (rstr.IndexOf("COM") 0)
{
m_ASCProt.CloseRSCom();
Console.WriteLine(_Comport + " closed!");
}
else if (rstr.IndexOf("ETH") 0)
{
m_ASCProt.CloseSocket();
}
else
Console.WriteLine("Wrong command!!!\n");
}
else
{
if (m_ASCProt.SendRS232(rstr))
{
Console.WriteLine("SendCom: " + rstr);
if (m_ASCProt.SendSocket(rstr))
Console.WriteLine("SendEth: " + rstr);
}
else if (m_ASCProt.SendSocket(rstr))
Console.WriteLine("SendEth: " + rstr);
else
Console.WriteLine("Not send!");
}
}
m_ASCProt.CloseRSCom();
Console.WriteLine(_Comport + " closed!");
m_ASCProt.CloseSocket();
m_ASCProt.OnRS232AscMsg -= new
ASCHFProtLib.HFProt.MsgRS232Received(OnMsgRecv);
m_ASCProt.OnEthConnect -= new
ASCHFProtLib.HFProt.EthConnect(OnConnect);
m_ASCProt.OnEthAscMsg -= new
ASCHFProtLib.HFProt.MsgEthReceived(OnMsgEthRecv);
m_ASCProt = null; Console.WriteLine("RETURN!");
Console.Read();
}
catch
{ }
}
private static void OnMsgRecv(object sender, string msg)
{
Console.WriteLine("RecvCom: " + msg + "\n");
}
private static void OnConnect(object sender, bool connect)
{
if (connect)
Console.WriteLine("IPAdd:" + _IPAdd + " Port: " +
_Port.ToString() + " connected!");
else
Console.WriteLine("IPAdd:" + _IPAdd + " Port: " +
_Port.ToString() + " disconnected!");
}
private static void OnMsgEthRecv(object sender, string msg)
{
Console.WriteLine("RecvEth: " + msg + "\n");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// END OF THE CODE THAT DOES WORK
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////