public static PopupViewModel CreateClearProductPopup(string message, long errorCode, AlcServerViewModel alcServer) { // TODO: define continue and quit message pack PlcMessagePack continueMessagePack = null; PlcMessagePack quitMessagePack = null; var content = errorCode == 2088? "请清除所有产品后点继续,或者点退出取消自动模式" : "请清料后点击继续,或者点退出再清料"; return(new PopupViewModel() { OkButtonText = "继续", CancelButtonText = "退出", OkCommand = new CloseDialogAttachedCommand(o => true, () => { alcServer.SentToPlc(continueMessagePack, PlcMessageType.Request); }), CancelCommand = new CloseDialogAttachedCommand(o => true, () => { alcServer.SentToPlc(quitMessagePack, PlcMessageType.Request); alcServer.IsAutoRunning = false; }), MessageItem = LoggingMessageItem.CreateMessage(message), Content = content, IsSpecialPopup = true }); }
public static PopupViewModel CreateSafeDoorPopup(string message, AlcServerViewModel alcServer) { // TODO: define continue and quit message pack PlcMessagePack continueMessagePack = null; PlcMessagePack quitMessagePack = null; var content = "请关门后点击继续,或者点取消退出自动模式"; return(new PopupViewModel() { OkButtonText = "继续", CancelButtonText = "取消", OkCommand = new CloseDialogAttachedCommand(o => true, () => { alcServer.SentToPlc(continueMessagePack, PlcMessageType.Request); }), CancelCommand = new CloseDialogAttachedCommand(o => true, () => { alcServer.SentToPlc(quitMessagePack, PlcMessageType.Request); alcServer.IsAutoRunning = false; }), MessageItem = LoggingMessageItem.CreateMessage(message), Content = content, IsSpecialPopup = true }); }
private void SwitchCommandID(PlcMessagePack messagePack) { switch (messagePack.CommandId) { case 1: SentToPlc(messagePack); OnPlcRegisterRequested(); break; case 2: SentToPlc(messagePack); OnPlcResetFinished(); break; case 3: SentToPlc(messagePack); OnPlcResetRequested(); break; case 5: SentToPlc(messagePack); OnPlcStartRequested(); break; case 6: OnPlcStopFinished(); break; case 7: SentToPlc(messagePack); OnPlcStopRequested(); break; case 9: OnPlcPauseRequested(); break; case 11: OnPlcContinueRequested(); break; case 12: SentToPlc(messagePack); OnWarningMessageReceived(messagePack.Errorcode); break; case 14: OnHeartBeatReceived(); break; case 23: OnPlcRequestToEnterNewRun(); break; default: OnCustomCommandReceived(messagePack.CommandId); break; } }
public void SentToPlc(PlcMessagePack messagePack, PlcMessageType messageType = PlcMessageType.Respond) { var output = messagePack; output.MsgType = (int)messageType; try { _clientSocket.Send(output.ToBytes()); OnMessagePackSent(messagePack); } catch { FailedAction(); } }
private void CommunicationLoop(Socket clientSocket) { while (true) { if (!IsConnected) { break; } var buffer = new byte[72]; int byteCount = 0; try { byteCount = clientSocket.Receive(buffer); } catch (SocketException) { FailedAction(); break; } // If no message received, end communication loop if (byteCount == 0) { break; } if (byteCount != 72) { continue; } var messagePack = PlcMessagePack.FromBytes(buffer); OnMessagePackReceived(messagePack); Task.Run(() => { SwitchCommandID(messagePack); }); } }
private void OnMessagePackSent(PlcMessagePack obj) { MessagePackSent?.Invoke(obj); }
private void OnMessagePackReceived(PlcMessagePack messagePack) { MessagePackReceived?.Invoke(messagePack); }