Example #1
0
        /// <summary>
        /// Serial 데이터를 수신 받았을 때 발생하는 이벤트
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int nLength = mSerialPort.BytesToRead;

            byte[]        buffer        = new byte[nLength];
            StringBuilder stringBuilder = new StringBuilder();

            mSerialPort.Read(buffer, 0, nLength);

            foreach (byte bData in buffer)
            {
                mDataBuffer.Add(bData);
            }

            int nSize = mDataBuffer.Count;

            if (nSize > 2)
            {
                if ((byte)mDataBuffer[nSize - 2] == '\r' &&
                    (byte)mDataBuffer[nSize - 1] == '\n')
                {
                    stringBuilder.AppendFormat("{0}{1}: ", Languages.Log_Receive, "\t");
                    foreach (byte bData in mDataBuffer)
                    {
                        if (bData == '\r' ||
                            bData == '\n')
                        {
                            break;
                        }
                        stringBuilder.Append(Convert.ToChar(bData));
                    }
                    stringBuilder.AppendLine();

                    mDataBuffer.Clear();
                }
            }

            if (WriteLogEvent != null)
            {
                WriteLogEvent(stringBuilder.ToString());
            }

            if (LogSave)
            {
                DateTime dateNow    = DateTime.Now;
                string   strDateNow = string.Format("{0}_{1}{2:D2}{3:D2}{4}", SerialName, dateNow.Year, dateNow.Month, dateNow.Day, LogMgr.LOG_EXTENSION);
                string   strLogPath = string.Empty;

                if (LogSavePath.LastIndexOf(@"\") != LogSavePath.Length - 1)
                {
                    strLogPath = string.Format(@"{0}{1}{2}", LogSavePath, Path.DirectorySeparatorChar, strDateNow);
                }
                else
                {
                    strLogPath = string.Format(@"{0}{1}", LogSavePath, strDateNow);
                }

                LogMgr.WriteLog(strLogPath, stringBuilder.ToString(), true);
            }
        }
Example #2
0
        public void SendData(string strData)
        {
            if (string.IsNullOrEmpty(strData) == false)
            {
                StringBuilder stringBuilder = new StringBuilder();
                if (mSerialPort.IsOpen)
                {
                    try
                    {
                        mSerialPort.Write(strData);
                        stringBuilder.AppendFormat("{0}{1}: {2}", Languages.Log_Send, "\t", strData);
                        stringBuilder.AppendLine();
                    }
                    catch (Exception ex)
                    {
                        LogMgr.WriteLog(LogMgr.LogType.COM, ex.Message);
                    }
                }
                else
                {
                    stringBuilder.AppendFormat("{0}{1}: {2} - {3}", Languages.Log_Send, "\t", strData, Languages.Log_Fail);
                    stringBuilder.AppendLine();
                }

                if (WriteLogEvent != null)
                {
                    WriteLogEvent(stringBuilder.ToString());
                }

                if (LogSave)
                {
                    DateTime dateNow    = DateTime.Now;
                    string   strDateNow = string.Format("{0}_{1}{2:D2}{3:D2}{4}", SerialName, dateNow.Year, dateNow.Month, dateNow.Day, LogMgr.LOG_EXTENSION);
                    string   strLogPath = string.Empty;

                    if (LogSavePath.LastIndexOf(@"\") != LogSavePath.Length - 1)
                    {
                        strLogPath = string.Format(@"{0}{1}{2}", LogSavePath, Path.DirectorySeparatorChar, strDateNow);
                    }
                    else
                    {
                        strLogPath = string.Format(@"{0}{1}", LogSavePath, strDateNow);
                    }

                    LogMgr.WriteLog(strLogPath, strData, true);
                }
            }
        }