| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package mcu_ctrl_board
- /*
- #include "mcu_ctrl_board.h"
- */
- import "C"
- import (
- "encoding/json"
- "fmt"
- "unsafe"
- )
- // 打开与MCU控制板的串口通讯
- func mcuCtrlBoard_ComInit() (int, error) {
- ret := int(C.MCU_ComInit())
- if ret != 0 {
- return ret, fmt.Errorf("an error occurred while calling the C.MCU_ComInit() function(%d)", ret)
- }
- return 0, nil
- }
- // 关闭与MCU控制板的串口通讯
- func mcuCtrlBoard_ComExit() (int, error) {
- ret := int(C.MCU_ComExit())
- if ret != 0 {
- return ret, fmt.Errorf("an error occurred while calling the C.MCU_ComExit() function(%d)", ret)
- }
- return 0, nil
- }
- // 向MCU控制板主动的写入数据
- func mcuCtrlBoard_Write(data []byte) (int, error) {
- var p *C.uchar
- if len(data) > 0 { // 避免访问空切片导致异常
- p = (*C.uchar)(unsafe.Pointer(&data[0]))
- }
- ret := int(C.MCU_SendRaw(p, C.int(len(data))))
- if ret != 0 {
- return ret, fmt.Errorf("an error occurred while calling the C.MCU_SendRaw() function(%d)", ret)
- }
- return ret, nil
- }
- // determine JSON msg type
- func detectJsonType(jsonStr string) string {
- var obj map[string]json.RawMessage
- if json.Unmarshal([]byte(jsonStr), &obj) != nil {
- return "invalid"
- }
- if string(obj["jsonrpc"]) == `"2.0"` {
- return "jsonrpc2"
- }
- return "jsondata"
- }
- //export go_handle_serial_json_info
- func go_handle_serial_json_info(jsonStr *C.char) *C.char {
- if Board == nil {
- return nil
- }
- s := C.GoString(jsonStr)
- t := detectJsonType(s)
- switch t {
- case "jsondata":
- return processJson(s)
- case "jsonrpc2":
- return processRPC2(s)
- default:
- return nil
- }
- }
- //export go_handle_serial_text_info
- func go_handle_serial_text_info(textStr *C.char) *C.char {
- if Board == nil {
- return nil
- }
- s := C.GoString(textStr)
- return processText(s)
- }
|