4 Star 0 Fork 0

周心桐/1216实习小组

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tcp&websocket01.go 24.38 KB
一键复制 编辑 原始数据 按行查看 历史
于平 提交于 2024-07-11 17:21 . 最新
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
package main
import (
"bufio"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/websocket"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
//"math"
//"bytes"
)
//const (
//heartbeatInterval = 5 * time.Second // 心跳包发送间隔
//heartbeatTimeout = 30 * time.Second // 心跳包超时时间
//)
var tcpChan = make(chan string, 128)
var myMap = make(map[string]int)
func main() {
go startWebsocketServer()
// 监听端口
ln, err := net.Listen("tcp", "10.16.31.149:8000")
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
defer ln.Close()
fmt.Println("Listening on :8000")
for {
// 接受连接
conn, err := ln.Accept()
if err != nil {
fmt.Println("Error accepting:", err.Error())
continue
}
fmt.Println("Connection established")
// 处理连接
go handleConnection(conn)
}
}
var (
upgrader = websocket.Upgrader{
// 读取存储空间大小
ReadBufferSize: 1024,
// 写入存储空间大小
WriteBufferSize: 1024,
// 允许跨域
CheckOrigin: func(r *http.Request) bool {
return true
},
}
)
func wsHandler(w http.ResponseWriter, r *http.Request) {
// 完成握手 升级为 WebSocket长连接,使用conn发送和接收消息。
con, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("upgrade:", err)
return
}
defer con.Close()
//调用连接的WriteMessage和ReadMessage方法以一片字节发送和接收消息。实现如何回显消息:
//p是一个[]字节,messageType是一个值为websocket.BinaryMessage或websocket.TextMessage的int。
for {
if myMap!=nil{
jsonData, err := json.Marshal(myMap)
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return
}
for key, value := range myMap {
fmt.Printf("Key: %s, Value: %d\n", key, value)
}
err = con.WriteMessage(websocket.TextMessage, jsonData)
if err != nil {
fmt.Println("Error writing message:", err)
return
}}
messageType, msg, err := con.ReadMessage()
if err != nil {
log.Println("Reading error...", err)
return
}
log.Printf("Read from client msg:%s \n", msg)
if err := con.WriteMessage(messageType, msg); err != nil {
log.Println("Writeing error...", err)
return
}
}
}
func startWebsocketServer() {
http.HandleFunc("/", wsHandler) // ws://127.0.0.1:8880/rom
// 监听 地址 端口
err := http.ListenAndServe(":8880", nil)
if err != nil {
log.Fatal("ListenAndServe", err.Error())
}
}
func HexToDecimalWithTwoDecimalPlaces(hexStr string) (string, error) {
// 将十六进制字符串转换为字节数组
bytes, err := hex.DecodeString(hexStr)
if err != nil {
return "", fmt.Errorf("error decoding hex string: %v", err)
}
// 确保字节数组的长度为4字节
if len(bytes) != 4 {
return "", fmt.Errorf("expected 4 bytes, got %d bytes", len(bytes))
}
// 将字节数组转换为32位有符号整数
var intValue int32
for i := 0; i < 4; i++ {
intValue = intValue<<8 + int32(bytes[i])
}
// 将整数转换为浮点数,并取后两位为小数
floatValue := float64(intValue) / 100.0
// 将浮点数格式化为精度为两位的小数
formattedValue := fmt.Sprintf("%.2f", floatValue)
return formattedValue, nil
}
func HexToDecimalWithTwoDecimalPlaces1(hexStr string) (string, error) {
// 将十六进制字符串转换为字节数组
bytes, err := hex.DecodeString(hexStr)
if err != nil {
return "", fmt.Errorf("error decoding hex string: %v", err)
}
// 确保字节数组的长度为4字节
if len(bytes) != 4 {
return "", fmt.Errorf("expected 4 bytes, got %d bytes", len(bytes))
}
// 将字节数组转换为32位无符号整数
var intValue uint32
for i := 0; i < 4; i++ {
intValue = intValue<<8 + uint32(bytes[i])
}
// 将整数转换为浮点数,并取后两位为小数
floatValue := float64(intValue) / 100.0
// 将浮点数格式化为精度为两位的小数
formattedValue := fmt.Sprintf("%.2f", floatValue)
return formattedValue, nil
}
func HexToDecimalWithTwoDecimalPlaces2(hexStr string) (string, error) {
// 将十六进制字符串转换为字节数组
bytes, err := hex.DecodeString(hexStr)
if err != nil {
return "", fmt.Errorf("error decoding hex string: %v", err)
}
// 确保字节数组的长度为4字节
if len(bytes) != 4 {
return "", fmt.Errorf("expected 4 bytes, got %d bytes", len(bytes))
}
// 将字节数组转换为32位无符号整数
var intValue uint32
for i := 0; i < 4; i++ {
intValue = intValue<<8 + uint32(bytes[i])
}
// 将整数转换为浮点数,并取后两位为小数
floatValue := float64(intValue) / 1000000.0
// 将浮点数格式化为精度为两位的小数
formattedValue := fmt.Sprintf("%.6f", floatValue)
return formattedValue, nil
}
// HexToPrefixedHex 将无符号十六进制字符串转换为带有前缀0x的十六进制字符串
//func HexToPrefixedHex(hexStr string) (string, error) {
// 将十六进制字符串转换为无符号整数
//decimalValue, err := strconv.ParseUint(hexStr, 16, 64)
//if err != nil {
// return "", fmt.Errorf("error converting hex to decimal: %v", err)
//}
// 将无符号整数转换为带有前缀0x的十六进制字符串
//hexWithPrefix := fmt.Sprintf("0x%X", decimalValue)
//return hexWithPrefix, nil
//}
func BytesToHexWithPrefix(data []byte) []string {
hexBytes := make([]string, len(data))
for i, b := range data {
hexBytes[i] = fmt.Sprintf("0x%02X", b)
}
return hexBytes
}
func BytesToHex(byteArray []byte) string {
// 使用strings.Builder来构建最终的十六进制字符串
var hexBuilder strings.Builder
// 遍历字节数组,将每个字节转换为十六进制
for _, b := range byteArray {
// 使用strconv.Itoa将字节转换为十进制字符串
// 然后使用strings.Builder的WriteString方法追加格式化后的十六进制字符串
hexBuilder.WriteString(fmt.Sprintf("%02X", b))
}
// 移除最后一个空格并返回结果字符串
return strings.TrimSpace(hexBuilder.String())
}
func hexToTime(hexStr string) (time.Time, error) {
// 将输入字符串按空格分割成四个十六进制数
hexBytes := strings.Fields(hexStr)
// 确保有四个十六进制数
if len(hexBytes) != 4 {
return time.Time{}, fmt.Errorf("input string must contain exactly four hex numbers")
}
// 将四个十六进制数转换为一个整数
var timestamp uint32
for i, hexByte := range hexBytes {
// 去除"0x"前缀
hexByte = strings.TrimPrefix(hexByte, "0x")
// 将十六进制字符串转换为整数
value, err := strconv.ParseUint(hexByte, 16, 32)
if err != nil {
return time.Time{}, err
}
// 根据字节顺序组合整数,注意类型转换为uint32
timestamp |= uint32(value) << ((3 - i) * 8)
}
// 将32位整数转换为时间(假设这是自Unix纪元以来的秒数)
return time.Unix(int64(timestamp), 0), nil
}
func CombineHexWithPrefixPerByte(hexStrings []string) (string, error) {
// 新的字符串数组,用于存储去除前缀后的十六进制数
var hexParts []string
for _, hex := range hexStrings {
// 去除"0x"前缀,只保留十六进制部分
hexPart := strings.TrimPrefix(hex, "0x")
hexParts = append(hexParts, hexPart)
}
// 检查所有部分的组合是否是有效的十六进制数
combinedHex := strings.Join(hexParts, "")
_, err := strconv.ParseInt(combinedHex, 16, 64)
if err != nil {
return "", fmt.Errorf("invalid combined hex string: %s", combinedHex)
}
// 构建最终的字符串,每个字节前都有"0x"前缀,并用空格隔开
finalString := ""
for _, part := range hexParts {
finalString += fmt.Sprintf("0x%s ", part)
}
// 移除字符串末尾的空格
finalString = strings.TrimSpace(finalString)
return finalString, nil
}
func handleConnection(conn net.Conn) {
defer conn.Close()
// 创建一个心跳包超时的计时器
//heartbeatTimer := time.NewTimer(heartbeatTimeout)
//go func() {
//for {
//select {
//case <-heartbeatTimer.C:
// 心跳包超时
//fmt.Println("Heartbeat timeout, closing connection")
//conn.Close()
//return
//}
//}
//}()
// 数据库连接信息
dsn := "root:zhou6706@tcp(10.16.31.149:3306)/"
// 连接到MySQL服务器
db, err := sql.Open("mysql", dsn)
if err != nil {
log.Fatal(err)
}
// 选择数据库
_, err = db.Exec("USE mydb")
if err != nil {
log.Fatal(err)
}
//createTableSQL := `
//CREATE TABLE IF NOT EXISTS dvinf (
// SIM卡号 VARCHAR(255),
// 群塔ID INT(255),
// 塔ID INT(255),
// 吊绳倍率 INT(255),
// 塔机X坐标 DECIMAL(10, 2),
// 塔机Y坐标 DECIMAL(10, 2),
// 塔机前臂长度 DECIMAL(10, 2),
// 塔机后臂长度 DECIMAL(10, 2),
// 塔帽高度 DECIMAL(10, 2),
// 塔身高度 DECIMAL(10, 2),
// 安装经度 DECIMAL(10, 6),
// 安装纬度 DECIMAL(10, 6),
// 左转最大角度 DECIMAL(10, 2),
// 右转最大角度 DECIMAL(10, 2),
// 小车前行最远点到塔臂前端距离 DECIMAL(10, 2),
// 小车回收最近到塔臂前端距离 DECIMAL(10, 2),
// 钩起升的最高点离塔臂的距离 DECIMAL(10, 2),
// 塔机支持的最大起重量 DECIMAL(10, 2),
// 力矩限位最大值 DECIMAL(10, 2),
// 预警百分比 INT(255),
// 报警百分比 INT(255),
// 违章百分比 INT(255),
// 校验和 VARCHAR(255),
// PRIMARY KEY (SIM卡号)
//);`
//_, err = db.Exec(createTableSQL)
//if err != nil {
// log.Fatal(err)
//}
//
//createTableSQL = `
//CREATE TABLE IF NOT EXISTS dzdata1(
// 上传时间戳 VARCHAR(255),
// 吊装开始时间戳 VARCHAR(255),
// 吊装结束时间戳 VARCHAR(255),
// 最大载重 DECIMAL(10, 2),
// 最大载重百分比 INT(255),
// 最大力矩 DECIMAL(10, 2),
// 最大力矩百分比 INT(255),
// 开始转角 DECIMAL(10, 2),
// 结束转角 DECIMAL(10, 2),
// 开始幅度 DECIMAL(10, 2),
// 结束幅度 DECIMAL(10, 2),
// 最大工作幅度 DECIMAL(10, 2),
// 最小工作幅度 DECIMAL(10, 2),
// 开始起升高度 DECIMAL(10, 2),
// 结束起升高度 DECIMAL(10, 2),
// 最大起升高度 DECIMAL(10, 2),
// 最小起升高度 DECIMAL(10, 2),
// 最大风速 DECIMAL(10, 2),
// 最大倾角X DECIMAL(10, 2),
// 最大倾角Y DECIMAL(10, 2),
// 设备状态 VARCHAR(255),
// 校验和 VARCHAR(255),
// PRIMARY KEY (上传时间戳 )
//);`
//_, err = db.Exec(createTableSQL)
//if err != nil {
// log.Fatal(err)
//}
//
//createTableSQL = `
//CREATE TABLE IF NOT EXISTS rtdata1(
// 上传时间戳 VARCHAR(255),
// 高度 DECIMAL(10, 2),
// 幅度 DECIMAL(10, 2),
// 转角 DECIMAL(10, 2),
// 载重 DECIMAL(10, 2),
// 安全载重 DECIMAL(10, 2),
// 安全载重百分比 INT(255),
// 力矩 DECIMAL(10, 2),
// 力矩载重百分比 INT(255),
// 当前风速 DECIMAL(10, 2),
// 倾斜角度X DECIMAL(10, 2),
// 倾斜角度Y DECIMAL(10, 2),
// 设备状态 DECIMAL(10, 2),
// 下降深度 DECIMAL(10, 2),
// 校验和 VARCHAR(255),
// PRIMARY KEY (上传时间戳 )
//);`
//_, err = db.Exec(createTableSQL)
//if err != nil {
// log.Fatal(err)
//}
//
//createTableSQL = `
//CREATE TABLE IF NOT EXISTS regist(
// 上传时间戳 VARCHAR(255),
// PRIMARY KEY (上传时间戳 )
//);`
//_, err = db.Exec(createTableSQL)
//if err != nil {
// log.Fatal(err)
//}
for {
// 从客户端读取数据
// buffer := make([]byte, 1024)
// n, err := conn.Read(buffer)
// if err != nil {
// log.Println("读取数据失败:", err)
// break
// }
// // 输出从客户端接收到的数据
// fmt.Printf("收到客户端数据: %s\n", buffer[5:n])
// 状态机状态
state := 0x00
// 数据包长度
length := uint16(0)
// 数据包
var recvBuffer []byte
// 游标
cursor := uint16(0)
reader := bufio.NewReader(conn)
for {
v, err := reader.ReadByte()
// var hexString strings.Builder
// hexString.WriteString(fmt.Sprintf("%02x", v1))
// hexstr :=hexString.String()
// v:=[]byte(hexstr)
//byteArray, err := hex.DecodeString(hexstr)
if err != nil {
fmt.Println("Error decoding hex string:", err)
return
}
if err != nil {
_ = conn.Close()
return
}
switch state {
case 0x00: // 帧头 2bytes 0xA55A
if v == 0xa5 {
state = 0x01
//初始化
recvBuffer = nil
cursor = 0
} else {
state = 0x00
}
case 0x01:
if v == 0x5a {
state = 0x02
} else {
state = 0x00
}
case 0x02: // 帧长度 1byte
if v >= 0x0f {
length = uint16(v)
// 一次申请缓存,准备读数据
recvBuffer = make([]byte, length)
recvBuffer[0] = 0xA5
recvBuffer[1] = 0x5A
recvBuffer[2] = v
state = 0x03
} //else {
// state = errState(ip, "长度检测", "帧长度错误,丢弃数据", []byte{v})
// }
case 0x03: // 厂家编号 1byte
recvBuffer[3] = v
state = 0x04
case 0x04: // 协议版本 1byte
recvBuffer[4] = v
state = 0x05
case 0x05: // 帧类型 1byte
recvBuffer[5] = v
state = 0x06
case 0x06: // 设备编号 4bytes
recvBuffer[6+cursor] = v
cursor++
if cursor == 4 {
// 恢复游标,待读取数据备用
cursor = 0
// 如果数据段为空则跳过
if (length - 15) != 0 {
state = 0x07
} else {
state = 0x08
}
}
case 0x07: // 信息段 Nbytes
recvBuffer[10+cursor] = v
//fmt.Print("intVar: ", v, "\n")
cursor++
if cursor == (length - 15) {
state = 0x08
}
case 0x08: // 校验和 1byte
recvBuffer[length-5] = v
state = 0x09
case 0x09: // 帧尾 4bytes 0xCC33C33C
recvBuffer[length-4] = 0xCC
recvBuffer[length-3] = 0x33
recvBuffer[length-2] = 0xC3
recvBuffer[length-1] = 0x3C
if recvBuffer[2] == 103 {
a1 := BytesToHex(recvBuffer[10:30])
myMap["a1"], _ = strconv.Atoi(a1)
a2, err := strconv.ParseInt(BytesToHex(recvBuffer[30:32]), 16, 64) //int
myMap["a2"] = int(a2)
a3, err := strconv.ParseInt(BytesToHex(recvBuffer[32:34]), 16, 64) //int
myMap["a3"] = int(a3)
a4, err := strconv.ParseInt(BytesToHex(recvBuffer[34:35]), 16, 64) //int
myMap["a4"] = int(a4)
a5, err := HexToDecimalWithTwoDecimalPlaces(BytesToHex(recvBuffer[35:39])) //float
myMap["a5"], _ = strconv.Atoi(a5)
a6, err := HexToDecimalWithTwoDecimalPlaces(BytesToHex(recvBuffer[39:43])) //float
myMap["a6"], _ = strconv.Atoi(a6)
a7, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[43:47])) //float
myMap["a7"], _ = strconv.Atoi(a7)
a8, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[47:51])) //float
myMap["a8"], _ = strconv.Atoi(a8)
a9, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[51:55])) //float
myMap["a9"], _ = strconv.Atoi(a9)
a10, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[55:59])) //float
myMap["a10"], _ = strconv.Atoi(a10)
a11, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[59:63])) //float
myMap["a11"], _ = strconv.Atoi(a11)
a12, err := HexToDecimalWithTwoDecimalPlaces2(BytesToHex(recvBuffer[63:67])) //float
myMap["a12"], _ = strconv.Atoi(a12)
a13, err := HexToDecimalWithTwoDecimalPlaces2(BytesToHex(recvBuffer[67:71])) //float
myMap["a13"], _ = strconv.Atoi(a13)
a14, err := HexToDecimalWithTwoDecimalPlaces(BytesToHex(recvBuffer[71:75])) //float
myMap["a14"], _ = strconv.Atoi(a14)
a15, err := HexToDecimalWithTwoDecimalPlaces(BytesToHex(recvBuffer[75:79])) //float
myMap["a15"], _ = strconv.Atoi(a15)
a16, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[79:83])) //float
myMap["a16"], _ = strconv.Atoi(a16)
a17, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[83:87])) //float
myMap["a17"], _ = strconv.Atoi(a17)
a18, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[87:91])) //float
myMap["a18"], _ = strconv.Atoi(a18)
a19, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[91:95])) //float
myMap["a19"], _ = strconv.Atoi(a19)
a20, err := strconv.ParseInt(BytesToHex(recvBuffer[95:96]), 16, 64) //int
myMap["a20"] = int(a20)
a21, err := strconv.ParseInt(BytesToHex(recvBuffer[96:97]), 16, 64) //int
myMap["a21"] = int(a21)
a22, err := strconv.ParseInt(BytesToHex(recvBuffer[97:98]), 16, 64) //int
myMap["a22"] = int(a22)
//a23,err := strconv.ParseInt(recvBuffer(buffer[:]), 16, 64)
//a24,err := string(recvBuffer[:])
// a25,err := strconv.ParseInt(string(buffer[:]), 16, 64)
// a26,err := string(recvBuffer[:])
a23 := BytesToHex(recvBuffer[98:99])
myMap["a13"], _ = strconv.Atoi(a13)
fmt.Print("intVar: ", a7, "\n")
if err != nil {
log.Fatal(err)
}
_, err1 := db.Exec("insert into dvinf(SIM卡号,群塔ID,塔ID,吊绳倍率,塔机X坐标,塔机Y坐标,塔机前臂长度,塔机后臂长度,塔帽高度,塔身高度,安装经度,安装纬度,左转最大角度,右转最大角度,小车前行最远点到塔臂前端距离,小车回收最近到塔臂前端距离,钩起升的最高点离塔臂的距离,塔机支持的最大起重量,力矩限位最大值,预警百分比,报警百分比,违章百分比,校验和)values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23)
if err1 != nil {
log.Fatal(err1)
}
recvBuffer[5] = 0x03
recvBuffer[10] = recvBuffer[98]
recvBuffer[11] = recvBuffer[99]
recvBuffer[12] = recvBuffer[100]
recvBuffer[13] = recvBuffer[101]
recvBuffer[14] = recvBuffer[102]
// 将接收到的数据发送回客户端
_, err = conn.Write(recvBuffer[:15])
if err != nil {
log.Println("发送数据失败:", err)
break
}
}
if recvBuffer[2] == 65 {
b0 := BytesToHexWithPrefix(recvBuffer[10:14])
b01, err := CombineHexWithPrefixPerByte(b0)
b1, err := hexToTime(b01)
myMap["b1"] = int(b1.UnixMilli())
b2, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[14:18])) //float
myMap["b2"], _ = strconv.Atoi(b2)
b3, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[18:22])) //float
myMap["b3"], _ = strconv.Atoi(b3)
b4, err := HexToDecimalWithTwoDecimalPlaces(BytesToHex(recvBuffer[22:26])) //float
myMap["b4"], _ = strconv.Atoi(b4)
b5, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[26:30])) //float
myMap["b5"], _ = strconv.Atoi(b5)
b6, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[30:34])) //float
myMap["b6"], _ = strconv.Atoi(b6)
b7, err := strconv.ParseInt(BytesToHex(recvBuffer[34:35]), 16, 64) //int
myMap["b7"] = int(b7)
b8, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[35:39])) //float
myMap["b8"], _ = strconv.Atoi(b8)
b9, err := strconv.ParseInt(BytesToHex(recvBuffer[39:40]), 16, 64) //int
myMap["b9"] = int(b9)
b10, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[40:44])) //float
myMap["b10"], _ = strconv.Atoi(b10)
b11, err := HexToDecimalWithTwoDecimalPlaces(BytesToHex(recvBuffer[44:48])) //float
myMap["b11"], _ = strconv.Atoi(b11)
b12, err := HexToDecimalWithTwoDecimalPlaces(BytesToHex(recvBuffer[48:52])) //float
myMap["b12"], _ = strconv.Atoi(b12)
b13, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[52:56])) //float
myMap["b13"], _ = strconv.Atoi(b13)
b14, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[56:60])) //float
myMap["b14"], _ = strconv.Atoi(b14)
b15 := BytesToHex(recvBuffer[60:61])
myMap["b15"], _ = strconv.Atoi(b15)
if err != nil {
log.Fatal(err)
}
_, err2 := db.Exec("insert into rtdata1(上传时间戳,高度,幅度,转角,载重,安全载重,安全载重百分比,力矩,力矩载重百分比,当前风速,倾斜角度X,倾斜角度Y,设备状态,下降深度,校验和)values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15)
if err2 != nil {
log.Fatal(err2)
}
recvBuffer[5] = 0x07
recvBuffer[10] = recvBuffer[60]
recvBuffer[11] = recvBuffer[61]
recvBuffer[12] = recvBuffer[62]
recvBuffer[13] = recvBuffer[63]
recvBuffer[14] = recvBuffer[64]
// 将接收到的数据发送回客户端
_, err = conn.Write(recvBuffer[:15])
if err != nil {
log.Println("发送数据失败:", err)
break
}
}
if recvBuffer[2] == 93 {
c00 := BytesToHexWithPrefix(recvBuffer[10:14])
c01 := BytesToHexWithPrefix(recvBuffer[14:18])
c02 := BytesToHexWithPrefix(recvBuffer[18:22])
c04, err := CombineHexWithPrefixPerByte(c00)
c05, err := CombineHexWithPrefixPerByte(c01)
c06, err := CombineHexWithPrefixPerByte(c02)
c1, err := hexToTime(c04)
myMap["c1"] = int(c1.UnixMilli())
c2, err := hexToTime(c05)
myMap["c2"] = int(c2.UnixMilli())
c3, err := hexToTime(c06)
myMap["c3"] = int(c3.UnixMilli())
c4, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[22:26]))
myMap["c4"], _ = strconv.Atoi(c4)
c5, err := strconv.ParseInt(BytesToHex(recvBuffer[26:27]), 16, 64)
myMap["c5"] = int(c5)
c6, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[27:31]))
myMap["c6"], _ = strconv.Atoi(c6)
c7, err := strconv.ParseInt(BytesToHex(recvBuffer[31:32]), 16, 64)
myMap["c7"] = int(c7)
c8, err := HexToDecimalWithTwoDecimalPlaces(BytesToHex(recvBuffer[32:36]))
myMap["c8"], _ = strconv.Atoi(c8)
c9, err := HexToDecimalWithTwoDecimalPlaces(BytesToHex(recvBuffer[36:40]))
myMap["c9"], _ = strconv.Atoi(c9)
c10, err1 := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[40:44]))
myMap["c10"], _ = strconv.Atoi(c10)
c11, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[44:48]))
myMap["c11"], _ = strconv.Atoi(c11)
c12, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[48:52]))
myMap["c12"], _ = strconv.Atoi(c12)
c13, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[52:56]))
myMap["c13"], _ = strconv.Atoi(c13)
c14, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[56:60]))
myMap["c14"], _ = strconv.Atoi(c14)
c15, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[60:64]))
myMap["c15"], _ = strconv.Atoi(c15)
c16, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[64:68]))
myMap["c16"], _ = strconv.Atoi(c16)
c17, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[68:72]))
myMap["c17"], _ = strconv.Atoi(c17)
c18, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[72:76]))
myMap["c18"], _ = strconv.Atoi(c18)
c19, err := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[76:80]))
myMap["c19"], _ = strconv.Atoi(c19)
c20, err3 := HexToDecimalWithTwoDecimalPlaces1(BytesToHex(recvBuffer[80:84]))
myMap["c20"], _ = strconv.Atoi(c20)
c03 := BytesToHexWithPrefix(recvBuffer[84:88])
c21, err := CombineHexWithPrefixPerByte(c03)
myMap["c21"], _ = strconv.Atoi(c21)
c22 := BytesToHex(recvBuffer[88:89])
myMap["c22"], _ = strconv.Atoi(c22)
if err != nil {
log.Fatal(err)
}
if err1 != nil {
log.Fatal(err1)
}
if err3 != nil {
log.Fatal(err3)
}
_, err2 := db.Exec("insert into dzdata1(上传时间戳,吊装开始时间戳,吊装结束时间戳,最大载重,最大载重百分比,最大力矩,最大力矩百分比,开始转角,结束转角,开始幅度,结束幅度,最大工作幅度,最小工作幅度,开始起升高度,结束起升高度,最大起升高度,最小起升高度,最大风速,最大倾角X,最大倾角Y,设备状态,校验和)values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?)", c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22)
if err2 != nil {
log.Fatal(err2)
}
recvBuffer[5] = 0x09
recvBuffer[10] = recvBuffer[88]
recvBuffer[11] = recvBuffer[89]
recvBuffer[12] = recvBuffer[90]
recvBuffer[13] = recvBuffer[91]
recvBuffer[14] = recvBuffer[92]
// 将接收到的数据发送回客户端
_, err = conn.Write(recvBuffer[:15])
if err != nil {
log.Println("发送数据失败:", err)
break
}
if recvBuffer[2] == 19 {
recvBuffer[5] = 0x01
// 将接收到的数据发送回客户端
_, err = conn.Write(recvBuffer[:19])
if err != nil {
log.Println("发送数据失败:", err)
break
}
}
}
tcpChan <- BytesToHex(recvBuffer[:recvBuffer[2]])
state = 0x00
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/timer_a/1216-internship-group.git
git@gitee.com:timer_a/1216-internship-group.git
timer_a
1216-internship-group
1216实习小组
master

搜索帮助