rts-sim-testing-service/third_party/can_btm/balise_btm.go
2023-11-21 18:13:50 +08:00

50 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package can_btm
import (
"fmt"
"joylink.club/bj-rtsts-server/third_party/message"
"joylink.club/bj-rtsts-server/third_party/udp"
"log/slog"
)
// btm与canet(网口-CAN口转换器)
type btmCanetClient struct {
//udp
udpServer udp.UdpServer
//udp
udpClient udp.UdpClient
localUdpPort int
remoteUdpPort int
remoteIp string
}
func (s *btmCanetClient) Start() {
//
s.udpServer = udp.NewServer(fmt.Sprintf(":%d", s.localUdpPort), s.handleCannetFrames)
s.udpServer.Listen()
//
s.udpClient = udp.NewClient(fmt.Sprintf("%s:%d", s.remoteIp, s.remoteUdpPort))
}
func (s *btmCanetClient) Stop() {
if s.udpServer != nil {
s.udpServer.Close()
}
if s.udpClient != nil {
s.udpClient.Close()
}
}
func (s *btmCanetClient) handleCannetFrames(cfs []byte) {
//一个cannet 帧 13字节
if len(cfs) > 0 && len(cfs)%13 == 0 {
cfSum := len(cfs) / 13
for cfi := 0; cfi < cfSum; cfi++ {
cfStart := cfi * 13
cf := message.NewCanetFrame(cfs[cfStart : cfStart+13])
fmt.Println(cf.String())
}
} else {
slog.Warn("从cannet接收数据未满足条件len(cfs) > 0 && len(cfs)%13 == 0", "len(cfs)", len(cfs))
}
}