通过Internet向设备发送命令并获取响应 [英] Sending command to device over internet and getting the response back

查看:86
本文介绍了通过Internet向设备发送命令并获取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以通过互联网与我的设备通信的Web应用程序。设备使用以太网端口进行通信。



之前我设计了一个Web应用程序可以通过LAN网络与称重设备通信。我想知道相同的代码是否可以用于互联网上的设备。我们使用IP地址和端口号来与设备通信。



我使用下面给出的代码进行沟通。



I want to create a web application that can communicate with my device over internet.The device use Ethernet port for communication.

Previously i have designed a web application that can communicate to weighing scale device over a LAN network.i wanted to know whether the same code can be used to devices over internet or not.We use IP address and port no to communicate to the device .

I have used the given below code for communication.

public string GetScaleWeight(string val1, string val2)//GetWeight contains the code to fetch the weight from the scale//
        {
            try
            {
                Socket sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//here we are using socket programming//
                IPAddress ip = IPAddress.Parse(val1);                                                    // to connect to the scale device//
                IPEndPoint dp = new IPEndPoint(ip, int.Parse(val2));
                sc.Connect(dp);
                Byte[] myArray = new Byte[100];
                int value = Convert.ToInt32("5", 16);
                string stringValue = Char.ConvertFromUtf32(value);
                sc.Send(Encoding.ASCII.GetBytes((stringValue)));//passing the hex command to the machine//
                int no = sc.Receive(myArray);//getting the length of the result array//
                string str = System.Text.Encoding.ASCII.GetString(myArray);//getting the result and storing it in a string//
                sc.Close();
                str = str.Substring(0, no);//checking and removing any unwanted garbage value if any //
               return str;
            }
            catch
            {
                return "";
            }
        }  





此代码可用于通过互联网与设备进行通信,并进行一些修改。

如果没有请帮助我们如何做到这一点。



Can this code be used to communicate to the devices over internet with some modification.
If not Please help on how can we do this.

推荐答案

如果没有中间代理或防火墙,套接字将起作用,但是,在互联网情况下不太可能,您应该使用HTTP / HTTPS进行通信。
Sockets would work if there are no intermediate proxies or firewall, but, since that is unlikely in case of internet, you should use HTTP/HTTPS for communication.


这篇关于通过Internet向设备发送命令并获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆