-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
189 lines (156 loc) · 5.75 KB
/
Copy pathProgram.cs
File metadata and controls
189 lines (156 loc) · 5.75 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using BaseCommon;
using System.Configuration;
using System.Reflection;
using System.Diagnostics;
namespace ModbusSample
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Log log = Log.GetInstance();
try
{
VEMConfigHelper config = VEMConfigHelper.Create();
string par = System.Environment.CommandLine;
if (!string.IsNullOrEmpty(par) && par.IndexOf("show_ui", StringComparison.OrdinalIgnoreCase) >= 0)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TestForm1());
return;
}
Vendor vendor = new Vendor();
if (!vendor.SetPortName(config.Port))
{
log.WriteDebugLog(config.Port + " 连接:打开串口错误!");
}
log.WriteDebugLog(config.Port + "连接:" + (vendor.CommIsOk ? "通读正常" : "通读异常"));
Console.InputEncoding = System.Text.Encoding.UTF8;
Console.OutputEncoding = System.Text.Encoding.UTF8;
log.WriteDebugLog("设置编码正常!开始读取指令!");
Task readerTast = Task.Factory.StartNew(() =>
{
while (true)
{
string txt = Console.In.ReadLine();
log.WriteDebugLog("获取指令:" + txt);
try
{
switch (txt)
{
case "A":
OutPutGoods(vendor, "A", log);
break;
case "B":
OutPutGoods(vendor, "B", log);
break;
case "exit":
Application.Exit();
break;
default:
break;
}
}
catch (Exception exin)
{
log.Error(exin.Message, exin);
}
if (txt == "exit")
{
break;
}
}
log.WriteDebugLog("退出读取指令线程!");
});
readerTast.Wait();
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}
}
public static void OutPutGoods(Vendor Vendor, string Idx, Log log)
{
int goodid = -1;
System.Threading.Thread.Sleep(2000);
if (!GetIndex(Idx, ref goodid))
{
Console.WriteLine("出货" + Idx + ":" + "失败");
log.Error("出货" + Idx + ":" + "失败", "Program", "OutPutGoods");
return;
}
if (!Vendor.CommIsOk)
{
Console.WriteLine("目前系统通讯故障。");
log.WriteDebugLog("目前系统通讯故障。");
return;
}
if (Vendor.ModbusData[Vendor.CurrRunningMotorIndex] != 255)
{
Console.WriteLine("上一次出货任务还未完成,请稍候。");
log.WriteDebugLog("上一次出货任务还未完成,请稍候。");
return;
}
Vendor.OutputGoods(goodid, (flag) =>
{
log.WriteDebugLog("出货" + Idx + ":{0},货道:{1}".ExtFormat(flag ? "成功" : "失败", goodid));
Console.WriteLine("出货" + Idx + ":" + (flag ? "成功" : "失败"));
});
}
public static bool GetIndex(string P, ref int Index)
{
int channelstart = 0, channelend = 0, current = 0, Per = 0;
VEMConfigHelper config = VEMConfigHelper.Create();
List<ChannelInfo> channnels = new List<ChannelInfo>();
if (P == "B")
{
current = config.BCurrent;
channnels = config.GetProductBChannels();
}
else if (P == "A")
{
current = config.ACurrent;
channnels = config.GetProductAChannels();
}
if (channnels != null && channnels.Count > 0)
{
var chan = channnels.First(p => { return p.IndexE >= current && p.IndexS <= current; });
if (chan == null)
{
Index = -1;
return false;
}
else
{
Index = chan.ID;
}
if (P == "A")
{
config.ACurrent = current + 1;
}
else if (P == "B")
{
config.BCurrent = current + 1;
}
config.Save();
return true;
}
else
{
Log.log.Error(P + "商品已经用完了", "Progam", MethodBase.GetCurrentMethod().Name);
Index = -1;
return false;
}
}
}
}