Пример #1
0
        public COMResult OpenCom()
        {
            COMResult result = new COMResult();

            try
            {
                comDevice.Open();
                comDevice.DataReceived += Com_DataReceived;
                result.Success          = true;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="data"></param>
        public COMResult SendData(byte[] data)
        {
            COMResult result = new COMResult();

            if (comDevice.IsOpen)
            {
                try
                {
                    comDevice.Write(data, 0, data.Length);//发送数据
                    result.Success = true;
                }
                catch (Exception ex)
                {
                    result.Msg = ex.Message;
                }
            }
            else
            {
                result.Msg = "串口未打开";
            }
            return(result);
        }