Exemple #1
0
 /// <summary>
 /// Open serial port in comModel
 /// </summary>
 /// <param name="portName"></param>
 /// <param name="baudRate"></param>
 /// <param name="dataBits"></param>
 /// <param name="stopBits"></param>
 /// <param name="parity"></param>
 /// <param name="handshake"></param>
 public void OpenSerialPort(string portName, String baudRate,
                            string dataBits, string stopBits, string parity, string handshake)
 {
     if (portName != null && portName != "")
     {
         comModel.Open(portName, baudRate, dataBits, stopBits, parity, handshake);
     }
 }
Exemple #2
0
        /// <summary>
        /// 发送指令串到led显示屏
        /// </summary>
        /// <param name="portName"></param>
        /// <param name="msg"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool sendMsg2Led(string portName,
                                       string msg,
                                       out string error)
        {
            error = "";

            ComModel _comModel = new ComModel();

            try
            {
                // 打开串口
                var ret = _comModel.Open(portName,
                                         "57600", //baudRate,
                                         "8",     //dataBits,
                                         "One",   //stopBits,
                                         "None",  //parity,
                                         "None",  //handshake
                                         out error);
                if (ret == false)
                {
                    return(false);
                }

                // 将字符转为二进制
                System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
                byte[] data = encoding.GetBytes(msg);

                // 给串口发送二进制
                bool bRet = _comModel.Send(data, out string strError);
                if (bRet == false)
                {
                    error = $"给串口({portName})发送消息失败: {strError}";
                    return(false);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(false);
            }
            finally
            {
                // 关闭串口
                _comModel.Close();
            }

            return(true);
        }
Exemple #3
0
 // Use this for initialization
 void Start()
 {
     com.Open("COM4", "9600", "8", "1", "None", "None");
 }