コード例 #1
0
        /// <summary>
        /// Serial Port에 접속
        /// </summary>
        /// <param name="serialInfo">Serial 통신 정보 객체</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Connect(SerialInfo serialInfo = null)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (serialInfo == null && this.serialInfo == null)
                {
                    result.isSuccess = false;
                }
                else
                {
                    if (serialHandle == null)
                    {
                        serialHandle = new SerialPort();
                    }

                    if (serialInfo != null)
                    {
                        serialHandle.PortName = serialInfo.portName;
                        serialHandle.BaudRate = serialInfo.baudRate;
                        serialHandle.Parity   = serialInfo.parity;
                        serialHandle.DataBits = serialInfo.dataBits;
                        serialHandle.StopBits = serialInfo.stopBits;

                        serialHandle.Open();

                        this.serialInfo = serialInfo;
                    }
                    else
                    {
                        serialHandle.Open();
                    }
                }

                if (serialHandle.IsOpen)
                {
                    result.isSuccess = true;
                }
                else
                {
                    result.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// MQTT Broker에 접속
        /// </summary>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Connect()
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                mqttHandle.Connect(Guid.NewGuid().ToString());

                if (mqttHandle.IsConnected)
                {
                    result.isSuccess = true;
                }
                else
                {
                    result.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// MS-SQL 접속 해제
        /// </summary>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult DisConnect()
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (dbState == System.Data.ConnectionState.Open)
                {
                    mssqlConn.Close();
                }

                //LogHandler.WriteLog(string.Empty, string.Format("{0} :: DisConnect() Success", this.ToString()));

                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                //LogHandler.WriteLog(string.Empty, string.Format("{0} :: DisConnect() Exception :: Message = {1}", this.ToString(), ex.Message));

                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #4
0
        /// <summary>
        /// Modbus TCP Slave에 접속
        /// </summary>
        /// <param name="slaveIp">Modbus Slave Ip</param>
        /// <param name="slavePort">Modbus Slave Port</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Connect(string slaveIp, int slavePort)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                tcpHandler.Connect();

                if (tcpHandler.isConnect)
                {
                    result.isSuccess = true;
                }
                else
                {
                    result.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #5
0
        /// <summary>
        /// MS-SQL 접속
        /// </summary>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Connect()
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (hostName.Equals(string.Empty) || dbName.Equals(string.Empty) || dbId.Equals(string.Empty) || dbPw.Equals(string.Empty))
                {
                    //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Fail :: Database Info Not Initialzed", this.ToString()));

                    result.isSuccess = false;
                }
                else
                {
                    connectionString = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};Pooling=true;Max Pool Size=10;Connection Timeout=5", hostName, dbName, dbId, dbPw);

                    if (dbState != ConnectionState.Open)
                    {
                        mssqlConn.ConnectionString = connectionString;
                        mssqlConn.Open();

                        if (dbState == ConnectionState.Open)
                        {
                            //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Success", this.ToString()));

                            result.isSuccess = true;
                        }
                        else
                        {
                            //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Fail", this.ToString()));

                            result.isSuccess = false;
                        }
                    }
                    else
                    {
                        //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Success :: Already Open", this.ToString()));

                        result.isSuccess = true;
                    }
                }
            }
            catch (Exception ex)
            {
                //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Exception :: Message = {1}", this.ToString(), ex.Message));

                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #6
0
        /// <summary>
        /// MQTT Broker에 Topic 발행 (송신)
        /// </summary>
        /// <param name="mqttTopic">Topic 명</param>
        /// <param name="topicMessage">메시지</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Publish(string mqttTopic, string topicMessage)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            try
            {
                if (mqttHandle.IsConnected)
                {
                    byte[] sendBytes     = Encoding.UTF8.GetBytes(topicMessage);
                    ushort publishResult = mqttHandle.Publish(mqttTopic, sendBytes);

                    result.isSuccess = true;
                }
                else
                {
                    result.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            return(result);
        }
コード例 #7
0
        /// <summary>
        /// Serial Port에 접속
        /// </summary>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Connect()
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                serialHandler.Connect();

                if (serialHandler.isConnect)
                {
                    result.isSuccess = true;
                }
                else
                {
                    result.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.isSuccess = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return result;
        }
コード例 #8
0
ファイル: FtpHandler.cs プロジェクト: a3255/Gunsol.Library
        /// <summary>
        /// 지정 FTP 경로에 파일 업로드
        /// </summary>
        /// <param name="remotePath">업로드 경로</param>
        /// <param name="localPath">로컬 파일 경로</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Upload(string remotePath, string localPath)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();
            Exception funcException        = null;
            bool      isSuccess            = false;

            WebClient  ftpClient  = new WebClient();
            FileStream fileStream = null;

            //stopWatch.Start();

            try
            {
                if (this.ftpUserId.Equals(string.Empty) || this.ftpUserPw.Equals(string.Empty))
                {
                    isSuccess = false;
                }
                else
                {
                    ftpClient.Credentials = ftpUserInfo;

                    FileInfo fileInfo      = new FileInfo(localPath);
                    byte[]   localFileData = new byte[fileInfo.Length];

                    fileStream = new FileStream(localPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                    fileStream.Read(localFileData, 0, localFileData.Length);

                    ftpClient.UploadData(remotePath, localFileData);

                    isSuccess = true;
                }

                funcException = null;
            }
            catch (Exception ex)
            {
                isSuccess     = false;
                funcException = ex;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }

            //stopWatch.Stop();

            result.isSuccess     = isSuccess;
            result.funcException = funcException;
            //result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            //stopWatch.Reset();

            return(result);
        }
コード例 #9
0
        /// <summary>
        /// TCP Server/Client에 접속
        /// </summary>
        /// <param name="tcpInfo">TCP 통신 정보 객체</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Connect(TcpInfo tcpInfo = null)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (tcpInfo == null && this.tcpInfo == null)
                {
                    result.isSuccess = false;
                }
                else
                {
                    if (tcpHandle == null)
                    {
                        tcpHandle = new TcpClient();
                    }

                    if (tcpInfo != null)
                    {
                        tcpHandle.Connect(tcpInfo.ipAddress, tcpInfo.portNo);

                        this.tcpInfo = tcpInfo;
                    }
                    else
                    {
                        tcpHandle.Connect(this.tcpInfo.ipAddress, this.tcpInfo.portNo);
                    }

                    if (tcpHandle.Connected)
                    {
                        result.isSuccess = true;
                    }
                    else
                    {
                        result.isSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #10
0
ファイル: FileHandler.cs プロジェクト: a3255/Gunsol.Library
        /// <summary>
        /// 지정 경로의 파일 복사
        /// </summary>
        /// <param name="destinationPath">복사 파일 경로 (생략할 경우 filePath Property 사용)</param>
        /// <param name="filePath">원본 파일 경로</param>
        /// <param name="isOverwrite">덮어쓰기 여부 (생략할 경우 전체 검색)</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult CopyFile(string destinationPath, string filePath = null, bool isOverwrite = false)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();
            bool      isSuccess            = false;
            Exception funcException        = null;

            stopWatch.Start();

            try
            {
                if (filePath != null)
                {
                    this.filePath = filePath;
                }

                if (isExist)
                {
                    string destFilePath = string.Empty;

                    if (destinationPath.Substring(destinationPath.Length - 1, 1).Equals("\\"))
                    {
                        destFilePath = string.Format(@"{0}{1}", destinationPath, this.fileName);
                    }
                    else
                    {
                        destFilePath = string.Format(@"{0}\{1}", destinationPath, this.fileName);
                    }

                    System.IO.File.Copy(this.filePath, destFilePath, isOverwrite);

                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                isSuccess     = false;
                funcException = ex;
            }


            stopWatch.Stop();

            result.isSuccess         = isSuccess;
            result.funcException     = funcException;
            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #11
0
        /// <summary>
        /// DDE Server에 접속
        /// </summary>
        /// <param name="ddeInfo">DDE 통신 정보 객체 (생략할 경우, 생성자 호출시 사용한 Paramter를 이용)</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Connect(DdeInfo ddeInfo = null)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (ddeInfo == null && this.ddeInfo == null)
                {
                    result.isSuccess = false;
                }
                else
                {
                    if (ddeHandle == null)
                    {
                        if (ddeInfo == null)
                        {
                            ddeHandle = new DdeClient(this.ddeInfo.ddeService, this.ddeInfo.ddeTopic);
                        }
                        else
                        {
                            ddeHandle    = new DdeClient(ddeInfo.ddeService, ddeInfo.ddeTopic);
                            this.ddeInfo = ddeInfo;
                        }
                    }

                    ddeHandle.Connect();

                    if (ddeHandle.IsConnected)
                    {
                        result.isSuccess = true;
                    }
                    else
                    {
                        result.isSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #12
0
        /// <summary>
        /// TCP Server/Client에 데이터 송신
        /// </summary>
        /// <param name="sendBytes">송신 데이터</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Send(byte[] sendBytes)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            NetworkStream tcpStream = null;

            stopWatch.Start();

            try
            {
                if (tcpHandle.Connected)
                {
                    tcpStream = tcpHandle.GetStream();

                    if (tcpStream.CanWrite)
                    {
                        tcpStream.Write(sendBytes, 0, sendBytes.Length);
                        tcpStream.Flush();

                        result.isSuccess = true;
                    }
                    else
                    {
                        result.isSuccess = false;
                    }
                }
                else
                {
                    result.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }
            finally
            {
                if (tcpStream != null)
                {
                    tcpStream.Close();
                }
            }

            return(result);
        }
コード例 #13
0
        /// <summary>
        /// MQTT Broker Topic 구독
        /// </summary>
        /// <param name="mqttTopics">Topic 명</param>
        /// <param name="qosLevels">QoS</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Subscribe(List <string> mqttTopics, List <byte> qosLevels)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            try
            {
                ushort subscribeResult = mqttHandle.Subscribe(mqttTopics.ToArray(), qosLevels.ToArray());

                result.isSuccess     = true;
                result.funcException = null;
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            return(result);
        }
コード例 #14
0
ファイル: FileHandler.cs プロジェクト: a3255/Gunsol.Library
        /// <summary>
        /// 지정 경로의 파일 삭제
        /// </summary>
        /// <param name="filePath">삭제 파일 경로 (생략할 경우 filePath Property 사용)</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult DeleteFile(string filePath = null)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (filePath != null)
                {
                    this.filePath = filePath;
                }

                if (isExist)
                {
                    System.IO.File.Delete(this.filePath);

                    result.isSuccess = true;
                }
                else
                {
                    result.isSuccess = false;
                }

                result.funcException = null;
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #15
0
        /// <summary>
        /// Serial Port에 데이터 송신
        /// </summary>
        /// <param name="sendBytes">송신 데이터</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult Send(byte[] sendBytes)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (serialHandle.IsOpen)
                {
                    int sendPacketLength = 0;

                    serialHandle.Write(sendBytes, 0, sendBytes.Length);

                    System.Threading.Thread.Sleep(100);

                    sendPacketLength = serialHandle.BytesToWrite;

                    if (sendPacketLength > 0)
                    {
                        result.isSuccess = true;
                    }
                    else
                    {
                        result.isSuccess = false;
                    }
                }
                else
                {
                    result.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            return(result);
        }
コード例 #16
0
        /// <summary>
        /// DDE Server 접속 해제
        /// </summary>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult DisConnect()
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (ddeHandle != null)
                {
                    if (ddeHandle.IsConnected)
                    {
                        ddeHandle.Disconnect();

                        result.isSuccess = true;
                    }
                    else
                    {
                        result.isSuccess = true;
                    }
                }
                else
                {
                    result.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #17
0
        /// <summary>
        /// Initialize 파일의 설정 값 쓰기
        /// </summary>
        /// <param name="iniPath">Initialize 파일 경로</param>
        /// <param name="section">Initialize Section</param>
        /// <param name="key">Initialize Key</param>
        /// <param name="value">설정 값</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public static CommonStruct.FuncResult SetConfig(string iniPath, string section, string key, string value)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();
            Exception funcException        = null;
            string    resultString         = string.Empty;
            bool      isSuccess            = false;

            stopWatch.Start();

            try
            {
                if (!System.IO.File.Exists(iniPath))
                {
                    System.IO.File.Create(iniPath);
                }

                WritePrivateProfileString(section, key, value, iniPath);

                resultString  = string.Empty;
                isSuccess     = true;
                funcException = null;
            }
            catch (Exception ex)
            {
                resultString  = string.Empty;
                isSuccess     = false;
                funcException = ex;
            }

            stopWatch.Stop();

            result.isSuccess         = isSuccess;
            result.funcException     = funcException;
            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #18
0
        /// <summary>
        /// MQTT Broker 접속 해제
        /// </summary>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult DisConnect()
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (mqttHandle == null)
                {
                    if (isConnect)
                    {
                        subMessage.Clear();

                        mqttHandle.Disconnect();
                    }

                    result.isSuccess = true;
                }
                else
                {
                    result.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #19
0
ファイル: FileHandler.cs プロジェクト: a3255/Gunsol.Library
        /// <summary>
        /// 지정 경로에 파일 생성
        /// </summary>
        /// <param name="filePath">생성 파일 경로 (생략할 경우 filePath Property 사용)</param>
        /// <param name="isOverwrite">덮어쓰기 여부 (생략할 경우 덮어쓰지 않음)</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult CreateFile(string filePath = null, bool isOverwrite = false)
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                FileStream creationFileStream = null;

                if (filePath != null)
                {
                    this.filePath = filePath;
                }

                if (isExist)
                {
                    if (isOverwrite)
                    {
                        System.IO.File.Delete(this.filePath);
                        creationFileStream = System.IO.File.Create(this.filePath);

                        //LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {1}) Success(Overwrite)", this.ToString(), this.filePath));
                    }
                    else
                    {
                        //LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {1}) Fail :: File Already Exist", this.ToString(), this.filePath));
                    }
                }
                else
                {
                    creationFileStream = System.IO.File.Create(filePath);

                    //LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {1}) Success", this.ToString(), this.filePath));
                }

                if (creationFileStream != null)
                {
                    creationFileStream.Close();
                }

                result.isSuccess     = true;
                result.funcException = null;

                #region Comment
                //if (filePath != null)
                //{
                //    if (System.IO.File.Exists(filePath))
                //    {
                //        if (isOverwrite)
                //        {
                //            System.IO.File.Delete(filePath);
                //            System.IO.File.Create(filePath);

                //            LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {1}) Success", this.ToString(), filePath));
                //        }
                //        else
                //        {
                //            LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {1}) Fail :: File Already Exist", this.ToString(), filePath));
                //        }
                //    }
                //    else
                //    {
                //        System.IO.File.Create(filePath);

                //        LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {1}) Success", this.ToString(), filePath));
                //    }
                //}
                //else
                //{
                //    if (isExist)
                //    {
                //        if (isOverwrite)
                //        {
                //            System.IO.File.Delete(this.filePath);
                //            System.IO.File.Create(this.filePath);

                //            LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {1}) Success", this.ToString(), this.filePath));
                //        }
                //        else
                //        {
                //            LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {1}) Fail :: File Already Exist", this.ToString(), this.filePath));
                //        }
                //    }
                //    else
                //    {
                //        System.IO.File.Create(filePath);

                //        LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {1}) Success", this.ToString(), this.filePath));
                //    }
                //}
                #endregion
            }
            catch (Exception ex)
            {
                //LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileCreate(Path = {2}) Exception :: Message = {1}", this.ToString(), ex.Message, this.filePath));

                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #20
0
        /// <summary>
        /// Modbus TCP 프로토콜을 사용하여 데이터 읽기
        /// </summary>
        /// <param name="funcCode">함수 Code</param>
        /// <param name="beginAddress">시작 주소</param>
        /// <param name="readCount">데이터 수</param>
        /// <returns>함수 실행 결과 (ProtocolResult 객체)</returns>
        public CommonStruct.ProtocolResult Read(byte funcCode, ushort beginAddress, ushort readCount)
        {
            CommonStruct.ProtocolResult result = new CommonStruct.ProtocolResult();

            stopWatch.Start();

            try
            {
                List <byte> sendData          = new List <byte>();
                List <byte> recvData          = new List <byte>();
                byte[]      beginAddressArray = BitConverter.GetBytes(beginAddress).Reverse().ToArray();
                byte[]      readCountArray    = BitConverter.GetBytes(readCount).Reverse().ToArray();
                byte[]      readDataArray     = new byte[2];

                sendData.AddRange(hDataUnit);
                sendData.Add(funcCode);
                sendData.AddRange(beginAddressArray);
                sendData.AddRange(readCountArray);

                CommonStruct.FuncResult sendResult = tcpHandler.Send(sendData.ToArray());

                if (sendResult.isSuccess)
                {
                    System.Threading.Thread.Sleep(10);

                    CommonStruct.ProtocolResult receiveResult = tcpHandler.Receive();

                    if (receiveResult.funcResult.isSuccess)
                    {
                        recvData = (List <byte>)receiveResult.receiveData;

                        if (recvData.Count > 0)
                        {
                            readDataArray[0] = recvData[recvData.Count - 1];
                            readDataArray[1] = recvData[recvData.Count - 2];

                            result.receiveData          = BitConverter.ToUInt16(readDataArray, 0);
                            result.funcResult.isSuccess = true;
                        }
                        else
                        {
                            result.receiveData          = null;
                            result.funcResult.isSuccess = false;
                        }
                    }
                    else
                    {
                        result.receiveData          = null;
                        result.funcResult.isSuccess = false;
                    }
                }
                else
                {
                    result.receiveData          = null;
                    result.funcResult.isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.receiveData              = null;
                result.funcResult.isSuccess     = false;
                result.funcResult.funcException = ex;
            }

            stopWatch.Stop();

            result.funcResult.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #21
0
ファイル: FileHandler.cs プロジェクト: a3255/Gunsol.Library
        /// <summary>
        /// 지정 경로의 파일 쓰기
        /// </summary>
        /// <param name="fileContents">파일 내용</param>
        /// <param name="filePath">읽을 파일 경로 (생략할 경우 filePath Property 사용)</param>
        /// <param name="isContinue">이어 쓰기 여부 (생략할 경우 기존 파일 내용에 이어 쓰기)</param>
        /// <returns>함수 실행 결과 (FuncResult 객체)</returns>
        public CommonStruct.FuncResult WriteFile(string fileContents, string filePath = null, bool isContinue = true)
        {
            CommonStruct.FuncResult result     = new CommonStruct.FuncResult();
            StreamWriter            fileWriter = null;
            FileStream fileStream    = null;
            Exception  funcException = null;
            bool       isSuccess     = false;

            stopWatch.Start();

            try
            {
                if (filePath == null)
                {
                    if (this.filePath.Equals(string.Empty))
                    {
                        isSuccess = false;
                    }
                }
                else
                {
                    this.filePath = filePath;
                }

                if (isExist)
                {
                    if (!isContinue)
                    {
                        System.IO.File.WriteAllText(this.filePath, string.Empty);
                    }

                    fileStream = new FileStream(this.filePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
                    fileWriter = new StreamWriter(fileStream);
                    fileWriter.BaseStream.Seek(0, SeekOrigin.End);
                    fileWriter.Write(fileContents);
                    fileWriter.Flush();

                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }

                result.funcException = null;
            }
            catch (Exception ex)
            {
                isSuccess     = false;
                funcException = ex;
            }
            finally
            {
                if (fileWriter != null)
                {
                    fileWriter.Close();
                }

                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }

            stopWatch.Stop();

            result.isSuccess         = isSuccess;
            result.funcException     = funcException;
            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }
コード例 #22
0
ファイル: SqliteHandler.cs プロジェクト: a3255/Gunsol.Library
        /// <summary>
        /// 데이터베이스에 접속
        /// </summary>
        /// <returns>IsSuccess</returns>
        private CommonStruct.FuncResult Connect()
        {
            CommonStruct.FuncResult result = new CommonStruct.FuncResult();

            stopWatch.Start();

            try
            {
                if (localDbPath.Equals(string.Empty))
                {
                    //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Fail :: Database Info Not Initialzed", this.ToString()));

                    result.isSuccess = false;
                }
                else
                {
                    connectionString = string.Format("Data source={0}; Version=3;", localDbPath);

                    if (!System.IO.File.Exists(localDbPath))
                    {
                        SQLiteConnection.CreateFile(localDbPath);
                    }

                    if (dbState == ConnectionState.Closed)
                    {
                        sqliteConn.ConnectionString = connectionString;
                        sqliteConn.Open();

                        if (dbState == ConnectionState.Open)
                        {
                            //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Success", this.ToString()));

                            result.isSuccess = true;
                        }
                        else
                        {
                            //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Fail", this.ToString()));

                            result.isSuccess = false;
                        }
                    }
                    else
                    {
                        //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Success :: Already Open", this.ToString()));

                        result.isSuccess = true;
                    }
                }
            }
            catch (Exception ex)
            {
                //LogHandler.WriteLog(string.Empty, string.Format("{0} :: Connect() Exception :: Message = {1}", this.ToString(), ex.Message));

                result.isSuccess     = false;
                result.funcException = ex;
            }

            stopWatch.Stop();

            result.totalMilliseconds = stopWatch.ElapsedMilliseconds;

            stopWatch.Reset();

            return(result);
        }