50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
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))
|
||
}
|
||
}
|