コード例 #1
0
		/// <summary>
		/// shutdown the connection
		/// </summary>
		private void Shutdown()
		{
			plc.Close();
			plc = null;

			bt_connect.Enabled = true;
			ip.Enabled = true;
			port.Enabled = true;
			transport_type.Enabled = true;
			bt_close.Enabled = false;
			bt_connection_data_read.Enabled = false;
			bt_speed_test.Enabled = false;
			groupWord.Enabled = false;
			groupBit.Enabled = false;
			groupDialogText.Enabled = false;

			UpdateNodesEnabled();
		}
コード例 #2
0
		/// <summary>
		/// try to connect to the plc
		/// </summary>
		private void Connect()
		{
			if (string.IsNullOrWhiteSpace(ip.Text) || string.IsNullOrWhiteSpace(port.Text))
				return;

			try
			{
				mcOMRON.TransportType protocol = (mcOMRON.TransportType) transport_type.SelectedItem;
				plc = new OmronPLC(protocol);
				
				IPAddress ipAddress = IPAddress.Parse(ip.Text);
				int port = Convert.ToInt32(this.port.Text);
				
				switch(protocol)
				{
					case mcOMRON.TransportType.Tcp:
						tcpFINSCommand tcpFinsCommand = (tcpFINSCommand) plc.FinsCommand;
						tcpFinsCommand.SetTCPParams(ipAddress, port);
						break;

					case mcOMRON.TransportType.Udp:
						if (string.IsNullOrWhiteSpace(source_node.Text) || string.IsNullOrWhiteSpace(destination_node.Text))
							return;

						int sa1 = Convert.ToInt32(source_node.Text);
						int da1 = Convert.ToInt32(destination_node.Text);
						
						udpFINSCommand udpFinsCommand = (udpFINSCommand) plc.FinsCommand;
						udpFinsCommand.SetUDPParams(ipAddress, port, sa1, da1);
						
						break;
				}

				// connection
				//
				if (! plc.Connect())
				{
					throw new Exception(plc.LastError);
				}

				if (protocol == mcOMRON.TransportType.Tcp)
				{
					tcpFINSCommand tcpFinsCommand = (tcpFINSCommand) plc.FinsCommand;
					
					destination_node.Text = tcpFinsCommand.DA1.ToString();
					source_node.Text = tcpFinsCommand.SA1.ToString();
				}

				// set UI
				//
				bt_connect.Enabled = false;
				ip.Enabled = false;
				this.port.Enabled = false;
				destination_node.Enabled = false;
				source_node.Enabled = false;
				transport_type.Enabled = false;
				bt_close.Enabled = true;
				bt_connection_data_read.Enabled = true;
				bt_speed_test.Enabled = true;
				groupWord.Enabled = true;
				groupBit.Enabled = true;
				groupDialogText.Enabled = true;
				dialog.Clear();
			}
			catch (Exception ex)
			{
				MessageBox.Show("Connect() error: " + ex.Message);
			}
		}