realidiot
介绍
江苏真源有源标签解析库 SDK
温度标签,盘点标签
软件架构
我的开发环境是 go 1.15
安装教程
go get -u -v gitee.com/aprilyu/realidiot
import "gitee.com/aprilyu/realidiot/iot"
使用说明
func udpTest(){
// 创建监听
socket, err := net.ListenUDP("udp4", &net.UDPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: 3721,
})
defer socket.Close()
if err != nil {
fmt.Println("监听失败!", err)
return
}
fmt.Println("开启监听:端口号:",3721 )
for {
// 读取数据
data := make([]byte, 100)
readLen, remoteAddr, err := socket.ReadFromUDP(data)
if err != nil {
fmt.Println("读取数据失败!", err)
time.Sleep(100*time.Millisecond)
continue
}
go handleData(remoteAddr,readLen,data)
//time.Sleep(100*time.Millisecond)
}
}
//解析udp数据,remote:udp数据发送者,readLen:本次数据长度,data:缓冲区数据
func handleData(remote *net.UDPAddr,readLen int,data []byte){
var dstData=make([]byte,readLen)
iohelper.BlockCopy(data,0,dstData,0,readLen)
var dataHex string=iohelper.GetHex(dstData)
//fmt.Println(read)
fmt.Println("来自ip:",remote," 的有效数据长度为:",readLen,dataHex)
iotInfo,err :=iot.ParseData(dstData)
if err!=nil{
fmt.Println("error:",err)
return
}
if iotInfo.Success{
jsonStr,_:=json.Marshal(&iotInfo)
if iotInfo.DataType==0{
fmt.Println("这网关的Ping信息")
}
fmt.Println( string(jsonStr))
}
}