0

I'm trying to write windows service to get value from device via COM-port. Firstly I write winform application. It works normaly. After I switched to service aplication. What I did: get list of my COM-devices with their connection parameters; then start usual thread for each device with methods that creates instanse COM-port class which encapsulate all port needs as parameters, SerialDataReceivedEventHandler and instance for writing to DB. Problem is: in service there isn't firing this event but in winform application I use the same combination - it works nice.

        void CreateScaleConn(ManualResetEventSlim mre, DataControl.Scale scaleItem)
    {
        CommunicationManager CommMan = new CommunicationManager(scaleItem.baudRate, scaleItem.parity, scaleItem.stopBits, scaleItem.dataBits, scaleItem.portName, DataCtrl.ScaleList.IndexOf(scaleItem));
        CommMan.CurrentTransmissionType = CommunicationManager.TransmissionType.Text;
        CommMan.OpenPort(DataCtrl.ConnStr, DataCtrl.InsCmd);
        if (!ExecutionAllowed) mre.Set();
    }


    protected override void OnStart(string[] args)
    {            
        string loadResDB, loadResDev;
        DataCtrl.LoadDBConf(out loadResDB);
        DataCtrl.LoadDevList(out loadResDev);
        AddLog("DB config load result is" + loadResDB);
        AddLog("Device config load result is" + loadResDev);            
        if (DataCtrl.ConnStr != null && DataCtrl.InsCmd != null)
        {                
            AddLog("CAS service Started");               
            foreach (DataControl.Scale scale in DataCtrl.ScaleList)
            {
                Thread scaleThread = new Thread(() => CreateScaleConn(mreS, scale));
                scaleThread.Name = "thr_" + scale.name;
                ThrArr.Add(scaleThread);
                AddLog("CAS service is started");                    
                scaleThread.Start();                    
            }
            mreS.WaitHandle.WaitOne();            
        }           
    }

and my class for port communication looks:

       public CommunicationManager(string baud, string par, string sBits, string dBits, string name, int scaleIndex)
    {
        _baudRate = baud;
        _parity = par;
        _stopBits = sBits;
        _dataBits = dBits;
        _portName = name;
        _scaleIndex = scaleIndex;     

        comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
    }


public bool OpenPort(string cStr, string insCmd)
    {
        try
        {                
            string DbConnState;                
            if (cStr != null & insCmd != null) DBCtrl.CreateConnToDB(cStr, insCmd, out DbConnState);
            if (comPort.IsOpen == true) comPort.Close();
            comPort.BaudRate = int.Parse(_baudRate);    //BaudRate
            comPort.DataBits = int.Parse(_dataBits);    //DataBits
            comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), _stopBits);    //StopBits
            comPort.Parity = (Parity)Enum.Parse(typeof(Parity), _parity);    //Parity
            comPort.PortName = _portName;   //PortName
            comPort.Open();               
        }
        catch (Exception ex)
        {
           ...
        }
    }


void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {           
        switch (CurrentTransmissionType)
        ...
                string msg = comPort.ReadExisting();  
                DisplayData(MessageType.Incoming, msg );...
    }
sad1002
  • 29
  • 4

0 Answers0