|
|
@@ -17,6 +17,7 @@ using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows.Input;
|
|
|
+using System.Windows.Threading;
|
|
|
|
|
|
namespace InkjetScale.GUI.MainViews.Views
|
|
|
{
|
|
|
@@ -24,24 +25,36 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
{
|
|
|
|
|
|
#region Property
|
|
|
- private eCommand commandName = eCommand.S;
|
|
|
- public eCommand CommandName { get => this.commandName; set => SetProperty(ref this.commandName, value); }
|
|
|
+ private eCommand selectedCommand;
|
|
|
+ public eCommand SelectedCommand { get => this.selectedCommand; set => SetProperty(ref this.selectedCommand, value); }
|
|
|
|
|
|
private eHeadList selectedHead;
|
|
|
public eHeadList SelectedHead { get => this.selectedHead; set => SetProperty(ref this.selectedHead, value); }
|
|
|
-
|
|
|
+
|
|
|
private bool isStable;
|
|
|
public bool IsStable { get => this.isStable; set => SetProperty(ref this.isStable, value); }
|
|
|
|
|
|
+ private bool isTareStable;
|
|
|
+ public bool IsTareStable { get => this.isTareStable; set => SetProperty(ref this.isTareStable, value); }
|
|
|
+
|
|
|
private string weightValue;
|
|
|
public string WeightValue { get => this.weightValue; set => SetProperty(ref this.weightValue, value); }
|
|
|
-
|
|
|
+
|
|
|
private string receiveMsg;
|
|
|
public string ReceiveMsg { get => this.receiveMsg; set => SetProperty(ref this.receiveMsg, value); }
|
|
|
|
|
|
+ private string tareReceiveMsg;
|
|
|
+ public string TareReceiveMsg { get => this.tareReceiveMsg; set => SetProperty(ref this.tareReceiveMsg, value); }
|
|
|
+
|
|
|
private string plValue;
|
|
|
public string PlValue { get => this.plValue; set => SetProperty(ref this.plValue, value); }
|
|
|
|
|
|
+ private string tareWeight;
|
|
|
+ public string TareWeight { get => this.tareWeight; set => SetProperty(ref this.tareWeight, value); }
|
|
|
+
|
|
|
+ private string dropCount;
|
|
|
+ public string DropCount { get => this.dropCount; set => SetProperty(ref this.dropCount, value); }
|
|
|
+
|
|
|
private ScaleControlView controlView;
|
|
|
public ScaleControlView ControlView { get => this.controlView; set => SetProperty(ref this.controlView, value); }
|
|
|
|
|
|
@@ -58,12 +71,13 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
|
|
|
SerialPort scale = new SerialPort();
|
|
|
|
|
|
- public ICommand SendCommand { get; set; }
|
|
|
public ICommand RunCommand { get; set; }
|
|
|
+
|
|
|
IEventAggregator ea = null;
|
|
|
IContainerProvider provider = null;
|
|
|
|
|
|
string Command = null;
|
|
|
+ Thread scaleThread = null;
|
|
|
|
|
|
public ScaleMainViewModel(IEventAggregator eventAggregator, IContainerProvider proVider)
|
|
|
{
|
|
|
@@ -73,33 +87,51 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
this.ea = eventAggregator;
|
|
|
this.provider = proVider;
|
|
|
|
|
|
- this.SendCommand = new DelegateCommand(ExecuteSend);
|
|
|
this.RunCommand = new DelegateCommand<string>(ExecuteRun);
|
|
|
+
|
|
|
+ scaleThread = new Thread(GetScaleInfo);
|
|
|
+ this.ea.GetEvent<SystemExitEvent>().Subscribe(SystemExitSubEvent, ThreadOption.UIThread);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ private void SystemExitSubEvent(bool isExit)
|
|
|
+ {
|
|
|
+ if (isExit)
|
|
|
+ {
|
|
|
+ isThreadUse = false;
|
|
|
+ scale.Close();
|
|
|
+ scaleThread.Join();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- #region Execute
|
|
|
- private void ExecuteSend()
|
|
|
+ bool isThreadUse = true;
|
|
|
+ private void GetScaleInfo()
|
|
|
{
|
|
|
- if (!scale.IsOpen)
|
|
|
- return;
|
|
|
- if (SelectedHead < 0)
|
|
|
- return;
|
|
|
+ while(scale.IsOpen)
|
|
|
+ {
|
|
|
+ Thread.Sleep(500);
|
|
|
+ if (!isThreadUse)
|
|
|
+ continue;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Command = "S";
|
|
|
+ scale.Write(Command + Environment.NewLine);
|
|
|
+ }
|
|
|
|
|
|
- Command = CommandName.ToString();
|
|
|
- scale.Write(Command + Environment.NewLine);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ #region Execute
|
|
|
+
|
|
|
private void ExecuteRun(string command)
|
|
|
{
|
|
|
if (!scale.IsOpen)
|
|
|
return;
|
|
|
- if (SelectedHead < 0)
|
|
|
- return;
|
|
|
+
|
|
|
+ if (scaleThread.IsAlive)
|
|
|
+ isThreadUse = false;
|
|
|
|
|
|
if (command != null)
|
|
|
- {
|
|
|
+ {
|
|
|
Command = command;
|
|
|
scale.Write(Command + Environment.NewLine); // command 보낼 때 \r\n 또는 Environment.NesLine 붙여서 보내야함
|
|
|
}
|
|
|
@@ -114,7 +146,7 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
{
|
|
|
if(!isInit)
|
|
|
{
|
|
|
-
|
|
|
+ DropCount = "2000";
|
|
|
this.ControlView = this.provider.Resolve<ScaleControlView>();
|
|
|
this.LogView = this.provider.Resolve<ScaleLogView>();
|
|
|
|
|
|
@@ -130,6 +162,10 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
|
|
|
OpenSerialPort();
|
|
|
|
|
|
+ this.SelectedHead = this.HeadList[(int)eHeadList.HeadPack1Head1 -1];
|
|
|
+
|
|
|
+ scaleThread.Start();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -154,15 +190,13 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
{
|
|
|
Thread.Sleep(50);
|
|
|
|
|
|
- ReceiveMsg = String.Empty;
|
|
|
- WeightValue = String.Empty;
|
|
|
byte[] ReceiveByte = new byte[28];
|
|
|
int num = this.scale.Read(ReceiveByte, 0, 28);
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- bool isNum;
|
|
|
char c;
|
|
|
+ bool isNum;
|
|
|
int val;
|
|
|
|
|
|
switch (Command)
|
|
|
@@ -170,6 +204,9 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
case "S": //-> S S 3.14 mg \r\n
|
|
|
{
|
|
|
|
|
|
+ ReceiveMsg = String.Empty;
|
|
|
+ WeightValue = String.Empty;
|
|
|
+
|
|
|
for (int index = 0; index < num; index++)
|
|
|
{
|
|
|
c = Convert.ToChar((object)ReceiveByte[index]); // Char 변환
|
|
|
@@ -178,13 +215,10 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
isNum = int.TryParse(c.ToString(), out val); //숫자인지 문자인지 확인 할 변수
|
|
|
if (isNum)
|
|
|
WeightValue += c;
|
|
|
- else
|
|
|
- {
|
|
|
- if (c == ' ' || c.ToString() == Command || c == '\r' || c == '\n')
|
|
|
- continue;
|
|
|
-
|
|
|
+ else if (c == '-' || c == '.')
|
|
|
WeightValue += c;
|
|
|
- }
|
|
|
+ else
|
|
|
+ continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -195,36 +229,67 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
else
|
|
|
IsStable = false;
|
|
|
|
|
|
- var mgVal = WeightValue.Substring(0, (WeightValue.Length - 5));
|
|
|
+ var mgValue = double.Parse(WeightValue);
|
|
|
var nozzle = 1024;
|
|
|
- var drops = 100;
|
|
|
- PlValue = $"{ScaleMath.GetPl(double.Parse(mgVal), nozzle, drops)}";
|
|
|
+ var drops = int.Parse(DropCount);
|
|
|
+ PlValue = $"{ScaleMath.GetPl(mgValue, nozzle, drops)}";
|
|
|
+
|
|
|
+ if (!isThreadUse)
|
|
|
+ ViewLogPubEvent(SelectedHead, mgValue, double.Parse(PlValue));
|
|
|
|
|
|
- ViewLogPubEvent(SelectedHead, double.Parse(WeightValue), double.Parse(mgVal));
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
case "Z": //-> Z A \r\n
|
|
|
{
|
|
|
+ ReceiveMsg = String.Empty;
|
|
|
for (int index = 0; index < num; index++)
|
|
|
{
|
|
|
c = Convert.ToChar((object)ReceiveByte[index]); // Char 변환
|
|
|
|
|
|
- if (c == ' ' || c == '\r' || c == '\n')
|
|
|
- continue;
|
|
|
- else
|
|
|
+ if (c == 'Z' || c == 'A')
|
|
|
ReceiveMsg += c;
|
|
|
+ else
|
|
|
+ continue;
|
|
|
}
|
|
|
|
|
|
if (ReceiveMsg == "ZA")
|
|
|
{
|
|
|
- ReceiveMsg = "Zero Complete";
|
|
|
- scale.Write("S" + Environment.NewLine);
|
|
|
+ ReceiveMsg = "Zero!";
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
+ case "T":
|
|
|
+ {
|
|
|
+ TareReceiveMsg = String.Empty;
|
|
|
+ TareWeight = String.Empty;
|
|
|
+
|
|
|
+ for (int index = 0; index < num; index++)
|
|
|
+ {
|
|
|
+ c = Convert.ToChar((object)ReceiveByte[index]); // Char 변환
|
|
|
+ TareReceiveMsg += c;
|
|
|
+
|
|
|
+ isNum = int.TryParse(c.ToString(), out val); //숫자인지 문자인지 확인 할 변수
|
|
|
+ if (isNum)
|
|
|
+ TareWeight += c;
|
|
|
+ else if (c == '-' || c == '.')
|
|
|
+ TareWeight += c;
|
|
|
+ else
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ var splitMsg = TareReceiveMsg.Split(' ');
|
|
|
+ var stableParam = splitMsg[1];
|
|
|
+ if (stableParam == "S" || stableParam == "M")
|
|
|
+ IsTareStable = true;
|
|
|
+ else
|
|
|
+ IsTareStable = false;
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
default:
|
|
|
{
|
|
|
ReceiveMsg = "Wrong Command!";
|
|
|
@@ -233,6 +298,9 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
|
|
|
}
|
|
|
|
|
|
+ if (!isThreadUse)
|
|
|
+ isThreadUse = true;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
catch
|