串口侦听C#中的windows服务

时间:2015-09-15 04:37:28

标签: c# windows-services

我已经创建了一个用于监听串口输入的Windows服务,下面是我的代码,但我得到了一个错误,比如缺少汇编,我用谷歌搜索了但很少有文章说添加System.WIndows.Forms命名空间,即使我添加了我得到同样的错误

  

错误1' VisionSystemService.VisionSystemService'不包含   ' BeginInvoke'的定义没有扩展方法' BeginInvoke'   接受第一个类型的参数   ' VisionSystemService.VisionSystemService'可以找到(是你   缺少using指令或程序集引用?)

 public partial class VisionSystemService : ServiceBase
    {
        SerialPort _SerialPort;
        private delegate void SetTextDeleg(string text);

        public VisionSystemService()
        {
            InitializeComponent();
            _SerialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
            _SerialPort.Handshake = Handshake.None;
            _SerialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
            _SerialPort.ReadTimeout = 500;
            _SerialPort.WriteTimeout = 500;
            _SerialPort.Open();

        }
        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(500);

            string data = _SerialPort.ReadExisting();
            this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
        }

        protected override void OnStart(string[] args)
        {




        }


        private void si_DataReceived(string data)
        {

        }

        protected override void OnStop()
        {

        }
    }

0 个答案:

没有答案