Skip to content

Commit

Permalink
Add AGWriteDBBit
Browse files Browse the repository at this point in the history
  • Loading branch information
leoluk committed Mar 18, 2023
1 parent c85e130 commit 5a5c7c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
3 changes: 2 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"time"
)

//Client interface s7 client
// Client interface s7 client
type Client interface {
/***************start API AG (Automatisationsgerät)***************/
//Read data blocks from PLC
AGReadDB(dbNumber int, start int, size int, buffer []byte) (err error)
//write data blocks into PLC
AGWriteDB(dbNumber int, start int, size int, buffer []byte) (err error)
AGWriteDBBit(dbNumber int, start int, size int, buffer []byte) (err error)
//Read Merkers area from PLC
AGReadMB(start int, size int, buffer []byte) (err error)
//Write Merkers from into PLC
Expand Down
54 changes: 29 additions & 25 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,47 +71,51 @@ func NewClient2(packager Packager, transporter Transporter) Client {
return &client{packager: packager, transporter: transporter}
}

//implement of the interface AGReadDB
// implement of the interface AGReadDB
func (mb *client) AGReadDB(dbnumber int, start int, size int, buffer []byte) (err error) {
return mb.readArea(s7areadb, dbnumber, start, size, s7wlbyte, buffer)
}

//implement of the interface AGWriteDB
// implement of the interface AGWriteDB
func (mb *client) AGWriteDB(dbNumber int, start int, size int, buffer []byte) (err error) {
return mb.writeArea(s7areadb, dbNumber, start, size, s7wlbyte, buffer)
}

//implement of the interface AGReadMB
func (mb *client) AGWriteDBBit(dbNumber int, start int, size int, buffer []byte) (err error) {
return mb.writeArea(s7areadb, dbNumber, start, size, s7wlbit, buffer)
}

// implement of the interface AGReadMB
func (mb *client) AGReadMB(start int, size int, buffer []byte) (err error) {
return mb.readArea(s7areamk, 0, start, size, s7wlbyte, buffer)
}

//implement of the interface AGWriteMB
// implement of the interface AGWriteMB
func (mb *client) AGWriteMB(start int, size int, buffer []byte) (err error) {
return mb.writeArea(s7areamk, 0, start, size, s7wlbyte, buffer)
}

//implement of the interface AGReadEB
// implement of the interface AGReadEB
func (mb *client) AGReadEB(start int, size int, buffer []byte) (err error) {
return mb.readArea(s7areape, 0, start, size, s7wlbyte, buffer)
}

//implement of the interface AGWriteEB
// implement of the interface AGWriteEB
func (mb *client) AGWriteEB(start int, size int, buffer []byte) (err error) {
return mb.writeArea(s7areape, 0, start, size, s7wlbyte, buffer)
}

//implement of the interface AGReadAB
// implement of the interface AGReadAB
func (mb *client) AGReadAB(start int, size int, buffer []byte) (err error) {
return mb.readArea(s7areapa, 0, start, size, s7wlbyte, buffer)
}

//implement of the interface AGWriteAB
// implement of the interface AGWriteAB
func (mb *client) AGWriteAB(start int, size int, buffer []byte) (err error) {
return mb.writeArea(s7areapa, 0, start, size, s7wlbyte, buffer)
}

//implement of the interface AGReadTM - read timer
// implement of the interface AGReadTM - read timer
func (mb *client) AGReadTM(start int, amount int, buffer []byte) (err error) {
sbuffer := make([]byte, amount*2)
err = mb.readArea(s7areatm, 0, start, amount, s7wltimer, sbuffer)
Expand All @@ -123,7 +127,7 @@ func (mb *client) AGReadTM(start int, amount int, buffer []byte) (err error) {
return err
}

//implement of the interface AGWriteTM - write timer
// implement of the interface AGWriteTM - write timer
func (mb *client) AGWriteTM(start int, amount int, buffer []byte) (err error) {
sbuffer := make([]byte, amount*2)
for c := 0; c < amount; c++ {
Expand All @@ -134,7 +138,7 @@ func (mb *client) AGWriteTM(start int, amount int, buffer []byte) (err error) {
return err
}

//implement of the interface AGReadCT - read counter
// implement of the interface AGReadCT - read counter
func (mb *client) AGReadCT(start int, amount int, buffer []byte) (err error) {
sbuffer := make([]byte, amount*2)
err = mb.readArea(s7areact, 0, start, amount, s7wlcounter, sbuffer)
Expand All @@ -146,7 +150,7 @@ func (mb *client) AGReadCT(start int, amount int, buffer []byte) (err error) {
return err
}

//implement of the interface AGWriteCT - write counter
// implement of the interface AGWriteCT - write counter
func (mb *client) AGWriteCT(start int, amount int, buffer []byte) (err error) {
sbuffer := make([]byte, amount*2)
for c := 0; c < amount; c++ {
Expand All @@ -157,7 +161,7 @@ func (mb *client) AGWriteCT(start int, amount int, buffer []byte) (err error) {
return err
}

//read generic area, pass result into a buffer
// read generic area, pass result into a buffer
func (mb *client) readArea(area int, dbNumber int, start int, amount int, wordLen int, buffer []byte) (err error) {
var address, numElements, maxElements, totElements, sizeRequested int
offset := 0
Expand Down Expand Up @@ -224,7 +228,7 @@ func (mb *client) readArea(area int, dbNumber int, start int, amount int, wordLe
request.Data[29] = byte(address & 0x0FF)
address = address >> 8
request.Data[28] = byte(address & 0x0FF)
var response *ProtocolDataUnit
var response *ProtocolDataUnit
response, sendError := mb.send(&request)
err = sendError

Expand All @@ -248,13 +252,13 @@ func (mb *client) readArea(area int, dbNumber int, start int, amount int, wordLe
return
}

//writeArea write generic area into PLC with following parameters:
//1.area: s7areape/s7areapa/s7areamk/s7areadb/s7areact/s7areatm
//2.dbnumber: specify dbnumber, to use in write DB area, otherwise = 0
//3.start: start of the address
//4.amount: amount of the address
//5.wordlen: bit/byte/word/dword/real/counter/timer
//6.buffer: a byte array input for writing
// writeArea write generic area into PLC with following parameters:
// 1.area: s7areape/s7areapa/s7areamk/s7areadb/s7areact/s7areatm
// 2.dbnumber: specify dbnumber, to use in write DB area, otherwise = 0
// 3.start: start of the address
// 4.amount: amount of the address
// 5.wordlen: bit/byte/word/dword/real/counter/timer
// 6.buffer: a byte array input for writing
func (mb *client) writeArea(area int, dbnumber int, start int, amount int, wordlen int, buffer []byte) (err error) {
var address, numElements, maxElements, totElements, dataSize, isoSize, length int
offset := 0
Expand Down Expand Up @@ -372,7 +376,7 @@ func (mb *client) writeArea(area int, dbnumber int, start int, amount int, wordl
return
}

//DBRead
// DBRead
func (mb *client) Read(variable string, buffer []byte) (value interface{}, err error) {
variable = strings.ToUpper(variable) //upper
variable = strings.Replace(variable, " ", "", -1) //remove spaces
Expand Down Expand Up @@ -464,7 +468,7 @@ func (mb *client) Read(variable string, buffer []byte) (value interface{}, err e
return
}

//send the package of a pdu request and a pdu response, check for response error and verify the package
// send the package of a pdu request and a pdu response, check for response error and verify the package
func (mb *client) send(request *ProtocolDataUnit) (response *ProtocolDataUnit, err error) {
dataResponse, err := mb.transporter.Send(request.Data)
if err != nil {
Expand All @@ -487,7 +491,7 @@ func (mb *client) send(request *ProtocolDataUnit) (response *ProtocolDataUnit, e
return response, err
}

//responseError get response error from pdu return S7Error with high and low byte
// responseError get response error from pdu return S7Error with high and low byte
func responseError(response *ProtocolDataUnit) error {
s7Error := &S7Error{}
if response.Data != nil && len(response.Data) > 0 {
Expand All @@ -509,7 +513,7 @@ func responseError(response *ProtocolDataUnit) error {
return s7Error
}

//dataSize to number of byte accordingly
// dataSize to number of byte accordingly
func dataSizeByte(wordLength int) int {
switch wordLength {
case s7wlbit:
Expand Down

0 comments on commit 5a5c7c1

Please sign in to comment.