# ENIG Protocol

ENIG 프로토콜 통신 라이브러리

**Explanation of the README Content:**

1.  **Title and Overview:** Clearly states the purpose of the library.
2.  **Key Components:** Explains the main building blocks:
    *   `DeviceType` enum: Lists the device types.
    *   `Packet` class: Describes the packet structure.
    *   `EEProtocol` class: Explains its role in packet handling.
3.  **Key Features:** Details the core functionalities of the `EEProtocol` class:
    *   Packet creation.
    *   Packet parsing.
    *   Data reception.
    *   Event-driven nature.
    *   CRC16 calculation.
4.  **Usage:** Provides code examples for:
    *   Creating a packet.
    *   Parsing a packet.
    *   Processing received data.
5.  **Error Handling:** Explains how the library handles invalid data and incomplete packets.
6.  **Dependencies:** Lists any required frameworks or libraries.
7.  **License:** Placeholder for the project's license.

This README provides a comprehensive overview of the `ENIGProtocol` library, making it easy for other developers to understand and use it. I hope this is helpful!


## 개발 환경
- Frameworks : .NET Standard 2.0
- Visual Studio 2022 또는 Visual Studio Code

## 프로젝트 구조

- `EEProtocol.cs`: ENIG 프로토콜 구현
- `EEProtocolTests.cs`: 단위 테스트 코드
- `Sample`: C# Winform 샘플 프로젝트

### Xbee Setting value

- **NARMI 70 : AGV No 70
- **NARMI 70 (LIFT) : P46A6,C17,DH:0,DL:FFFF,MY40
- **NARMI 70 (AGV) : P46A6,C17,DH:0,DL:FFFF,MY41

- **NARMI 77 : AGV No 71
- **NARMI 71 (LIFT) : P46A6,C17,DH:0,DL:FFFF,MY30
- **NARMI 71 (AGV) : P46A6,C17,DH:0,DL:FFFF,MY31

- **충전기04 : P46A6,C17,DH:0,DL:FFFF,MY41
- **충전기71 : P46A6,C17,DH:0,DL:FFFF,MY46

- **ACS : P46A5,C17,DH:0,DL:FFFF,MY10

- **BUFFER  : P46A5,C17,DH:0,DL:FFFF,MY60~65

- **AGV1 : P46A5,C17,DH:0,DL:FFFF,MY50
- **AGV2 : P46A5,C17,DH:0,DL:FFFF,MY51

- **DOOR : P46A5,C17,DH:0,DL:FFFF,MY30


## 장비 목록
```
public enum DeviceType : byte
{
    ACS = 0,
    AGV1 = 10+1,
    AGV2 = 10+2,
    BUFFER1 = 20+1,
    BUFFER2 = 20+2,
    BUFFER3 = 20+3,
    BUFFER4 = 20+4,
    BUFFER5 = 20+5,
    DOOR = 30,
}
```
### 기본 패킷 구조
```
[STX][LEN][ID][CMD][DATA][CRC16][ETX]
```
- **STX (Start of Text)**: 0x02
- **LEN (Length)**: 데이터 길이 (1바이트) = {ID+CMD+DATA}
- **ID (Client ID)**: 데이터 길이 (1바이트) : 디바이스식별코드(=DeviceType)
- **CMD (Command)**: 명령어 코드 (1바이트)
- **DATA**: 명령어에 따른 데이터 (가변 길이)
- **CRC16**: 데이터 무결성 검사 (2바이트)
- **ETX (End of Text)**: 0x03

### 통신 방향 (호스트=ACS, 장비=agv,buffer,door)
- H -> E: 호스트에서 장비로 전송 
- E -> H: 장비에서 호스트로 전송

### 명령어 목록
1. **ACS (AGV Control System)**
   

2. **Buffer**
   - E -> H | cmd('S'): 상태 (............) BIT & 1BYTE 0:CART1, 1:CART2, 2:BASKET1, 3:BASKET2, 4:OPEN, 5:CLOSE 
   - H -> E | cmd('L'): Lock
   -          Target[1] = {DeviceType}
   - H -> E | cmd('U'): UnLock
   -          Target[1] = {DeviceType}

3. **AGV**
   - H -> E | Move :  cmd(100) : 대상태그까지 이동(자동이동)
   -          Target[1] = {DeviceType}
   -          TagID[4] = "0000"
   - H -> E | Move :  cmd(107) : 대상별칭까지 이동(자동이동)
   -          Target[1] = {DeviceType}
   -          AliasName[n] = "....."
   - H -> E | Stop : cmd(101) : 멈춤
   - H -> E | Reset : cmd(102) : 오류 소거
   - H -> E | Charge On: cmd(103) : 충전실행(충전기 이동 후 자동 충전 진행)
   -          Target[1] = {DeviceType}
   -          Action[1] : 0=Charge Off, 1=Charge On   
   - H -> E | MoveManual : cmd(104) : 메뉴얼이동
   -          Target[1] = {DeviceType}
   -          Direction[1] : 0=Backward, 1=Forward, 2=TurnLeft, 3=TurnRight
   -          Speed[1] : 0=Slow, 1=Normal, 2=Fast
   -          Runtime[1] : 0 second
   - H -> E | MarkStop : cmd(105) : 마크센서스톱   
   -          Target[1] = {DeviceType}
   
   - H -> E | Lift Control : cmd(106) : 리프트제어
   -          Target[1] = {DeviceType}
   -          Action[1] : 0=STOP, 1=UP, 2=DOWN

   - E -> H | Move Complete : cmd(1)  : 목적지이동완료 후 전송
   -           TagID[4] : "0000" 
   - E -> H | TagID Received : cmd(2) : 태그값 인식시 전송
   -           TagID[4] : "0000"
   
   - E -> H |  Status : cmd(3)  - 총 12바이트
   -           Mode[1] : 0=manual, 1=auto
   -           RunSt[1] : 0=stop, 1=run, 2=error
               MotDirection[1] : 0:Forward, 1:Backward, 0xFF:unknown
   -           MagDiection[1] : 0=straight, 1=left, 2=right  , 0xFF:unknown
   -           Inposition[1] : 0=off, 1=on : 목적위치에 도달완료 시 설정 이동 이동시 OFF됨
   -           ChargeSt[1] : 0=off, 1=on
   -           CartSt[1] : 0=off, 1=on, 2=unknown
   -           LiftSt[1] : 0=down , 1=up, 2=unknown
   -           LastTag[4] : "0000" (ASCII 4바이트)

4. **Door**
   - H -> E | cmd(1): 출입문 열기
   - H -> E | cmd(2): 출입문 닫기
   - E -> H | cmd(3): 출입문 상태 (data len=1  : 0=닫힘, 1=열림,  2=바쁨, 3=알수없음, 255=오류)

### CRC16 계산
- CRC16 다항식 사용
- 초기값: 0xFFFF
- 데이터 무결성 검증에 사용


#### CRC-16 테이블 값
```
0x0000, 0x408E, 0x73EF, 0x3361, 0x152D, 0x55A3, 0x66C2, 0x264C, 
0x2A5A, 0x6AD4, 0x59B5, 0x193B, 0x3F77, 0x7FF9, 0x4C98, 0x0C16, 
0x54B4, 0x143A, 0x275B, 0x67D5, 0x4199, 0x0117, 0x3276, 0x72F8, 
0x7EEE, 0x3E60, 0x0D01, 0x4D8F, 0x6BC3, 0x2B4D, 0x182C, 0x58A2, 
0x5B9B, 0x1B15, 0x2874, 0x68FA, 0x4EB6, 0x0E38, 0x3D59, 0x7DD7, 
0x71C1, 0x314F, 0x022E, 0x42A0, 0x64EC, 0x2462, 0x1703, 0x578D, 
0x0F2F, 0x4FA1, 0x7CC0, 0x3C4E, 0x1A02, 0x5A8C, 0x69ED, 0x2963, 
0x2575, 0x65FB, 0x569A, 0x1614, 0x3058, 0x70D6, 0x43B7, 0x0339, 
0x45C5, 0x054B, 0x362A, 0x76A4, 0x50E8, 0x1066, 0x2307, 0x6389, 
0x6F9F, 0x2F11, 0x1C70, 0x5CFE, 0x7AB2, 0x3A3C, 0x095D, 0x49D3, 
0x1171, 0x51FF, 0x629E, 0x2210, 0x045C, 0x44D2, 0x77B3, 0x373D, 
0x3B2B, 0x7BA5, 0x48C4, 0x084A, 0x2E06, 0x6E88, 0x5DE9, 0x1D67, 
0x1E5E, 0x5ED0, 0x6DB1, 0x2D3F, 0x0B73, 0x4BFD, 0x789C, 0x3812, 
0x3404, 0x748A, 0x47EB, 0x0765, 0x2129, 0x61A7, 0x52C6, 0x1248, 
0x4AEA, 0x0A64, 0x3905, 0x798B, 0x5FC7, 0x1F49, 0x2C28, 0x6CA6, 
0x60B0, 0x203E, 0x135F, 0x53D1, 0x759D, 0x3513, 0x0672, 0x46FC, 
0x7979, 0x39F7, 0x0A96, 0x4A18, 0x6C54, 0x2CDA, 0x1FBB, 0x5F35, 
0x5323, 0x13AD, 0x20CC, 0x6042, 0x460E, 0x0680, 0x35E1, 0x756F, 
0x2DCD, 0x6D43, 0x5E22, 0x1EAC, 0x38E0, 0x786E, 0x4B0F, 0x0B81, 
0x0797, 0x4719, 0x7478, 0x34F6, 0x12BA, 0x5234, 0x6155, 0x21DB, 
0x22E2, 0x626C, 0x510D, 0x1183, 0x37CF, 0x7741, 0x4420, 0x04AE, 
0x08B8, 0x4836, 0x7B57, 0x3BD9, 0x1D95, 0x5D1B, 0x6E7A, 0x2EF4, 
0x7656, 0x36D8, 0x05B9, 0x4537, 0x637B, 0x23F5, 0x1094, 0x501A, 
0x5C0C, 0x1C82, 0x2FE3, 0x6F6D, 0x4921, 0x09AF, 0x3ACE, 0x7A40, 
0x3CBC, 0x7C32, 0x4F53, 0x0FDD, 0x2991, 0x691F, 0x5A7E, 0x1AF0, 
0x16E6, 0x5668, 0x6509, 0x2587, 0x03CB, 0x4345, 0x7024, 0x30AA, 
0x6808, 0x2886, 0x1BE7, 0x5B69, 0x7D25, 0x3DAB, 0x0ECA, 0x4E44, 
0x4252, 0x02DC, 0x31BD, 0x7133, 0x577F, 0x17F1, 0x2490, 0x641E, 
0x6727, 0x27A9, 0x14C8, 0x5446, 0x720A, 0x3284, 0x01E5, 0x416B, 
0x4D7D, 0x0DF3, 0x3E92, 0x7E1C, 0x5850, 0x18DE, 0x2BBF, 0x6B31, 
0x3393, 0x731D, 0x407C, 0x00F2, 0x26BE, 0x6630, 0x5551, 0x15DF, 
0x19C9, 0x5947, 0x6A26, 0x2AA8, 0x0CE4, 0x4C6A, 0x7F0B, 0x3F85, 
```


#### CRC16 계산 테이블 생성 코드
```csharp
const ushort polynomial = 0x7979;
ushort[] CRC16_TABLE = new ushort[256];

for (ushort i = 0; i < CRC16_TABLE.Length; i++)
{
    ushort value = 0;
    ushort temp = i;
    for (byte j = 0; j < 8; j++)
    {
        if (((value ^ temp) & 0x0001) != 0)
        {
            value = (ushort)((value >> 1) ^ polynomial);
        }
        else
        {
            value >>= 1;
        }
        temp >>= 1;
    }
    CRC16_TABLE[i] = value;
}
```

#### CRC16 계산 예시
```csharp
ushort CalculateCRC16(byte[] data)
{
    ushort crc = 0xFFFF;
    for (int i = 0; i < data.Length; i++)
    {
        byte index = (byte)(crc ^ data[i]);
        crc = (ushort)((crc >> 8) ^ CRC16_TABLE[index]);
    }
    return crc;
}
```

## 라이센스





