socketcan

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: BSD-3-Clause Imports: 7 Imported by: 1

README

socketcan_go

forked from https://github.com/atuleu/golang-socketcan

  1. add filter support
  2. only working on linux
  3. need cgo support

Install

  • go get github.com/yoursmengle/socketcan_go

Usage

package main

import (
	"fmt"
	"time"

	socketcan "github.com/yoursmengle/socketcan_go"
)

func main() {
	fmt.Println("can1 start...")
	sock_can, err := socketcan.NewCanItf("can1")  // can0 or can1
	if err != nil {
        fmt.Println("new can1 error:", err)
		return
	}
	defer sock_can.Close()

	// step1, set filter for can1
	err = sock_can.AddfilterPass(recv_ids1, uint32(len(recv_ids1)))
	if err != nil {
		return
	}

	//step2, create receive thread
	go can1_recv(sock_can)

	//step3, send data periodically 10ms
	ticker := time.NewTicker(10 * time.Millisecond)
	for {
		<-ticker.C // tick 10ms

        // fill can frame data to send
		frame, need_send := fill_can1_frame()
		if ! need_send {
			continue
		}

        // send can frame
		err := sock_can.Send(frame)
		if err != nil {
            fmt.Println("send error:", err)
		}
	}
}

// can1 receive thread
func can1_recv(sock_can *socketcan.RawInterface) {
	for {
		// waiting for receiving one can frame
		frame, err := sock_can.Receive()
		if err != nil {
            fmt.Println("recv error:", err)
            continue
		}

        // process can frame if received data
		proc_can1_frame(&frame)
	}
}

// receive filter
// can frame id list 
var recv_ids1 []uint32 = []uint32{
	0x0cf00400 | socketcan.CAN_EFF_FLAG,  // ext id 
    0x0cf00401 | socketcan.CAN_EFF_FLAG,  // ext id
	0x67b,     // std id
    0x67c,     // std id
}

func fill_can1_frame() (socketcan.CanFrame, bool) {
	frame_send := socketcan.CanFrame{}
	frame_send.ID = 0x1cf14717
	frame_send.Data = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
	frame_send.Dlc = 8
	frame_send.Extended = true

	return frame_send, true
}

func proc_can1_frame(pFrame *socketcan.CanFrame) {
	if pFrame.ID == recv_ids1[0]&0x7fffffff {
		rpm := (uint16(pFrame.Data[3])<<8 | uint16(pFrame.Data[2])) / 8
        fmt.Println("rpm:", rpm)
	} else if pFrame.ID == recv_ids1[1]&0x7fffffff {
		fmt.Println("proc_can1:", pFrame)
	}
}

Documentation

Index

Constants

View Source
const (
	CAN_EFF_MASK uint32 = unix.CAN_EFF_MASK
	CAN_SFF_MASK uint32 = unix.CAN_SFF_MASK
	CAN_RTR_FLAG uint32 = unix.CAN_RTR_FLAG
	CAN_EFF_FLAG uint32 = unix.CAN_EFF_FLAG
)
View Source
const (
	BAUD_1M   = 1000000
	BAUD_500K = 500000
	BAUD_250K = 250000
	BAUD_125K = 125000
	BAUD_100K = 100000
	BAUD_50K  = 50000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CanFrame

type CanFrame struct {
	ID       uint32
	Dlc      byte
	Data     []byte
	Extended bool
	RTR      bool
}

type RawInterface

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

func NewCanItf

func NewCanItf(ifName string) (*RawInterface, error)

func (*RawInterface) AddfilterPass

func (itf *RawInterface) AddfilterPass(recv_ids []uint32, len uint32) error

func (*RawInterface) Close

func (itf *RawInterface) Close() error

func (*RawInterface) Receive

func (itf *RawInterface) Receive() (*CanFrame, error)

func (*RawInterface) Send

func (itf *RawInterface) Send(f *CanFrame) error

func (*RawInterface) SetBaud

func (itf *RawInterface) SetBaud(baud uint32) error

func (*RawInterface) SetTxQueueLen

func (itf *RawInterface) SetTxQueueLen(size uint32) error

Jump to

Keyboard shortcuts

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