spc-kiosk-pb/Agent/Measuring/ScalePoleDisplay.cs
2019-06-16 14:12:09 +09:00

156 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using Cosmos.UserFrame;
namespace Cosmos.Measuring
{
class ScalePoleDisplay
{
#region
private static SerialPort m_cSerialPort = null;
#endregion
#region
#endregion
#region OPEN / CLOSE
public bool OpenScalePoleDisplay(string sSerialPort, long lBaudrate)
{
bool bRet = false;
try
{
if(m_cSerialPort != null)
{
//grayber@20180307 시리얼 포트 flush 추가 start - Open 함수 Input Output 버퍼 삭제(주석처리)
m_cSerialPort.DiscardInBuffer();
m_cSerialPort.DiscardOutBuffer();
//grayber@20180307 시리얼 포트 flush 추가 end
m_cSerialPort.Close();
System.Threading.Thread.Sleep(50);
m_cSerialPort = null;
}
m_cSerialPort = new SerialPort();
m_cSerialPort.NewLine = "\r\n";
m_cSerialPort.Encoding = Encoding.Default;
m_cSerialPort.WriteBufferSize = 512;
m_cSerialPort.PortName = sSerialPort;
m_cSerialPort.BaudRate = (int)lBaudrate;
m_cSerialPort.ReadTimeout = 500;
m_cSerialPort.WriteTimeout = 2000;
m_cSerialPort.DataBits = 8;
m_cSerialPort.StopBits = StopBits.One;
m_cSerialPort.Parity = Parity.None;
m_cSerialPort.Handshake = Handshake.None;
m_cSerialPort.Open();
if(m_cSerialPort.IsOpen == false)
{
UserLog.WriteLogFile(UserCom.LOG_IOS,
UserCom.WARNING_LEVEL,
System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명)
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명))
System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명))
"SERIAL PORT OPEN ERROR");
return bRet;
}
}
catch (Exception ex)
{
UserLog.WriteLogFile(UserCom.LOG_IOS,
System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명)
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명))
System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명))
ex.Message);
}
//grayber@20171222 저울,라벨프린터, PoleDisplay 디버그 로그 추가 start
#if true // 로그추가
//grayber@20171222 디버그 로그 추가 - 저울 디버그
UserLog.WriteLogFile(UserCom.LOG_OP,
System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명)
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명))
System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명))
"▶▶ [OPEN]" + bRet);
#endif
//grayber@20171222 저울,라벨프린터, PoleDisplay 디버그 로그 추가 end
return bRet;
}
public bool CloseScalePoleDisplay()
{
bool bRet = false;
try
{
if(m_cSerialPort == null)
{
UserLog.WriteLogFile(UserCom.LOG_IOS,
System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명)
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명))
System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명))
"Close fail(m_cSerialPort_Scale == null)");
return bRet;
}
//grayber@20180307 시리얼 포트 flush 추가 start - Close 함수 Input Output 버퍼 삭제
m_cSerialPort.DiscardInBuffer();
m_cSerialPort.DiscardOutBuffer();
//grayber@20180307 시리얼 포트 flush 추가 end
m_cSerialPort.Close();
bRet = true;
//grayber@20180307 시리얼 포트 flush 추가 start - try 문 밖으로 이동 (기존)
//m_cSerialPort = null;
//grayber@20180307 시리얼 포트 flush 추가 end
}
catch (Exception ex)
{
UserLog.WriteLogFile(UserCom.LOG_IOS,
System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명)
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명))
System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명))
ex.Message);
}
//grayber@20180307 시리얼 포트 flush 추가 start - try 문 밖으로 이동 (변경)
m_cSerialPort = null;
//grayber@20180307 시리얼 포트 flush 추가 end
return bRet;
}
#endregion OPEN / CLOSE
#region
public void WriteToScalePoleDisplay(string sScaleData)
{
try
{
m_cSerialPort.WriteLine(sScaleData);
System.Threading.Thread.Sleep(500);
m_cSerialPort.WriteLine(sScaleData);
System.Threading.Thread.Sleep(500);
m_cSerialPort.WriteLine(sScaleData);
System.Threading.Thread.Sleep(500);
}
catch (Exception ex)
{
UserLog.WriteLogFile(UserCom.LOG_IOS,
System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명)
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명))
System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명))
ex.Message);
}
}
#endregion
}
}