Tjiny
2025-06-15 3c595f60b368838829b2e898478b269a762d55cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Linq;
using EasyModbus;
using HH.WCS.Mobox3.RiDong.apiMethod;
using HH.WCS.Mobox3.RiDong.models;
using HH.WCS.Mobox3.RiDong.util;
using S7.Net.Types;
using Task = HH.WCS.Mobox3.RiDong.models.Task;
 
namespace HH.WCS.Mobox3.RiDong.generalMethod;
 
/// <summary>
/// 线程方法
/// </summary>
public static class ThreadMenthod
{
    /// <summary>
    /// 根据作业创建对应任务
    /// </summary>
    public static void CreateTask()
    {
        // 获取所有等待中的作业
        var operations = AdoSqlMethod<Operation>.QueryList(p => p.N_B_STATE == 0);
        
        foreach (var operation in operations)
        {
            TaskHelper.CreateTask(operation);
        }
    }
 
    /// <summary>
    /// 根据配盘明细创建出库任务
    /// </summary>
    public static void CreateOutTaskFromDistributionCntrDetail()
    {
        var tnDistributionCntrs = AdoSqlMethod<TN_Distribution_CNTR>
            .QueryList(p => p.N_B_STATE == 1);
 
        TaskHelper.CreateOutTaskFromDistributionCntrDetail(tnDistributionCntrs);
    }
 
    /// <summary>
    /// 出库任务推送(AGV)
    /// </summary>
    public static void DispatchFromAGV()
    {
        // 查询中间表数据
        var conveyorLinesInfo = AdoSqlMethod<ConveyorLinesInfo>.QueryFirst(p=>p.PURPOSE == "门禁");
 
        if (conveyorLinesInfo.ACCESS == 0)
        {
            // 获取所有出库的agv任务
            var tasks = AdoSqlMethod<Task>
                .QueryList(p =>
                    p.N_B_STATE == 0 && p.N_SCHEDULE_TYPE == 2 &&
                    (p.N_TYPE == 2 || p.N_TYPE == 4 || p.N_TYPE == 5 || p.N_TYPE == 6 || p.N_TYPE == 7));
            
            
            // 排序
            tasks = tasks.OrderBy(p=>p.N_ROADWAY).ThenBy(p=>p.T_CREATE).ToList();
            
 
            foreach (var task in tasks)
            {
                // 判断有没有已推送的任务,有的话不推送,没有就推送
                // if(AdoSqlMethod<Task>.QueryCount(p=>(p.N_B_STATE == 1 || p.N_B_STATE == 2) && p.N_SCHEDULE_TYPE == 2 ))
                
                LogHelper.Info($"当前推送任务在第{task.N_ROADWAY}巷道,任务编号为{task.S_CODE}");
                
                // 出库AGV不做限制直接推送
                if (TaskHelper.SendTaskFromAGV(task))
                {
                    task.N_B_STATE = 1;
                    task.S_B_STATE = "已推送";
                    task.T_MODIFY = DateTime.Now;
                    AdoSqlMethod<Task>.UpdateFirst(task, p => new { p.N_B_STATE, p.S_B_STATE, p.T_MODIFY });
                }
            }
        }
    }
 
 
    /// <summary>
    /// 创建出库单(获取MaterialOut文件)
    /// </summary>
    public static void CreateOutboundOrderFromMaterialOut()
    {
        // 获取所有的MaterialOut数据
        var materialOuts = AdoSqlMethod<MaterialOut>.QueryList(p => p.S_CREATE_OK == 0);
 
        OutWareHouseService.CreateOutboundOrderFromMaterialOut(materialOuts);
    }
 
    /// <summary>
    /// 创建出库单(获取DoOut文件)
    /// </summary>
    public static void CreateOutboundOrderFromDoOut()
    {
        // 获取所有的DoOut数据
        var doOuts = AdoSqlMethod<DoOut>.QueryList(p => p.S_CREATE_OK == 0);
 
        OutWareHouseService.CreateOutboundOrderFromDoOut(doOuts);
    }
 
    /// <summary>
    /// 完成作业
    /// </summary>
    public static void AccomplishOperation()
    {
        var operations = AdoSqlMethod<Operation>.QueryList(p => p.N_B_STATE == 1);
 
        foreach (var operation in operations)
        {
            // 判断该任务是否全部完成
            var tasks = AdoSqlMethod<Task>.QueryList(p => p.S_OP_CODE == operation.S_CODE);
 
            if (tasks.Count == tasks.Count(p => p.N_B_STATE == 3))
            {
                operation.N_B_STATE = 2;
                operation.S_B_STATE = "完成";
                operation.T_MODIFY = DateTime.Now;
                operation.T_END_TIME = DateTime.Now;
 
                AdoSqlMethod<Operation>.UpdateFirst(operation,
                    p => new { p.N_B_STATE, p.S_B_STATE, p.T_MODIFY, p.T_END_TIME });
            }
            else if (tasks.Count(p => p.N_B_STATE == 4) > 0)
            {
                operation.N_B_STATE = 3;
                operation.S_B_STATE = "错误";
                operation.T_MODIFY = DateTime.Now;
                operation.T_END_TIME = DateTime.Now;
 
                AdoSqlMethod<Operation>.UpdateFirst(operation,
                    p => new { p.N_B_STATE, p.S_B_STATE, p.T_MODIFY, p.T_END_TIME });
            }
        }
    }
 
    /// <summary>
    /// 读取使用到的线体并执行对应的逻辑关系
    /// </summary>
    public static void ReadConveyorlinesMessage()
    {
        var conveyorlinesInfos =
            AdoSqlMethod<ConveyorLinesInfo>.QueryList(p => p.ENABLE == 1);
 
        foreach (var conveyorlinesInfo in conveyorlinesInfos)
        {
            PipeLineHelper.ReadConveyorlinesMessage(conveyorlinesInfo);
        }
    }
 
    public static void ModbusHelper()
    {
        // 要写入的字符串
        string inputString = "你好,Modbus TCP!";
            
        // 将字符串编码为字节(GB2312)
        byte[] byteData = System.Text.Encoding.GetEncoding("GB2312").GetBytes(inputString);
 
        // 计算寄存器数量及寄存器值(寄存器是16位的,每个寄存器可以存储2个字节)
        int numberOfRegisters = (int)Math.Ceiling((double)byteData.Length / 2);
        int[] registerValues = new Int32[numberOfRegisters];
 
        for (int i = 0; i < byteData.Length; i++)
        {
            if (i % 2 == 0) // 每两个字节组成一个寄存器
            {
                if (i + 1 < byteData.Length)
                {
                    // 高字节在前
                    registerValues[i / 2] = (ushort)((byteData[i] << 8) | byteData[i + 1]);
                }
                else
                {
                    // 填充低字节为 0
                    registerValues[i / 2] = (ushort)(byteData[i] << 8);
                }
            }
        }
 
        // 创建 Modbus 客户端
        ModbusClient client = new ModbusClient("169.93.160.60", 5003);
        try
        {
            // 连接到 Modbus 服务器
            client.Connect();
 
            // 写入寄存器,寄存器地址需要减去 1,因为 EasyModbus 使用 0 基址
            client.WriteMultipleRegisters(40001, registerValues);
 
            Console.WriteLine("Data written successfully.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}