public RemoteOperationMaster(MESSAGE_FRAME_TYPE_T frameType, MESSAGE_DATA_CODE_T dataCode, bool dedicationR, SocketInterface sc, ref DESTINATION_ADDRESS_T destination, int sendBufferSize = 4096, int receiveBufferSize = 4096, object sync = null) { __socket = sc; __frame_type = frameType; __data_code = dataCode; __destination = destination; __send_byte_array = new byte[sendBufferSize]; __receive_byte_array = new byte[receiveBufferSize]; __sync_object = sync ?? new object(); switch (frameType, dataCode, dedicationR) {
private async void CommunicationTest_Click(object sender, RoutedEventArgs e) { if (__property_error_counter != 0) { HandyControl.Controls.MessageBox.Show(this, "At least one user input field is invalid.", "Error Message", MessageBoxButton.OK, MessageBoxImage.Error); } else { var property = DataContext as TargetPropertyDataModel; SocketInterface com = null; IsEnabled = false; _BusyIndicator.Visibility = Visibility.Visible; try { DESTINATION_ADDRESS_T destination = new DESTINATION_ADDRESS_T( property.NetworkNumber, property.StationNumber, property.ModuleIONumber, property.MultidropNumber, property.ExtensionStationNumber); if (property.UDPTransportLayer == true) { com = new UDP(new System.Net.IPEndPoint(property.SourceIPv4, property.SourcePort), new System.Net.IPEndPoint(property.DestinationIPv4, property.DestinationPort), property.ReceiveBufferSize, property.SendTimeoutValue, property.ReceiveTimeoutValue); } else { com = new TCP(new System.Net.IPEndPoint(property.SourceIPv4, 0), new System.Net.IPEndPoint(property.DestinationIPv4, property.DestinationPort), property.SendTimeoutValue, property.ReceiveTimeoutValue); //__tcp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, 0)); await Task.Run(() => (com as TCP).Connect()); } var master = new RemoteOperationMaster(property.FrameType, property.DataCode, property.R_DedicatedMessageFormat, com, ref destination, property.SendBufferSize, property.ReceiveBufferSize, null); (ushort end, string name, ushort code) = await master.ReadTypeNameAsync(property.MonitoringTimer); if (end == (ushort)RESPONSE_MESSAGE_ENDCODE_T.NO_ERROR) { HandyControl.Controls.MessageBox.Show(this, $"Communication success, the destination device is { name.Trim()} (0x{code:X4}).", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } else { HandyControl.Controls.MessageBox.Show(this, $"The destination device returns end code 0x{end:X4}.", "Warning Message", MessageBoxButton.OK, MessageBoxImage.Warning); } } catch (SLMPException ex) { HandyControl.Controls.MessageBox.Show(this, "At least one unexpected error occured while doing communication test.\n" + ex.Message, "Error Message", MessageBoxButton.OK, MessageBoxImage.Error); } finally { _BusyIndicator.Visibility = Visibility.Hidden; IsEnabled = true; if (com != null) { com.Dispose(); } com = null; } } }
public async Task <DATA_SYNCHRONIZER_STATE_T> Startup(TargetPropertyDataModel target) { __sync_operation_access_lock.Wait(); try { switch (State) { case DATA_SYNCHRONIZER_STATE_T.CONNECTED: //throw new InvalidOperationException("The data synchronization thread is still running."); return(DATA_SYNCHRONIZER_STATE_T.CONNECTED); case DATA_SYNCHRONIZER_STATE_T.EXCEPTION: if (__data_sync_thread != null) { __data_sync_thread.Join(); __data_sync_thread = null; } break; case DATA_SYNCHRONIZER_STATE_T.READY: break; } __stop_event.Reset(); if (target.UDPTransportLayer) { __io = new UDP(new System.Net.IPEndPoint(target.SourceIPv4, target.SourcePort), new System.Net.IPEndPoint(target.DestinationIPv4, target.DestinationPort), target.ReceiveBufferSize, target.SendTimeoutValue, target.ReceiveTimeoutValue); } else { __io = new TCP(new System.Net.IPEndPoint(target.SourceIPv4, 0), new System.Net.IPEndPoint(target.DestinationIPv4, target.DestinationPort), target.SendTimeoutValue, target.ReceiveTimeoutValue); } DESTINATION_ADDRESS_T destination = new DESTINATION_ADDRESS_T(target.NetworkNumber, target.StationNumber, target.ModuleIONumber, target.MultidropNumber, target.ExtensionStationNumber); DeviceAccessMaster master = new DeviceAccessMaster(target.FrameType, target.DataCode, target.R_DedicatedMessageFormat, __io, ref destination, target.SendBufferSize, target.ReceiveBufferSize); if (__io is TCP) { State = DATA_SYNCHRONIZER_STATE_T.CONNECTING; await Task.Run(() => (__io as TCP).Connect()); } __heartbeat_counter = 0; __data_sync_thread = new Thread(new ParameterizedThreadStart(__data_sync_routine)); __data_sync_thread.Start(Tuple.Create(master, (ushort)(target.MonitoringTimer / 250), target.PollingInterval)); State = DATA_SYNCHRONIZER_STATE_T.CONNECTED; ExceptionMessage = ""; return(DATA_SYNCHRONIZER_STATE_T.CONNECTED); } catch (SLMPException ex) { State = DATA_SYNCHRONIZER_STATE_T.EXCEPTION; ExceptionMessage = ex.Message; return(DATA_SYNCHRONIZER_STATE_T.EXCEPTION); } finally { __sync_operation_access_lock.Release(); } }