المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Serial Port DataReceived method in a Windows Service



C# Programming
06-29-2009, 08:51 PM
I have a solution working with serial port ( COM1 ) . I added a serial port module from toolbox to a form and it worked properly , but then I was asked to use this in a windows service . I added a serial port to my service and then installed the service correctly ( I am sure ) and in the event log I could see the message I wrote in OnStart() method , but I don't know why the program doesn't go to the Data_Received event of the serial port ! Can any one help me! Her's the code:
protected override void OnStart(string[] args)
{
LogEvent("TestService started !", EventLogEntryType.Warning);
}

protected override void OnStop()
{
LogEvent("TestService stopped !", EventLogEntryType.Warning);
}
-------MY PROBLEM is that this block of code doesn't run------------------
private void sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
LogEvent("DataReceived !", EventLogEntryType.Warning);
}
--------------------------------------------------------------------------
static void LogEvent(String Message, EventLogEntryType type)
{
String source = "TestService";
String log = "Application";
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, log);
}
EventLog eLog = new EventLog();
eLog.Source = source;
eLog.WriteEntry(Message, type);
}http://www.barakasoft.com/script/Forums/Images/smiley_confused.gif