2026-08-28 15:06:01 +08:00
|
|
|
|
package plc
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2026-10-14 17:24:59 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/robinson/gos7"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
|
|
|
|
|
|
"bj_power_mes/internal/config"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// 西门子 S7 真读:48 位块(MB895..MB900,6 字节)覆盖产线点位信号。
|
|
|
|
|
|
// 位布局(按业务口径 2026-10 定稿,锚点 C15=M898.2):
|
|
|
|
|
|
//
|
|
|
|
|
|
// 位 0..11 G1..G12 工位托盘位(工位 n 的托盘传感器,QueryStationPallet 读)
|
|
|
|
|
|
// 位 12..26 C1..C15 主线缓存位(C15=M898.2 → 位 26)
|
|
|
|
|
|
// 位 27..46 DOCK01..DOCK20 工位接驳台托盘位(每工位 2 个,共 20)
|
|
|
|
|
|
// 位 47 JBT21 库房接驳台(AGV 取料位)
|
|
|
|
|
|
//
|
|
|
|
|
|
// 注:真实点位表以现场 PLC 工程师确认为准;以下布局由 C15=M898.2 反推,已满足 48 位自洽。
|
|
|
|
|
|
const (
|
|
|
|
|
|
plcBlockStartByte = 895
|
|
|
|
|
|
plcBlockBytes = 6
|
|
|
|
|
|
plcReadTimeout = 30 * time.Second
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// s7Real 真实西门子 S7 客户端(gos7),持有一条长连接,断线惰性重连。
|
|
|
|
|
|
type s7Real struct {
|
|
|
|
|
|
c config.PlcConf
|
|
|
|
|
|
handler *gos7.TCPClientHandler
|
|
|
|
|
|
client gos7.Client
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func newS7Real(c config.PlcConf) *s7Real {
|
|
|
|
|
|
h := gos7.NewTCPClientHandler(c.Host, c.Rack, c.Slot)
|
|
|
|
|
|
h.Timeout = plcReadTimeout
|
|
|
|
|
|
return &s7Real{c: c, handler: h}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ensure 惰性连接 + 断线重连。
|
|
|
|
|
|
func (s *s7Real) ensure() error {
|
|
|
|
|
|
if s.client != nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := s.handler.Connect(); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
s.client = gos7.NewClient(s.handler)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// readBlock 读取 48 位块(MB895..MB900)原始 6 字节。
|
|
|
|
|
|
func (s *s7Real) readBlock(ctx context.Context) ([]byte, error) {
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
|
|
cl := s.client
|
|
|
|
|
|
need := cl == nil
|
|
|
|
|
|
if need {
|
|
|
|
|
|
if err := s.ensure(); err != nil {
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
cl = s.client
|
|
|
|
|
|
}
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
buf := make([]byte, plcBlockBytes)
|
|
|
|
|
|
done := make(chan error, 1)
|
|
|
|
|
|
go func() { done <- cl.AGReadMB(plcBlockStartByte, plcBlockBytes, buf) }()
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
return nil, ctx.Err()
|
|
|
|
|
|
case err := <-done:
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
|
|
_ = s.handler.Close()
|
|
|
|
|
|
s.client = nil
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return buf, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// readByte 读取单个 Merker 字节(用于 48 位块之外的完成信号位等)。
|
|
|
|
|
|
func (s *s7Real) readByte(ctx context.Context, byteOffset int) (byte, error) {
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
|
|
if err := s.ensure(); err != nil {
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
cl := s.client
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
|
|
done := make(chan error, 1)
|
|
|
|
|
|
go func() { done <- cl.AGReadMB(byteOffset, 1, buf) }()
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
return 0, ctx.Err()
|
|
|
|
|
|
case err := <-done:
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
|
|
_ = s.handler.Close()
|
|
|
|
|
|
s.client = nil
|
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
return 0, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return buf[0], nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// bit 取块内某位(bitOffset 0..47)。
|
|
|
|
|
|
func bit(buf []byte, offset int) bool {
|
|
|
|
|
|
if offset < 0 || offset >= len(buf)*8 {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
return buf[offset/8]&(1<<(offset%8)) != 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// dockOffset DOCK 编码 → 块内位偏移;JBT21 单独处理。
|
|
|
|
|
|
func dockOffset(dockCode string) (int, bool) {
|
|
|
|
|
|
dockCode = strings.ToUpper(strings.TrimSpace(dockCode))
|
|
|
|
|
|
if dockCode == "JBT21" {
|
|
|
|
|
|
return 47, true
|
|
|
|
|
|
}
|
|
|
|
|
|
if !strings.HasPrefix(dockCode, "DOCK") {
|
|
|
|
|
|
return 0, false
|
|
|
|
|
|
}
|
|
|
|
|
|
n, err := strconv.Atoi(strings.TrimPrefix(dockCode, "DOCK"))
|
|
|
|
|
|
if err != nil || n < 1 || n > 20 {
|
|
|
|
|
|
return 0, false
|
|
|
|
|
|
}
|
|
|
|
|
|
return 27 + (n - 1), true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// tcpClient 真实 S7 对接实现(替换原 mock 占位)。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
type tcpClient struct {
|
2026-10-14 17:24:59 +08:00
|
|
|
|
c config.PlcConf
|
|
|
|
|
|
real *s7Real
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func newTcpClient(c config.PlcConf) *tcpClient {
|
2026-10-14 17:24:59 +08:00
|
|
|
|
return &tcpClient{c: c, real: newS7Real(c)}
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// SendProcessCode 下发工位组合(如 "1,3,5"),返回已下发工位码。
|
|
|
|
|
|
// 真实 S7:应把组合码写入 PLC 工艺寄存器;寄存器地址待现场点位表确认,当前仅解析并打点。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func (t *tcpClient) SendProcessCode(_ context.Context, combination string) ([]int, error) {
|
2026-10-14 17:24:59 +08:00
|
|
|
|
codes := parseCombination(combination)
|
|
|
|
|
|
// TODO(PLC): 待点位表确认后在此 gos7.WriteArea 写入组合码寄存器
|
|
|
|
|
|
return codes, nil
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// QueryDone 查询最近一次下发是否收到完成信号。
|
|
|
|
|
|
// 读 DoneByte.DoneBit(待点位表确认);未配置时安全返回 false(不下发下一条,不误进)。
|
|
|
|
|
|
func (t *tcpClient) QueryDone(ctx context.Context) (bool, error) {
|
|
|
|
|
|
if t.c.DoneByte < 0 || t.c.DoneBit < 0 {
|
|
|
|
|
|
return false, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
b, err := t.real.readByte(ctx, t.c.DoneByte)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return b&(1<<(t.c.DoneBit%8)) != 0, nil
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// AckDone 真实 S7 下完成由 PLC 自动置位,无需 MES 写;保留接口。
|
|
|
|
|
|
func (t *tcpClient) AckDone(_ context.Context, _ bool) {}
|
2026-09-04 14:18:53 +08:00
|
|
|
|
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// QueryDockPallet 读接驳台托盘传感器(AGV 到达目的地前确认接驳台空闲)。
|
|
|
|
|
|
func (t *tcpClient) QueryDockPallet(ctx context.Context, dockCode string) (bool, error) {
|
|
|
|
|
|
offset, ok := dockOffset(dockCode)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return false, fmt.Errorf("未知接驳台编码: %s", dockCode)
|
|
|
|
|
|
}
|
|
|
|
|
|
buf, err := t.real.readBlock(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return bit(buf, offset), nil
|
2026-10-13 15:51:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-14 17:24:59 +08:00
|
|
|
|
// QueryStationPallet 读工位托盘位(PLC↔MES 对账 #8):G{stationNo} 位。
|
|
|
|
|
|
func (t *tcpClient) QueryStationPallet(ctx context.Context, stationNo int) (bool, error) {
|
|
|
|
|
|
if stationNo < 1 || stationNo > 12 {
|
|
|
|
|
|
return false, fmt.Errorf("工位号越界: %d", stationNo)
|
|
|
|
|
|
}
|
|
|
|
|
|
buf, err := t.real.readBlock(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return bit(buf, stationNo-1), nil
|
|
|
|
|
|
}
|