翻译结果:滚动计数器

huangapple go评论83阅读模式
英文:

Rollover counter

问题

我正在为Go语言编写一个翻转计数器的代码。每当来自PLC计数器的新值进来时,代码就会被访问一次。当值达到32768以上时,PLC计数器将重置为1,并将翻转赋值为1。我需要编写一段代码,可以计算重置的次数,并将这些重置存储在内存中,以便在代码重新启动时可以使用先前的重置值进行计算。

计数的公式如下:

总计数 = 计数 + 32768 * 重置次数

更新:
我已经为翻转计数编写了以下代码,但是当翻转的值达到1时,总计数变为132769而不是32768翻转计数的值存储为1在rollover.data文件中,但是当它被访问时,它被提取为11

input1和input2基本上是从PLC计数器中连续出现的值,在一个2事件计数窗口中。例如,初始值将是input1 = 0,input2 = 0,然后(input2 = 1,input1 = 0),(input2 = 2,input1 = 1)等。因此,按照这个逻辑,当input2 = 32768时,翻转的值应该增加1。在此值之后,PLC计数器将重置为1。

package main

import (
	"fmt"
	"io/ioutil"
	"strconv"
)

func main() {
	var input1 int = 32768
	var input2 int = 1

	if input1 == 0 {

		t := strconv.Itoa(0)
		mydata := []byte(t)
		recordfile := []byte(t)

		// the WriteFile method returns an error if unsuccessful
		err := ioutil.WriteFile("rollover.data", mydata, 0777)
		// handle this error
		if err != nil {
			// print it out
			fmt.Println(err)
		}
		err2 := ioutil.WriteFile("record.data", recordfile, 0777)
		// handle this error
		if err2 != nil {
			// print it out
			fmt.Println(err2)
		}
	}

	rollover, err := ioutil.ReadFile("rollover.data")
	fmt.Printf("%T\n", rollover)

	if err != nil {
		fmt.Println(err)
	}

	rollovervalue, err3 := strconv.Atoi(string(rollover))

	if err3 == nil {
		fmt.Printf("%T \n %v", rollovervalue, rollovervalue)
	}

	//fmt.Printf("%T", rollovervalue)
	totalcount := input2 + rollovervalue*32768

	fmt.Printf("%v\n", totalcount)

	if input2 == 32768 {

		t := strconv.Itoa(rollovervalue + 1)
		mydata := []byte(t)
		err := ioutil.WriteFile("rollover.data", mydata, 0777)

		if err != nil {
			// print it out
			fmt.Println(err)
		}

	}

}

以上是你要翻译的内容。

英文:

I am writing code for a rollover counter in Go. The code gets accessed every time a new value from the PLC counter comes in. When the value reaches above 32768, the PLC counter resets to 1 and roll over is assigned a value of 1. I need to write a code that can count the number of resets and store those resets in the memory such that whenever the code is restarted the previous value of reset is available for use in calculation.

The formula for counting is below:

Total count = count + 32768*number of resets

Update:
I have written the following code for the rollover counting but as soon as the value of rollover reaches 1, the total count becomes 132769 instead of 32768. The value of rollover counter gets stored as 1 in the rollover.data file but when it is accessed it is getting extracted as 11.

input1 and input2 are basically the consecutive values coming from PLC counter in a 2 event count window. For example the initial values will be input1 = 0 input2 = 0 and then (input2 = 1, input1 =0), (input2 = 2, input1 =1) etc. So by that logic when input2 = 32768, the value of rollover should increment by 1. After this value the PLC counter gets reset to 1.

package main
import (
"fmt"
"io/ioutil"
"strconv"
)
func main() {
var input1 int = 32768
var input2 int = 1
if input1 == 0 {
t := strconv.Itoa(0)
mydata := []byte(t)
recordfile := []byte(t)
// the WriteFile method returns an error if unsuccessful
err := ioutil.WriteFile("rollover.data", mydata, 0777)
// handle this error
if err != nil {
// print it out
fmt.Println(err)
}
err2 := ioutil.WriteFile("record.data", recordfile, 0777)
// handle this error
if err2 != nil {
// print it out
fmt.Println(err2)
}
}
rollover, err := ioutil.ReadFile("rollover.data")
fmt.Printf("%T\n", rollover)
if err != nil {
fmt.Println(err)
}
rollovervalue, err3 := strconv.Atoi(string(rollover))
if err3 == nil {
fmt.Printf("%T \n %v", rollovervalue, rollovervalue)
}
//fmt.Printf("%T", rollovervalue)
totalcount := input2 + rollovervalue*32768
fmt.Printf("%v\n", totalcount)
if input2 == 32768 {
t := strconv.Itoa(rollovervalue + 1)
mydata := []byte(t)
err := ioutil.WriteFile("rollover.data", mydata, 0777)
if err != nil {
// print it out
fmt.Println(err)
}
}
}

,,,

答案1

得分: 6

变量的内容在进程退出时会丢失。

当进程退出时,您需要一种方法来保持滚动计数器的持久化。有很多方法可以实现这一点:

  • 创建一个文件,并在值发生变化时将其写入文件。在程序启动时,如果文件存在,则读取文件。
  • 使用某种数据库(如MySQL、SQLite、Redis等)。
英文:

The contents of a variable are lost when the process exits.

You need a way to persist the rollover counter when the process exits. There are a lot of ways to achieve this:

  • create a file and write the value to the file whenever it changes. On program startup, read the file if it exists.
  • use some kind of database (an SQL database like MySQL, SQLite, Redis, etc.).

huangapple
  • 本文由 发表于 2021年6月24日 22:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/68118072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定