Skip to content

Commit

Permalink
Merge pull request #73 from nospam2000/fix-control-commands
Browse files Browse the repository at this point in the history
fix(control commands): fix result pdu handling
  • Loading branch information
robinson authored Dec 5, 2024
2 parents 612c92d + 35f4f24 commit 7ea1d6f
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"fmt"
)

//implement PLC hot start interface
// implement PLC hot start interface
func (mb *client) PLCHotStart() error {
requestData := make([]byte, len(s7HotStartTelegram))
copy(requestData, s7HotStartTelegram)
request := NewProtocolDataUnit(requestData)
//send
response, err := mb.send(&request)
if err == nil {
if length := len(response.Data); length > 18 { // 18 is the minimum expected
if length := len(response.Data); length >= 20 { // 20 is the minimum expected
if int(response.Data[19]) != pduStart {
err = fmt.Errorf(ErrorText(errCliCannotStartPLC))
} else {
} else if length >= 21 {
if int(response.Data[20]) == pduAlreadyStarted {
err = fmt.Errorf(ErrorText(errCliAlreadyRun))
} else {
Expand All @@ -33,18 +33,18 @@ func (mb *client) PLCHotStart() error {
return err
}

//implement of PLC Colde Start interface
// implement of PLC Colde Start interface
func (mb *client) PLCColdStart() error {
requestData := make([]byte, len(s7ColdStartTelegram))
copy(requestData, s7ColdStartTelegram)
request := NewProtocolDataUnit(requestData)
//send
response, err := mb.send(&request)
if err == nil {
if length := len(response.Data); length > 18 { // 18 is the minimum expected
if length := len(response.Data); length >= 20 { // 20 is the minimum expected
if int(response.Data[19]) != pduStart {
err = fmt.Errorf(ErrorText(errCliCannotStartPLC))
} else {
} else if length >= 21 {
if int(response.Data[20]) == pduAlreadyStarted {
err = fmt.Errorf(ErrorText(errCliAlreadyRun))
} else {
Expand All @@ -65,11 +65,11 @@ func (mb *client) PLCStop() error {
//send
response, err := mb.send(&request)
if err == nil {
if length := len(response.Data); length > 18 { // 18 is the minimum expected
if length := len(response.Data); length >= 20 { // 20 is the minimum expected
if int(response.Data[19]) != pduStop {
err = fmt.Errorf(ErrorText(errCliCannotStopPLC))
} else {
if int(response.Data[20]) == pduAlreadyStarted {
} else if length >= 21 {
if int(response.Data[20]) == pduAlreadyStopped {
err = fmt.Errorf(ErrorText(errCliAlreadyStop))
} else {
err = fmt.Errorf(ErrorText(errCliCannotStopPLC))
Expand All @@ -82,7 +82,6 @@ func (mb *client) PLCStop() error {
return err
}

//
func (mb *client) PLCGetStatus() (status int, err error) {
//initialize
requestData := make([]byte, len(s7GetStatusTelegram))
Expand Down

0 comments on commit 7ea1d6f

Please sign in to comment.