程式碼區塊
ipAddr = IPAddress.Parse("192.168.5.1");
ipep = new IPEndPoint(ipAddr, 9600);
server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
server.Connect(ipep);
MessageBox.Show("成功");
if (server.Connected)
{
byte[] data = { 0x46, 0x49, 0x4E, 0x53, 0x00, 0x00,
0x00, 0x0C,0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00,
0x00,0x00,0x00, 0x00, 0x03 };
server.Send(data);
byte[] rece0 = new byte[40];
server.Receive(rece0);
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
}
各位大神好!
小弟目前正在學習寫關於plC(omron)程式
現在都可以連接了與運作!
但目前用ethernet只能去手動設定電腦ip
才可以與plc連接溝通
是否有方法可以寫成用自動取的ip方式連接?
還請各位大神傾囊教導不才小弟
using System.Net;
private void button7_Click(object sender, EventArgs e)
{
String strHostName = Dns.GetHostName(); //取得本機電腦名稱
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
string ip = "";
foreach (IPAddress ipaddress in iphostentry.AddressList)
{
MessageBox.Show(ipaddress.ToString());
if (ipaddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
label1.Text = ipaddress.ToString();
ip = ip + ipaddress.ToString();
}
textBox1.Text=ip;
}
AddressFamily可以去選你要第幾版的IP
兩種方法取得本機IP