프로그래밍
C# SECS 메세지 생성 및 S9 메세지 처리 예제
준이바라기얍
2021. 8. 10. 10:44
반응형
아래에 이어서 만들어진 메세지에 대한 송신 및 S9 메세지 처리에 대한
예제 입니다.
참고바랍니다.
void clsEventBridge_Stream9Send(int MsgID, int res, byte[] bHeadData)
{
int SendMsgID = 0;
short nFunc = 0;
try
{
switch ((ValidationCode)res)
{
case ValidationCode.S9F11DataTooLong:
nFunc = 11;
break;
case ValidationCode.S9F1UnrecognizedDeviceID:
nFunc = 1;
break;
case ValidationCode.S9F3UnrecognizedStream:
nFunc = 3;
break;
case ValidationCode.S9F5UnrecognizedFunction:
nFunc = 5;
break;
case ValidationCode.S9F7IllegalData:
nFunc = 7;
break;
case ValidationCode.S9F9TransactionTimerTimeout:
nFunc = 9;
break;
}
MakeSecsMsg(ref SendMsgID, 1, 9, nFunc, 0);
SetBinaryItem(SendMsgID, bHeadData);
SendSECSMsg(SendMsgID);
CloseSecsMsg(MsgID);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// SECS MSG 생성
/// </summary>
/// <param name="nMsgID">MsgID</param>
/// <param name="nDevID">DeviceID</param>
/// <param name="nStrm">Stream</param>
/// <param name="nFunc">Function</param>
/// <param name="nSysByte">SystemByte</param>
/// <returns>ErrCode</returns>
public short MakeSecsMsg(ref int nMsgID, short nDevID, short nStrm, short nFunc, int nSysByte)
{
try
{
clsSECSMessage sTemp = new clsSECSMessage();
sTemp.DeviceID = nDevID;
if (nStrm % 2 == 0) sTemp.Wbit = 8;
else sTemp.Wbit = 0;
sTemp.Stream = nStrm;
sTemp.Function = nFunc;
sTemp.SystemByte = nSysByte;
clsDataManager.MsgID++;
nMsgID = clsDataManager.MsgID;
if (!clsDataManager.MessageList.ContainsKey(nMsgID))
clsDataManager.MessageList.Add(nMsgID, sTemp); //msgID check
else return (short)ErrCode.MsgIdExist;
}
catch (Exception ex)
{
clsMSGLOG.SetLOG(MSGLOGTYPE.SYSTEMERROR, ex.ToString());
return -1;
}
return 0;
}
public short SendSECSMsg(int MsgID)
{
clsSECSMessage csm;
byte[] MsgHeader = new byte[6];
byte[] sbTemp = new byte[4];
string strTemp = string.Empty;
string sLog = string.Empty;
try
{
//MsgID 존재 여부 Check
if (clsDataManager.MessageList.ContainsKey(MsgID))
csm = clsDataManager.MessageList[MsgID];
else return (short)ErrCode.MsgIdNotExist;
MsgHeader[0] = 0x00; // SessionID 0으로 고정
MsgHeader[1] = (byte)csm.DeviceID; //DeviceID
//W-Bit and Stream
strTemp = Convert.ToString(csm.Wbit, 2).PadLeft(4, '0') + Convert.ToString(csm.Stream, 2).PadLeft(4, '0');
MsgHeader[2] = Convert.ToByte(strTemp, 2);
//Function
//strTemp = Convert.ToString(0, 2).PadLeft(4, '0') + Convert.ToString(csm.Function, 2).PadLeft(4, '0');
strTemp = Convert.ToString(csm.Function, 2).PadLeft(8, '0');
MsgHeader[3] = Convert.ToByte(strTemp, 2);
MsgHeader[4] = 0; //Ptype 0으로 고정
MsgHeader[5] = 0; //Stype Data Message의 경우 0으로 고정
sbTemp = BitConverter.GetBytes(csm.SystemByte);
if (BitConverter.IsLittleEndian)
Array.Reverse(sbTemp);
csm.dataBuffer.InsertRange(0, sbTemp);
csm.dataBuffer.InsertRange(0, MsgHeader);
byte[] sBuffer = new byte[csm.dataBuffer.Count];
csm.dataBuffer.CopyTo(0, sBuffer, 0, csm.dataBuffer.Count);
SendBuffer(sBuffer);
if (csm.Function % 2 != 0 && csm.Stream != 9) //홀수 라면
{
string Key = csm.Stream + "F" + csm.Function + "SB" + csm.SystemByte;
//clsT3Checker ctoc = new clsT3Checker(DateTime.Now, clsMSG_PARSE.ByteCombine(MsgHeader, sbTemp), MsgID, Key);
//ctoc.OnClosed += new ClosedDelegate(ctoc_OnClosed);
//clsDataManager.TimeOutChk.Add(Key, ctoc);
}
SetPcomLog(clsDataManager.MsgID_Log, ref sLog, false); //SECS MSG SEND 후 LOG Set
clsMSGLOG.SetLOG(MSGLOGTYPE.PCOM, sLog); //SECS MSG SEND 후 LOG Write
CloseSecsMsg(clsDataManager.MsgID_Log); //SECS MSG SEND 후 보낸 MSG MessageList에서 삭제
CloseSecsMsg(MsgID);
}
catch (Exception ex)
{
clsMSGLOG.SetLOG(MSGLOGTYPE.SYSTEMERROR, ex.ToString());
return -1;
}
return 0;
}
반응형