CANOpenAscii

package
v0.0.1-beta.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

README

1. CANOpenNode Go Ascii Client

CANopenLinux is a CANopen stack running on Linux devices.

It is based on CANopenNodeLinux

Start CANOpenLinux driver in tcp port 6543

canopend can0 -i 1 -c tcp-6543


establishes socket connection with canopend (CANopen Linux commander device). It sends standardized CANopen commands (CiA309-3) to gateway and prints the responses to stdout and stderr.

package main

func main()  {
	client := NewCANOpenClient("localhost", 6543, 5*time.Second)
	err := client.Connect()
	if err != nil {
		fmt.Println(err)
		return
	}

	if client.connected {
		// 准备多条指令
		commands := []Command{
			ReadSDO(1, 4, 0x1000, 0, u8, 200*time.Millisecond, 2),
			ReadSDO(1, 4, 0x1000, 0, u16, 200*time.Millisecond, 2), // 读取 SDO,超时 200ms,重试 2 次 // 读取 SDO,超时 200ms,重试 2 次
			ReadSDO(1, 4, 0x1000, 0, u32, 200*time.Millisecond, 2), // 读取 SDO,超时 200ms,重试 2 次 // 读取 SDO,超时 200ms,重试 2 次

			WriteSDO(2, 4, 0x6040, 0, "8", "u16", 300*time.Millisecond, 1), // 写入 SDO,超时 300ms,重试 1 次
			SetNMTState(3, 4, MNT_Start, 100*time.Millisecond, 0),          // 设置 NMT,超时 100ms,无重试
		}

		// 并发发送指令
		results := client.SendMultipleCommands(commands)

		// 打印结果
		for _, result := range results {
			if result.Error != nil {
				fmt.Printf("Command %d failed: %v\n", result.Command.CommandID, result.Error)
			} else {
				resp := result.Response
				if resp.IsError {
					fmt.Printf("Command %d error response: %s %s\n", result.Command.CommandID, resp.ErrorCode, resp.ErrorMsg)
				} else {
					fmt.Printf("Command %d success: %s\n", result.Command.CommandID, resp.Value)
				}
			}
		}

		// 断开连接
		client.Disconnect()
	}
}
    

Documentation

Index

Constants

View Source
const (
	MNT_Start     = "start"
	MNT_Stop      = "stop"
	MNT_Preop     = "preop"
	MNT_ResetNode = "reset node"
	MNT_ResetComm = "reset comm"
)
View Source
const (
	I8  = "I8"
	I16 = "i16"
	I32 = "i32"
	I64 = "i64"

	U8  = "U8"
	U16 = "U16"
	U32 = "U32"
	U64 = "U64"

	X8  = "x8"
	X16 = "x16"
	X32 = "x32"
	X64 = "x64"

	R32 = "r32"
	R64 = "r64"

	T  = "t"
	TD = "td"

	VS = "vs"

	OS = "os"
	US = "us"

	D = "d"

	HEX = "hex"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CANOpenClient

type CANOpenClient struct {
	// contains filtered or unexported fields
}

CANOpenClient 代表 CANOpen 客户端

func NewCANOpenClient

func NewCANOpenClient(host string, port int, timeout, retryInterval time.Duration) *CANOpenClient

NewCANOpenClient 创建新的 CANOpen 客户端

func (*CANOpenClient) Connect

func (c *CANOpenClient) Connect() error

Connect 连接到 CANOpen 网关

func (*CANOpenClient) Disconnect

func (c *CANOpenClient) Disconnect()

Disconnect 断开连接

func (*CANOpenClient) SendCommand

func (c *CANOpenClient) SendCommand(cmd Command) (CommandResult, error)

SendCommand 发送单条命令,支持重试

func (*CANOpenClient) SendMultipleCommands

func (c *CANOpenClient) SendMultipleCommands(commands []Command) []CommandResult

SendMultipleCommands 并发发送多条命令

type Command

type Command struct {
	CommandID  int // 唯一标识符
	NodeID     int
	Index      int
	SubIndex   int
	DataType   DataType
	Content    string        // 指令内容,如 "[1] r 1000 0"
	Timeout    time.Duration // 命令超时
	MaxRetries int           // 最大重试次数
}

Command 代表一条 CANOpen 指令

func ReadSDO

func ReadSDO(cmdID int, nodeID, index, subindex int, dataType DataType, timeout time.Duration, retries int) Command

ReadSDO 生成读取 SDO 的命令

func SetNMTState

func SetNMTState(cmdID int, nodeID int, state MNTOpt, timeout time.Duration, retries int) Command

SetNMTState 生成设置 NMT 状态的命令

func WriteSDO

func WriteSDO(cmdID int, nodeID, index, subindex int, value, dataType DataType, timeout time.Duration, retries int) Command

WriteSDO 生成写入 SDO 的命令

type CommandResult

type CommandResult struct {
	Command  Command
	Response Response
	Error    error
}

CommandResult 代表指令执行结果

type DataType

type DataType string

type MNTOpt

type MNTOpt string

type Response

type Response struct {
	CommandID string // 对应的命令 CommandID
	Value     string // 成功时的值(如 SDO 读取结果)
	IsError   bool   // 是否为错误响应
	ErrorCode string // 错误代码(如 "100")
	ErrorMsg  string // 错误消息(如 "Invalid parameter")
	Raw       string // 原始响应
}

Response 代表解析后的响应

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL