Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello dear friend,
Thanks for your useful article. That was very useful and very interesting to me.

I have another problem.
My project is Attendance Device. In this project I have 2 card reader that read card info from com port and transfer them to server and save in database, by using WCF Service. WCF listen the port and when card reader read a card, raised event and save data in database. Also i created my custome event in WCF service like this

C#
public delegate void PortInfoReceivedEventHandler(string portInfo);


C#
public class CardReaderWCFService : ICardReaderWCFService
    {

        <pre lang="cs">CardReaderData CardReaderData = new CardReaderData();



C#
public event PortInfoReceivedEventHandlerPortInfoReceived = delegate { };



C#
public void Connect()
        {
            SetPortInfo();
        }



C#
private void SetPortInfo(string portName, int buadRate, System.IO.Ports.Parity parity, int dataBits)
        {
            port = new SerialPort("COM4", 38400, System.IO.Ports.Parity.Odd, 8);
            //port = new SerialPort(portName, buadRate, parity, dataBits);
            port.Handshake = Handshake.XOnXOff;
            //port.DtrEnable = true;
            //port.RtsEnable = true;

            try
            {
                port.Open();

            }
            catch (Exception exp)
            {
                throw exp;
            }

            port.DataReceived += new
            SerialDataReceivedEventHandler(port_DataReceived);

        }


and

C#
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            try
            {

                Thread.Sleep(500);
                string portInfo = port.ReadExisting();

                if (portInfo != "")
                {
                    DateTime readingDate = DateTime.Now;



                    int index = 0;
                    index = portInfo .IndexOf("$");
                    string strPortNumber = portInfo.Substring(index + 1, 3);
                    int portNumber = Convert.ToInt32(strPortNumber);


                    int endIndexOfCardNumber = 0;
                    endIndexOfCardNumber = portInfo.IndexOf("**");
                    string strCardNumber = portInfo.Substring(index + 4, endIndexOfCardNumber - 4);




                    string strDate = readingDate.Year.ToString() + "/" + (readingDate.Month < 10 ? "0" + readingDate.Month.ToString() : readingDate.Month.ToString()) + "/"
                                                                       + (readingDate.Day < 10 ? "0" + readingDate.Day.ToString() : readingDate.Day.ToString());// readingDate.Date.ToString().Substring(0, 10);
                    string strTime = readingDate.TimeOfDay.ToString().Substring(0, 8);

                    //


                    portInfo = strPortNumber + "," + strCardNumber + "," + portNumber + "," + strDate + "," + strTime;



                    if (PortInfoReceived_Handler != null)
                    {
                        this.PortInfoReceived_Handler(portInfo);
                    }
                }

            }
            catch (Exception exp)
            {
                port.Close();
            }
        }



in the last event 'port_DataReceived' i put my custom event 'this.PortInfoReceived_Handler(portInfo)'
this event create a string of all of data after reading card. This staring will show in listbox in windowsform.


this code work good. But i want handle my custom event in another project under windows form.
I have windows project thet refrenced to WCF (web service).
when i get instance from WCF service i can get created web method and iterface.
How can i get my custom event 'PortInfoReceived_Handler' and handle it under my form.


In Windows Form :


C#
CardReader_WCF.CardReaderWCFServiceClient crWCFService = new CardReader_WCF.CardReaderWCFServiceClient();


I want impelement custom event like this :

C#
crWCFService.PortInfoReceived_Handler += crFacade_PortInfoReceived_Event;


and

C#
public void crFacade_PortNameReceived_Event(string portInfo)
        {
            try
            {
                SetText(portInfo);
            }
            catch (Exception exp)
            {
                
                throw exp;
            }
        }


and

C#
delegate void SetTextCallback(string text);


C#
private void SetText(string text)
        {
            if (this.lstReceivedMessage.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.lstReceivedMessage.Items.Add(text);
                this.lstBuffer.Items.Add(text);
            }
        }



Would you please help me how can i do it?
Thank you so much
Posted
Comments
DamithSL 7-Nov-14 5:02am    
which tutorial you refering? you better ask this question below the comments area of the tutorial
Farzad Niknam B 7-Nov-14 6:01am    
http://www.codeproject.com/Tips/150702/Use-Custom-Events-from-your-WCF-ServiceHost
Farzad Niknam B 7-Nov-14 10:21am    
Thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900