IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> GOlang语言实现TCP调试助手客户端功能并把返回的数据结果保存到结构体数组里面(解析Modbus TCP协议为案例) -> 正文阅读

[网络协议]GOlang语言实现TCP调试助手客户端功能并把返回的数据结果保存到结构体数组里面(解析Modbus TCP协议为案例)

package main

import (
   "fmt"
   "net"
   "strings"
)

const (
   BitNotConsidered ="不考虑的比特位"
   bit01 = "断纤告警"
   bit02 = "高温告警"
   bit03 = "温升告警"
)
type TotalStat struct {
   GlobalAlarm string
   //第一个寄存器bit0~bit3的状态
   FirstBitGlobalAlarmStatus string
   SecondBitGlobalAlarmStatus string
   ThirdBitGlobalAlarmStatus string
   ForthBitGlobalAlarmStatus string


    //第二个寄存器每个比特采集状态
   FirstBitCollectStatus string
   SecondBitCollectStatus string
   ThirdBitCollectStatus string
   ForthBitCollectStatus string
   FifthBitCollectStatus string
   SixthBitCollectStatus string
   SeventhBitCollectStatus string
   EighthBitCollectStatus string
   NinthBitCollectStatus string
   TenthBitCollectStatus string
   EleventhBitCollectStatus string
   TwelfthBitCollectStatus string
   ThirteenthBitCollectStatus string
   FourteenthBitCollectStatus string
   FifteenthBitCollectStatus string
   SixteenthBitCollectStatus string


   //第五个寄存器每个比特断纤状态
   FirstBitBrokenFiberStatus string
   SecondBitBrokenFiberStatus string
   ThirdBitBrokenFiberStatus string
   ForthBitBrokenFiberStatus string
   FifthBitBrokenFiberStatus string
   SixthBitBrokenFiberStatus string
   SeventhBitBrokenFiberStatus string
   EighthBitBrokenFiberStatus string
   NinthBitBrokenFiberStatus string
   TenthBitBrokenFiberStatus string
   EleventhBitBrokenFiberStatus string
   TwelfthBitBrokenFiberStatus string
   ThirteenthBitBrokenFiberStatus string
   FourteenthBitBrokenFiberStatus string
   FifteenthBitBrokenFiberStatus string
   SixteenthBitBrokenFiberStatus string
//第六个寄存器断纤告警状态
   FirstBitFiberCutStatus string
   SecondBitFiberCutStatus string
   ThirdBitFiberCutStatus string
   ForthBitFiberCutStatus string
   FifthBitFiberCutStatus string
   SixthBitFiberCutStatus string
   SeventhBitFiberCutStatus string
   EighthBitFiberCutStatus string
   NinthBitFiberCutStatus string
   TenthBitFiberCutStatus string
   EleventhBitFiberCutStatus string
   TwelfthBitFiberCutStatus string
   ThirteenthBitFiberCutStatus string
   FourteenthBitFiberCutStatus string
   FifteenthBitFiberCutStatus string
   SixteenthBitFiberCutStatus string
   //第八个寄存器断纤告警状态
   FirstBitFiberCutAlarmStatus string
   SecondBitFiberCutAlarmStatus string
   ThirdBitFiberCutAlarmStatus string
   ForthBitFiberCutAlarmStatus string
   FifthBitFiberCutAlarmStatus string
   SixthBitFiberCutAlarmStatus string
   SeventhBitFiberCutAlarmStatus string
   EighthBitFiberCutAlarmStatus string

   CH16 string
   CH1732 string
}
func Str2DEC(s string) (num int) {
   l := len(s)
   for i := l - 1; i >= 0; i-- {
      num += (int(s[l-i-1]) & 0xf) << uint8(i)

   }
   return
}

type DeviceStatus struct{
   RegisterID int
   RegisterName string
   FirstBitStatus string
   SecondBitStatus string
   ThirdBitStatus string
   ForthBitStatus string
   FifthBitStatus string
   SixthBitStatus string
   SeventhBitStatus string
   EighthBitStatus string
   NinthBitStatus string
   TenthBitStatus string
   EleventhBitStatus string
   TwelfthBitStatus string
   ThirteenthBitStatus string
   FourteenthBitStatus string
   FifteenthBitStatus string
   SixteenthBitStatus string
}
type DeviceStatusNumber struct{
   RegisterId int
   RegisterName string
   Number int
}
var TS TotalStat

func main() {

   //主动连接服务器
   conn, err := net.Dial("tcp", "192.168.1.154:502")
   if err != nil {
      fmt.Println("client dial err=", err)
      return
   }
   //defer conn.Close()
   _, err = conn.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x01, 0x00, 0x09}) // + "\n"
   if err != nil {
      fmt.Println("conn.Write err=", err)
   }
   //接收服务器回复的数据
   buf := make([]byte, 2048)

   for {
      //n,err2:= conn.Read(buf)
      n, err2 := conn.Read(buf)
      if err2 != nil {
         fmt.Println("服务器read err=", err2)
         return
      }
      st := fmt.Sprintf("%08b", buf[9:n])     //%08b
      st1 := strings.Replace(st, " ", "", -1) //去除字符串所有空格
      runeArr := []rune(st1)                  //runeArr := []rune(inputStr)

      //解析数据
      //第一个寄存器
      Binary := string(runeArr[1:17]) //取字符串第1到16个字符
      //字符串转数组并打印显示
      var stringArr [16]string
      for index, value := range Binary {
         stringArr[index] = string(value)
      }
      if stringArr[0] == "1" {
         TS.FirstBitGlobalAlarmStatus = "告警状态,故障为设备与软件中断"
      } else {
         TS.FirstBitGlobalAlarmStatus = "设备正常"
      }
      if stringArr[1] == "1" {
         TS.SecondBitGlobalAlarmStatus = "全局告警,故障为硬件故障"
      } else {
         TS.SecondBitGlobalAlarmStatus = "设备正常"
      }
      if stringArr[2] == "1" {
         TS.ThirdBitGlobalAlarmStatus = "全局告警,故障为设备断纤"
      } else {
         TS.ThirdBitGlobalAlarmStatus = "设备正常"
      }
      if stringArr[3] == "1" {
         TS.ForthBitGlobalAlarmStatus = "全局告警,故障为温度告警"
      } else {
         TS.ForthBitGlobalAlarmStatus = "设备正常"
      }
      //第二个寄存器
      Binary2 := string(runeArr[17:33]) //取字符串第1到16个字符,左闭右开
      //字符串转数组
      var stringArr2 [16]string
      for index, value := range Binary2 {
         stringArr2[index] = string(value)
      }
      if stringArr2[0] == "1" {
         TS.FirstBitCollectStatus = "正常采集"
      } else {
         TS.FirstBitCollectStatus = "停止采集"
      }
      if stringArr2[1] == "1" {
         TS.SecondBitCollectStatus = "正常采集"
      } else {
         TS.SecondBitCollectStatus = "停止采集"
      }
      if stringArr2[2] == "1" {
         TS.ThirdBitCollectStatus = "正常采集"
      } else {
         TS.ThirdBitCollectStatus = "停止采集"
      }
      if stringArr2[3] == "1" {
         TS.ForthBitCollectStatus = "正常采集"
      } else {
         TS.ForthBitCollectStatus = "停止采集"
      }
      if stringArr2[4] == "1" {
         TS.FifthBitCollectStatus = "正常采集"
      } else {
         TS.FifthBitCollectStatus = "停止采集"
      }
      if stringArr2[5] == "1" {
         TS.SixthBitCollectStatus = "正常采集"
      } else {
         TS.SixthBitCollectStatus = "停止采集"
      }
      if stringArr2[6] == "1" {
         TS.SeventhBitCollectStatus = "正常采集"
      } else {
         TS.SeventhBitCollectStatus = "停止采集"
      }
      if stringArr2[7] == "1" {
         TS.EighthBitCollectStatus = "正常采集"
      } else {
         TS.EighthBitCollectStatus = "停止采集"
      }
      if stringArr2[8] == "1" {
         TS.NinthBitCollectStatus = "正常采集"
      } else {
         TS.NinthBitCollectStatus = "停止采集"
      }
      if stringArr2[9] == "1" {
         TS.TenthBitCollectStatus = "正常采集"
      } else {
         TS.TenthBitCollectStatus = "停止采集"
      }
      if stringArr2[10] == "1" {
         TS.EleventhBitCollectStatus = "正常采集"
      } else {
         TS.EleventhBitCollectStatus = "停止采集"
      }
      if stringArr2[11] == "1" {
         TS.TwelfthBitCollectStatus = "正常采集"
      } else {
         TS.TwelfthBitCollectStatus = "停止采集"
      }
      if stringArr2[12] == "1" {
         TS.ThirteenthBitCollectStatus = "正常采集"
      } else {
         TS.ThirteenthBitCollectStatus = "停止采集"
      }
      if stringArr2[13] == "1" {
         TS.FourteenthBitCollectStatus = "正常采集"
      } else {
         TS.FourteenthBitCollectStatus = "停止采集"
      }
      if stringArr2[14] == "1" {
         TS.FifteenthBitCollectStatus = "正常采集"
      } else {
         TS.FifteenthBitCollectStatus = "停止采集"
      }
      if stringArr2[15] == "1" {
         TS.SixteenthBitCollectStatus = "正常采集"
      } else {
         TS.SixteenthBitCollectStatus = "停止采集"
      }
      /*第三个寄存器
      //Binary3:=string(runeArr[33:49])//取字符串第1到16个字符,左闭右开
      //var stringArr3[16]string
      //for index,value:=range Binary3 {
      // stringArr3[index]=string(value)
      //}
      //Str2DEC(runeArr[33:49])
      //ChannelNumber:=Str2DEC(Binary3)
      //fmt.Println("设备通道数目为:",Str2DEC(Binary3))*/
      //第五个寄存器
      Binary5 := string(runeArr[65:81]) //取字符串左闭右开
      //字符串转数组
      var stringArr5 [16]string
      for index, value := range Binary5 {
         stringArr5[index] = string(value)
      }
      if stringArr5[0] == "1" {
         TS.FirstBitBrokenFiberStatus = "断纤"
      } else {
         TS.FirstBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[1] == "1" {
         TS.SecondBitBrokenFiberStatus = "断纤"
      } else {
         TS.SecondBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[2] == "1" {
         TS.ThirdBitBrokenFiberStatus = "断纤"
      } else {
         TS.ThirdBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[3] == "1" {
         TS.ForthBitBrokenFiberStatus = "断纤"
      } else {
         TS.ForthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[4] == "1" {
         TS.FifthBitBrokenFiberStatus = "断纤"
      } else {
         TS.FifthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[5] == "1" {
         TS.SixthBitBrokenFiberStatus = "断纤"
      } else {
         TS.SixthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[6] == "1" {
         TS.SeventhBitBrokenFiberStatus = "断纤"
      } else {
         TS.SeventhBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[7] == "1" {
         TS.EighthBitBrokenFiberStatus = "断纤"
      } else {
         TS.EighthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[8] == "1" {
         TS.NinthBitBrokenFiberStatus = "断纤"
      } else {
         TS.NinthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[9] == "1" {
         TS.TenthBitBrokenFiberStatus = "断纤"
      } else {
         TS.TenthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[10] == "1" {
         TS.EleventhBitBrokenFiberStatus = "断纤"
      } else {
         TS.EleventhBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[11] == "1" {
         TS.TwelfthBitBrokenFiberStatus = "断纤"
      } else {
         TS.TwelfthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[12] == "1" {
         TS.ThirteenthBitBrokenFiberStatus = "断纤"
      } else {
         TS.ThirteenthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[13] == "1" {
         TS.FourteenthBitBrokenFiberStatus = "断纤"
      } else {
         TS.FourteenthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[14] == "1" {
         TS.FifteenthBitBrokenFiberStatus = "断纤"
      } else {
         TS.FifteenthBitBrokenFiberStatus = "正常无断纤"
      }
      if stringArr5[15] == "1" {
         TS.SixteenthBitBrokenFiberStatus = "断纤"
      } else {
         TS.SixteenthBitBrokenFiberStatus = "正常无断纤"
      }
      //第六个寄存器
      Binary6 := string(runeArr[81:97]) //取字符串左闭右开
      //字符串转数组
      var stringArr6 [16]string
      for index, value := range Binary6 {
         stringArr6[index] = string(value)
      }
      //fmt.Println("第六个寄存器",stringArr6)
      if stringArr6[0] == "1" {
         TS.FirstBitFiberCutStatus = "断纤"
      } else {
         TS.FirstBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[1] == "1" {
         TS.SecondBitFiberCutStatus = "断纤"
      } else {
         TS.SecondBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[2] == "1" {
         TS.ThirdBitFiberCutStatus = "断纤"
      } else {
         TS.ThirdBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[3] == "1" {
         TS.ForthBitFiberCutStatus = "断纤"
      } else {
         TS.ForthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[4] == "1" {
         TS.FifthBitFiberCutStatus = "断纤"
      } else {
         TS.FifthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[5] == "1" {
         TS.SixthBitFiberCutStatus = "断纤"
      } else {
         TS.SixthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[6] == "1" {
         TS.SeventhBitFiberCutStatus = "断纤"
      } else {
         TS.SeventhBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[7] == "1" {
         TS.EighthBitFiberCutStatus = "断纤"
      } else {
         TS.EighthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[8] == "1" {
         TS.NinthBitFiberCutStatus = "断纤"
      } else {
         TS.NinthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[9] == "1" {
         TS.TenthBitFiberCutStatus = "断纤"
      } else {
         TS.TenthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[10] == "1" {
         TS.EleventhBitFiberCutStatus = "断纤"
      } else {
         TS.EleventhBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[11] == "1" {
         TS.TwelfthBitFiberCutStatus = "断纤"
      } else {
         TS.TwelfthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[12] == "1" {
         TS.ThirteenthBitFiberCutStatus = "断纤"
      } else {
         TS.ThirteenthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[13] == "1" {
         TS.FourteenthBitFiberCutStatus = "断纤"
      } else {
         TS.FourteenthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[14] == "1" {
         TS.FifteenthBitFiberCutStatus = "断纤"
      } else {
         TS.FifteenthBitFiberCutStatus = "正常无断纤"
      }
      if stringArr6[15] == "1" {
         TS.SixteenthBitFiberCutStatus = "断纤"
      } else {
         TS.SixteenthBitFiberCutStatus = "正常无断纤"
      }
      //第8个寄存器
      Binary8 := string(runeArr[113:121]) //取字符串左闭右开
      //字符串转数组
      var stringArr8 [8]string
      for index, value := range Binary8 {
         stringArr8[index] = string(value)
      }
      //fmt.Println("第8个寄存器",stringArr8)
      if stringArr6[0] == "1" {
         TS.FirstBitFiberCutAlarmStatus = "断纤"
      } else {
         TS.FirstBitFiberCutAlarmStatus = "正常无断纤"
      }
      if stringArr6[1] == "1" {
         TS.SecondBitFiberCutAlarmStatus = "断纤"
      } else {
         TS.SecondBitFiberCutAlarmStatus = "正常无断纤"
      }
      if stringArr6[2] == "1" {
         TS.ThirdBitFiberCutAlarmStatus = "断纤"
      } else {
         TS.ThirdBitFiberCutAlarmStatus = "正常无断纤"
      }
      if stringArr6[3] == "1" {
         TS.ForthBitFiberCutAlarmStatus = "断纤"
      } else {
         TS.ForthBitFiberCutAlarmStatus = "正常无断纤"
      }
      if stringArr6[4] == "1" {
         TS.FifthBitFiberCutAlarmStatus = "断纤"
      } else {
         TS.FifthBitFiberCutAlarmStatus = "正常无断纤"
      }
      if stringArr6[5] == "1" {
         TS.SixthBitFiberCutAlarmStatus = "断纤"
      } else {
         TS.SixthBitFiberCutAlarmStatus = "正常无断纤"
      }
      if stringArr6[6] == "1" {
         TS.SeventhBitFiberCutAlarmStatus = "断纤"
      } else {
         TS.SeventhBitFiberCutAlarmStatus = "正常无断纤"
      }
      if stringArr6[7] == "1" {
         TS.EighthBitFiberCutAlarmStatus = "断纤"
      } else {
         TS.EighthBitFiberCutAlarmStatus = "正常无断纤"
      }

      var arr = [5]DeviceStatus{
         {1, "GlobalAlarm:", TS.FirstBitGlobalAlarmStatus, TS.SecondBitGlobalAlarmStatus, TS.ThirdBitGlobalAlarmStatus,
            TS.ForthBitGlobalAlarmStatus, BitNotConsidered, BitNotConsidered, BitNotConsidered,
            BitNotConsidered, BitNotConsidered, BitNotConsidered, BitNotConsidered,
            BitNotConsidered, BitNotConsidered, BitNotConsidered, BitNotConsidered,
            BitNotConsidered},
         {2, "CollectStatus:", TS.FirstBitCollectStatus, TS.SecondBitCollectStatus, TS.ThirdBitCollectStatus,
            TS.ForthBitCollectStatus, TS.FifthBitCollectStatus, TS.SixthBitCollectStatus, TS.SeventhBitCollectStatus,
            TS.EighthBitCollectStatus, TS.NinthBitCollectStatus, TS.TenthBitCollectStatus, TS.EleventhBitCollectStatus,
            TS.TwelfthBitCollectStatus, TS.ThirteenthBitCollectStatus, TS.FourteenthBitCollectStatus, TS.FifteenthBitCollectStatus,
            TS.SixteenthBitCollectStatus},
         {5, "CH1~16断纤告警状态:", TS.FirstBitBrokenFiberStatus, TS.SecondBitBrokenFiberStatus, TS.ThirdBitBrokenFiberStatus,
            TS.ForthBitBrokenFiberStatus, TS.FifthBitBrokenFiberStatus, TS.SixthBitBrokenFiberStatus, TS.SeventhBitBrokenFiberStatus,
            TS.EighthBitBrokenFiberStatus, TS.NinthBitBrokenFiberStatus, TS.TenthBitBrokenFiberStatus, TS.EleventhBitBrokenFiberStatus,
            TS.TwelfthBitBrokenFiberStatus, TS.ThirteenthBitBrokenFiberStatus, TS.FourteenthBitBrokenFiberStatus, TS.FifteenthBitBrokenFiberStatus,
            TS.SixteenthBitBrokenFiberStatus},
         {6, "CH17~32断纤告警状态:", TS.FirstBitFiberCutStatus, TS.SecondBitFiberCutStatus, TS.ThirdBitFiberCutStatus,
            TS.ForthBitFiberCutStatus, TS.FifthBitFiberCutStatus, TS.SixthBitFiberCutStatus, TS.SeventhBitFiberCutStatus,
            TS.EighthBitFiberCutStatus, TS.NinthBitFiberCutStatus, TS.TenthBitFiberCutStatus, TS.EleventhBitFiberCutStatus,
            TS.TwelfthBitFiberCutStatus, TS.ThirteenthBitFiberCutStatus, TS.FourteenthBitFiberCutStatus, TS.FifteenthBitFiberCutStatus,
            TS.SixteenthBitFiberCutStatus},
         {8, "CH25~32断纤告警状态:", TS.FirstBitFiberCutAlarmStatus, TS.SecondBitFiberCutAlarmStatus, TS.ThirdBitFiberCutAlarmStatus,
            TS.ForthBitFiberCutAlarmStatus, TS.FifthBitFiberCutAlarmStatus, TS.SixthBitFiberCutAlarmStatus, TS.SeventhBitFiberCutAlarmStatus,
            TS.EighthBitFiberCutAlarmStatus, BitNotConsidered, BitNotConsidered, BitNotConsidered,
            BitNotConsidered, BitNotConsidered, BitNotConsidered, BitNotConsidered,
            BitNotConsidered},
      }
      var number = [1]DeviceStatusNumber{
         {3, "ChannelNumber:", Str2DEC(string(runeArr[33:49]))},
      }
      for i := 0; i < len(number); i++ {
         fmt.Println(number[i])
      }
      for i := 0; i < len(arr); i++ {
         fmt.Println(arr[i])
      }





   }

 





   }






}
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-08-25 12:33:59  更:2021-08-25 12:34:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/25 21:24:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码