# SendMsg

### int SendMsg(int lMsgId)

사용자가 직접 생성하거나 구성한 SECS 메시지를 상대측(Host)으로 전송합니다. 장비에서 먼저 보내는 Primary 메시지뿐만 아니라, 호스트의 요청을 수신한 후 작성한 Reply 메시지를 보낼 때도 동일하게 이 함수를 사용합니다.

Parameters

<table data-header-hidden><thead><tr><th width="201" valign="top"></th><th width="130" valign="top"></th><th valign="top"></th></tr></thead><tbody><tr><td valign="top">Name</td><td valign="top">Type</td><td valign="top">Description</td></tr><tr><td valign="top">lMsgId</td><td valign="top">int</td><td valign="top">전송할 Message의 ID</td></tr></tbody></table>

다음은 S2F25W 를 사용자가 직접 처리하는 예시입니다.

```csharp
private void OnS2F25W(int lMsgId)
{
    try
    {
        // 1. 호스트가 보낸 Binary 아이템의 실제 개수(길이)를 먼저 확인합니다.
        // GetNextItemCount는 다음 읽을 아이템이 배열(Binary 등)인 경우 그 길이를 반환합니다.
        int nActualLength = m_gem.GetNextItemCount(lMsgId);

        if (nActualLength < 0)
        {
            Console.WriteLine("데이터 길이를 가져오는데 실패했습니다.");
            return;
        }

        // 2. 확인된 실제 길이에 딱 맞는 크기로 배열을 동적으로 생성합니다.
        // 이제 47바이트보다 많이 들어와도 안전하게 모두 수용할 수 있습니다.
        byte[] arrValue = new byte[nActualLength];

        // 3. 실제 데이터를 배열에 채웁니다.
        int nRet = m_gem.GetBinaryItem(lMsgId, arrValue);

        if (nRet >= 0)
        {
            Console.WriteLine($"데이터 수신 성공: {nActualLength} bytes 읽음.");

            // 4. 응답 메시지(S2F26) 생성
            int rMsgId = m_gem.CreateReplyMsg(lMsgId);

            if (rMsgId > 0)
            {
                // 5. 받은 길이와 동일한 데이터를 응답 메시지에 추가합니다.
                // 전송 시에도 arrValue의 크기만큼 정확하게 전송됩니다.
                m_gem.AddBinaryItem(rMsgId, arrValue);

                // 6. 메시지 전송
                m_gem.SendMsg(rMsgId);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("OnS2F25W() 오류: " + ex.Message);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nviasoft.gitbook.io/nviasoft-docs/api-reference/undefined-4/sendmsg.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
