123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package main
- /*
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LEN 1024
- // 设备->平台
- void T(const char *t, const char *b, char **r_b) {
- size_t len1 = strlen(t);
- size_t len2 = strlen(b);
- *r_b = (char *)malloc(len1 + len2 + 1);
- strcpy(*r_b, t);
- strcat(*r_b, b);
- }
- void R(const char *t, const char *b, char **r_t, char **r_b) {
- size_t len1 = strlen(t);
- size_t len2 = strlen(b);
- *r_t = (char *)malloc(len1 + 1);
- *r_b = (char *)malloc(len2 + 1);
- strcpy(*r_t, t);
- strcpy(*r_b, b);
- }
- */
- import "C"
- /////-------
- import (
- "encoding/hex"
- "fmt"
- "strings"
- "unsafe"
- )
- /*
- 设备->平台
- */
- func T(t string, b []byte) string {
- var r_b *C.char
- C.T(C.CString(t), C.CString(string(b)), &r_b)
- r_b_ := C.GoString(r_b)
- C.free(unsafe.Pointer(r_b)) // Free the memory allocated in C code
- return r_b_
- }
- /*
- 平台->设备
- */
- func R(sn string, b string) (string, []byte) {
- var r_t, r_b *C.char
- C.R(C.CString(sn), C.CString(b), &r_t, &r_b)
- r_t_ := C.GoString(r_t)
- r_b_ := C.GoString(r_b)
- C.free(unsafe.Pointer(r_t)) // Free the memory allocated in C code
- C.free(unsafe.Pointer(r_b)) // Free the memory allocated in C code
- return r_t_, []byte(r_b_)
- }
- // ---DATA---
- var Data_hex = false
- var Topic = "/topic/snxxx1"
- var T_string = "A456"
- var R_string = "495051"
- // ///-------
- func main() {
- // -------------------------------设备->平台
- T_byte := []byte(T_string)
- if Data_hex {
- T_byte, _ = hex.DecodeString(T_string)
- }
- // 设备->平台(发送)
- if len(T_string) != 0 {
- data_ := T(Topic, T_byte)
- fmt.Print("|-=&" + data_)
- } else {
- fmt.Print("|-=&" + "无参数")
- }
- // 平台->设备(接收):
- if len(R_string) != 0 {
- topic_, data_ := R("{$sn}", R_string)
- fmt.Print("|-=&" + topic_)
- if Data_hex {
- fmt.Print("|-=&" + strings.ToUpper(hex.EncodeToString(data_)))
- } else {
- fmt.Print("|-=&" + string(data_))
- }
- } else {
- fmt.Print("|-=&" + "无参数")
- fmt.Print("|-=&" + "无参数")
- }
- // 返回数据: 1、 设备发送数据解析到平台json 2、topic 3、平台处理后返回给设备数据
- }
|