|
|
@@ -1,4 +1,5 @@
|
|
|
using InkjetScale.BusinessLogic.Calculation;
|
|
|
+using InkjetScale.BusinessLogic.Scale;
|
|
|
using InkjetScale.Common;
|
|
|
using InkjetScale.Common.Events;
|
|
|
using InkjetScale.GUI.LogViews.Views;
|
|
|
@@ -25,9 +26,6 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
{
|
|
|
|
|
|
#region Property
|
|
|
- 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); }
|
|
|
|
|
|
@@ -54,162 +52,56 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
|
|
|
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); }
|
|
|
|
|
|
- private ScaleControlView controlView;
|
|
|
- public ScaleControlView ControlView { get => this.controlView; set => SetProperty(ref this.controlView, value); }
|
|
|
+ private RegisteredWeightView registerView;
|
|
|
+ public RegisteredWeightView RegisterView { get => this.registerView; set => SetProperty(ref this.registerView, value); }
|
|
|
|
|
|
private ScaleLogView logView;
|
|
|
public ScaleLogView LogView { get => this.logView; set => SetProperty(ref this.logView, value); }
|
|
|
|
|
|
- private List<eCommand> commandList;
|
|
|
- public List<eCommand> CommandList { get => this.commandList; set => SetProperty(ref this.commandList, value); }
|
|
|
-
|
|
|
private List<eHeadList> headList;
|
|
|
public List<eHeadList> HeadList { get => this.headList; set => SetProperty(ref this.headList, value); }
|
|
|
-
|
|
|
#endregion
|
|
|
|
|
|
- SerialPort scale = new SerialPort();
|
|
|
-
|
|
|
public ICommand RunCommand { get; set; }
|
|
|
-
|
|
|
IEventAggregator ea = null;
|
|
|
IContainerProvider provider = null;
|
|
|
+ ScaleManager scaleManager;
|
|
|
|
|
|
- string Command = null;
|
|
|
- Thread scaleThread = null;
|
|
|
-
|
|
|
- public ScaleMainViewModel(IEventAggregator eventAggregator, IContainerProvider proVider)
|
|
|
+ public ScaleMainViewModel(IEventAggregator eventAggregator, IContainerProvider proVider, ScaleManager scaleManager)
|
|
|
{
|
|
|
- this.CommandList = new List<eCommand>();
|
|
|
- this.HeadList = new List<eHeadList>();
|
|
|
-
|
|
|
+ this.scaleManager = scaleManager;
|
|
|
this.ea = eventAggregator;
|
|
|
this.provider = proVider;
|
|
|
+ this.HeadList = new List<eHeadList>();
|
|
|
|
|
|
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();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- bool isThreadUse = true;
|
|
|
- private void GetScaleInfo()
|
|
|
- {
|
|
|
- while(scale.IsOpen)
|
|
|
- {
|
|
|
- Thread.Sleep(500);
|
|
|
- if (!isThreadUse)
|
|
|
- continue;
|
|
|
- else
|
|
|
- {
|
|
|
- Command = "S";
|
|
|
- scale.Write(Command + Environment.NewLine);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- #region Execute
|
|
|
-
|
|
|
- private void ExecuteRun(string command)
|
|
|
- {
|
|
|
- if (!scale.IsOpen)
|
|
|
- return;
|
|
|
-
|
|
|
- if (scaleThread.IsAlive)
|
|
|
- isThreadUse = false;
|
|
|
-
|
|
|
- if (command != null)
|
|
|
- {
|
|
|
- Command = command;
|
|
|
- scale.Write(Command + Environment.NewLine); // command 보낼 때 \r\n 또는 Environment.NesLine 붙여서 보내야함
|
|
|
- }
|
|
|
- else
|
|
|
- return;
|
|
|
-
|
|
|
- }
|
|
|
- #endregion
|
|
|
-
|
|
|
- bool isInit = false;
|
|
|
- internal void Init()
|
|
|
- {
|
|
|
- if(!isInit)
|
|
|
- {
|
|
|
- DropCount = "2000";
|
|
|
- this.ControlView = this.provider.Resolve<ScaleControlView>();
|
|
|
- this.LogView = this.provider.Resolve<ScaleLogView>();
|
|
|
-
|
|
|
- for (eHeadList head = eHeadList.HeadPack1Head1; head <= eHeadList.HeadPack3Head4; head++)
|
|
|
- {
|
|
|
- this.HeadList.Add(head);
|
|
|
- }
|
|
|
-
|
|
|
- for (eCommand com = eCommand.S; com <= eCommand.C; com++)
|
|
|
- {
|
|
|
- this.CommandList.Add(com);
|
|
|
- }
|
|
|
-
|
|
|
- OpenSerialPort();
|
|
|
-
|
|
|
- this.SelectedHead = this.HeadList[(int)eHeadList.HeadPack1Head1 -1];
|
|
|
-
|
|
|
- scaleThread.Start();
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private void OpenSerialPort()
|
|
|
- {
|
|
|
- scale.PortName = "COM2";
|
|
|
- scale.BaudRate = 9600;
|
|
|
- scale.DataBits = 8;
|
|
|
- scale.StopBits = StopBits.One;
|
|
|
- scale.Parity = Parity.None;
|
|
|
-
|
|
|
- if (!scale.IsOpen)
|
|
|
- {
|
|
|
- scale.Open();
|
|
|
- scale.Encoding = Encoding.Default;
|
|
|
- scale.DataReceived += new SerialDataReceivedEventHandler(SerialDataReceived);
|
|
|
- }
|
|
|
+ this.ea.GetEvent<ReceiveMessageEvent>().Subscribe(ScaleMessageSubEvent);
|
|
|
}
|
|
|
|
|
|
- private void SerialDataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
|
+ private void ScaleMessageSubEvent(ReceiveMessageEventArgs obj)
|
|
|
{
|
|
|
- Thread.Sleep(50);
|
|
|
-
|
|
|
- byte[] ReceiveByte = new byte[28];
|
|
|
- int num = this.scale.Read(ReceiveByte, 0, 28);
|
|
|
-
|
|
|
try
|
|
|
{
|
|
|
char c;
|
|
|
bool isNum;
|
|
|
int val;
|
|
|
-
|
|
|
- switch (Command)
|
|
|
+
|
|
|
+ switch (obj.Command)
|
|
|
{
|
|
|
- case "S": //-> S S 3.14 mg \r\n
|
|
|
+ case eCommand.S: //-> S S 3.14 mg \r\n
|
|
|
{
|
|
|
|
|
|
ReceiveMsg = String.Empty;
|
|
|
WeightValue = String.Empty;
|
|
|
|
|
|
- for (int index = 0; index < num; index++)
|
|
|
+ for (int index = 0; index < obj.ReceiveCount; index++)
|
|
|
{
|
|
|
- c = Convert.ToChar((object)ReceiveByte[index]); // Char 변환
|
|
|
+ c = Convert.ToChar((object)obj.ReceiveByte[index]); // Char 변환
|
|
|
ReceiveMsg += c;
|
|
|
|
|
|
isNum = int.TryParse(c.ToString(), out val); //숫자인지 문자인지 확인 할 변수
|
|
|
@@ -228,24 +120,24 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
IsStable = true;
|
|
|
else
|
|
|
IsStable = false;
|
|
|
-
|
|
|
+
|
|
|
var mgValue = double.Parse(WeightValue);
|
|
|
var nozzle = 1024;
|
|
|
var drops = int.Parse(DropCount);
|
|
|
PlValue = $"{ScaleMath.GetPl(mgValue, nozzle, drops)}";
|
|
|
|
|
|
- if (!isThreadUse)
|
|
|
+ if (!obj.IsThreadUse)
|
|
|
ViewLogPubEvent(SelectedHead, mgValue, double.Parse(PlValue));
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- case "Z": //-> Z A \r\n
|
|
|
+ case eCommand.Z: //-> Z A \r\n
|
|
|
{
|
|
|
ReceiveMsg = String.Empty;
|
|
|
- for (int index = 0; index < num; index++)
|
|
|
+ for (int index = 0; index < obj.ReceiveCount; index++)
|
|
|
{
|
|
|
- c = Convert.ToChar((object)ReceiveByte[index]); // Char 변환
|
|
|
+ c = Convert.ToChar((object)obj.ReceiveByte[index]); // Char 변환
|
|
|
|
|
|
if (c == 'Z' || c == 'A')
|
|
|
ReceiveMsg += c;
|
|
|
@@ -253,7 +145,7 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- if (ReceiveMsg == "ZA")
|
|
|
+ if (ReceiveMsg == "ZA")
|
|
|
{
|
|
|
ReceiveMsg = "Zero!";
|
|
|
}
|
|
|
@@ -261,14 +153,14 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- case "T":
|
|
|
+ case eCommand.T:
|
|
|
{
|
|
|
TareReceiveMsg = String.Empty;
|
|
|
TareWeight = String.Empty;
|
|
|
|
|
|
- for (int index = 0; index < num; index++)
|
|
|
+ for (int index = 0; index < obj.ReceiveCount; index++)
|
|
|
{
|
|
|
- c = Convert.ToChar((object)ReceiveByte[index]); // Char 변환
|
|
|
+ c = Convert.ToChar((object)obj.ReceiveByte[index]); // Char 변환
|
|
|
TareReceiveMsg += c;
|
|
|
|
|
|
isNum = int.TryParse(c.ToString(), out val); //숫자인지 문자인지 확인 할 변수
|
|
|
@@ -290,6 +182,12 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
+ case eCommand.C:
|
|
|
+ {
|
|
|
+ ReceiveMsg = "Clear";
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
default:
|
|
|
{
|
|
|
ReceiveMsg = "Wrong Command!";
|
|
|
@@ -298,19 +196,43 @@ namespace InkjetScale.GUI.MainViews.Views
|
|
|
|
|
|
}
|
|
|
|
|
|
- if (!isThreadUse)
|
|
|
- isThreadUse = true;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
{
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ #region Execute
|
|
|
+ private void ExecuteRun(string command)
|
|
|
+ {
|
|
|
+ this.scaleManager.SendCommand(command);
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ bool isInit = false;
|
|
|
+ internal void Init()
|
|
|
+ {
|
|
|
+ if(!isInit)
|
|
|
+ {
|
|
|
+ DropCount = "2000";
|
|
|
+ //this.ControlView = this.provider.Resolve<ScaleControlView>();
|
|
|
+ this.RegisterView = this.provider.Resolve<RegisteredWeightView>();
|
|
|
+ this.LogView = this.provider.Resolve<ScaleLogView>();
|
|
|
+
|
|
|
+ for (eHeadList head = eHeadList.HeadPack1Head1; head <= eHeadList.HeadPack3Head4; head++)
|
|
|
+ {
|
|
|
+ this.HeadList.Add(head);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.SelectedHead = this.HeadList[(int)eHeadList.HeadPack1Head1 -1];
|
|
|
+ this.scaleManager.Open();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private void ViewLogPubEvent(eHeadList head, double weight, double pl)
|
|
|
{
|
|
|
- var c = new ViewLogArgs() { Head = head , Weight = weight, pl = pl};
|
|
|
+ var c = new ViewLogArgs() {IsRegister = false, Time = DateTime.Now, Head = head , Weight = weight, pl = pl};
|
|
|
ea.GetEvent<ViewLogEvent>().Publish(c);
|
|
|
}
|
|
|
}
|