Здравствуйте, Sshur, Вы писали:
S>Ну, это факт, что инициализаторы полей класса выполняются до конструктора. Не в начале конструктора, а до конструктора. Возможно, у вас из-за этого вылезают какие-то побочные эффекты.
S>Откуда берется объект mySystem? может в первом случае он не существует до того, как создастся объект barcode. Надо смотреть ваш код
S>Если вы поставите точку останова на new Form1 (), то по F11 вы сначала пройдете по всем инициализаторам, а потом по конструктору Form1(). Можно будет разобраться, что там не так
трассировка ничего не даёт. выполняется инициализация barc а дальше InitialiseComponent.
Вот полный код:
следующий этап — COM dll чтобы использовать её в unmanaged MFC приложении.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Cognex.DataMan.SDK;
using Cognex.DataMan.SDK.Events;
using System.Timers;
namespace barcodetest1win
{
public partial class Form1 : Form
{
Barcode barc = null;
public Form1()
{
barc = new Barcode();
InitializeComponent();
barc.Initialise("COM42");
}
private void button1_Click(object sender, EventArgs e)
{
string res = barc.GetBarcode(5000);
label2.Text = res;
}
private void button2_Click(object sender, EventArgs e)
{
if(!barc.IsInitialised())
barc.Initialise("COM42");
}
}
public interface CognexDLL
{
string Initialise(string comport);
string GetBarcode(int timeon);
bool IsInitialised();
int Close();
}
public class Barcode : CognexDLL
{
/// <summary>
/// Provides functionalities needed to work with DataMan devices.
/// </summary>
Cognex.DataMan.SDK.DataManSystem mySystem = null;
System.Timers.Timer tm = null;
public string resultText;
public Barcode()
{
System.Media.SystemSounds.Beep.Play();
resultText = "empty";
mySystem = new Cognex.DataMan.SDK.DataManSystem();
tm = new System.Timers.Timer();
}
void OnTimedEvent(object source, ElapsedEventArgs e)
{
System.Media.SystemSounds.Beep.Play();
resultText = mySystem.TransmittedResults.ToString();
}
public string Initialise(string comport)
{
string address = comport; //Use: "xxx.xxx.xxx.xxx" (IPv4) or "COMx"
if (mySystem == null)
{
mySystem = new DataManSystem();
}
try
{
mySystem.Imaging.Live.Enabled = false;
}
catch (Exception e3)
{
//MessageBox.Show(e3.Message);
}
try
{
mySystem.Connect(new DataManConnectionParams(address));
}
catch (Exception eConnect)
{
//MessageBox.Show(eConnect.Message);
}
//If connected
if (mySystem.IsConnected())
{
System.Media.SystemSounds.Beep.Play();
mySystem.DmccResponseArrived += new Cognex.DataMan.SDK.DataManSystem.DmccResponseArrivedEventHandler(this.mySystem_DmccResponseArrived);
mySystem.ImageArrived += new Cognex.DataMan.SDK.DataManSystem.ImageArrivedEventHandler(this.mySystem_ImageArrived);
mySystem.GraphicArrived += new Cognex.DataMan.SDK.DataManSystem.GraphicArrivedEventHandler(this.mySystem_GraphicArrived);
mySystem.ResultArrived += new Cognex.DataMan.SDK.DataManSystem.ResultArrivedEventHandler(this.mySystem_ResultArrived);
mySystem.HeartBeatSkipped += new EventHandler(mySystem_HeartBeatSkipped);
//mySystem.DmccResponseArrived += delegate { Console.WriteLine("hello world"); };
tm.Interval = 3000;
//tm.Enabled = true;
//tm.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimedEvent);
// tm.Start();
//Timer event works!!!
return comport + "Initialised.";
}
return "failed";
}
/// <summary>
/// Displaying responses to DMCC commands.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void mySystem_DmccResponseArrived(object sender, DmccResponseArrivedEventArgs e)
{
System.Media.SystemSounds.Beep.Play();
//Response arrived - To which sent command?
if (e.Data != null)
{
resultText = e.Data;
}
}
public void mySystem_ResultArrived(object sender, ResultArrivedEventArgs e)
{
System.Media.SystemSounds.Beep.Play();
if (e.Result != null)
{
resultText = e.Result;
}
}
void mySystem_ImageArrived(object sender, ImageArrivedEventArgs e)
{
System.Media.SystemSounds.Beep.Play();
}
void mySystem_GraphicArrived(object sender, GraphicArrivedEventArgs e)
{
System.Media.SystemSounds.Beep.Play();
}
void mySystem_HeartBeatSkipped(object sender, EventArgs e)
{
//MessageBox.Show("Heartbeat skipped");
System.Media.SystemSounds.Beep.Play();
}
public bool IsInitialised()
{
return mySystem.IsConnected();
}
public string GetBarcode(int timeon)
{
//code
mySystem.SendDmcc("||>TRIGGER ON");
System.Threading.Thread.Sleep(timeon / 10);
return resultText;
}
public int Close()
{
return 1;
}
}
}