[Nanopb] Basic type proto sample - Encode sample code

Posted by [하늘이]
2020. 1. 30. 13:53 IT/C, C++
반응형

[Nanopb for Google protobuf] Basic type proto sample - Encode sample code


※ 관련글

  • Google Protobuf
  1. [Google Protobuf] For Java in Android Studio - 설치 및 proto 파일 컴파일
  2. [Google Protobuf] For Java in Android Studio - Gen Java 파일 Android Studio 적용
  3. [Google Protobuf] For Java in Android Studio - Basic example code 기본 소스 코드 사용 샘플
  4. [Google Protobuf] For C++ in Linux - Settings C++ 빌드 환경 만들기
  5. [Google Protobuf] For C++ in Linux - Basic example code 기본 소스 코드 사용 샘플
  • Nanopb Basic type value example
  1. [Nanopb for Google protobuf] Basic type proto sample
  2. [Nanopb for Google protobuf] Basic type proto sample - Sample proto file
  3. [Nanopb for Google protobuf] Basic type proto sample - Encode sample code
  4. [Nanopb for Google protobuf] Basic type proto sample - Decode sample code
  • Nanopb String / bytes value example
  1. [Nanopb for Google protobuf] String/Bytes type proto
  2. [Nanopb for Google protobuf] String/Bytes type proto - Sample proto file 
  3. [Nanopb for Google protobuf] String/Bytes type proto - Encode example code 1
  4. [Nanopb for Google protobuf] String/Bytes type proto - Encode example code 2
  5. [Nanopb for Google protobuf] String/Bytes type proto - Decode example code
  • Nanopb basic submsg example
  1. [Nanopb for Google protobuf] Sub message - Example proto file
  2. [Nanopb for Google protobuf] Sub message - Encode example code
  3. [Nanopb for Google protobuf] Sub message - Decode example code
  • Nanopb array submsg value example

  1. [Nanopb for Google protobuf] Array Sub message - Example proto file
  2. [Nanopb for Google protobuf] Array Sub message - Encode example code
  3. [Nanopb for Google protobuf] Array Sub message - Decode example code

Encode sample code

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void testBasicTypes(){
    int encodeSize = 0;
    // 메시지 생성 및 초기화
    TestBasicTypes msg = TestBasicTypes_init_default;
 
    // 메시지에 테스트 데이터 입력
    msg.testInt32 = 101;
    msg.testInt64 = 102102102;
    msg.testFloat = 1030.3;
    msg.testDouble = 1040.4;
    msg.testUint32 = 105;
 
    // Decode 시키기 위한 버퍼 및 stream 생성
    uint8_t buffer[255];
    pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
 
    // stream 과 메시지로 Encode 시킨다. 실패 시 false가 리턴된다. 성공 시 buffer에 데이터가 byte 배열로 들어간다.    
    if (pb_encode(&stream, TestBasicTypes_fields, &msg))
    {
        encodeSize = stream.bytes_written;
 
        Serial.print("TestBasicTypes_encode OK size:");
        Serial.print(encodeSize);
        Serial.println("");
 
        Serial.print("Basic types: ");
        // encode된 데이터 바이트 값 출력.
        for (int i = 0; i < encodeSize; i++) {
            Serial.print(buffer[i], 16);
            Serial.print(" ");
        }
        Serial.println("");
 
        // decode 시킨다.
        testDecode(encodeSize, buffer);
    }
    else
    {
        Serial.print("TestBasicTypes Encoding failed: ");
        Serial.print(PB_GET_ERROR(&stream));
        Serial.println("");
        return;
    }
}
cs


반응형