Android设备USB转以太网连接Modbus TCP/IP服务器失败排查
问题描述
我正在开发一款Android平台的Modbus TCP/IP客户端应用,设备已连接USB转以太网转换器,尝试以笔记本电脑作为服务器通信时连接失败,报错信息如下:
java.net.ConnectionException: failed to connect to /192.168.29.244 (port 502) from /::(port 0): connect failed: ENETUNREACH (network is unreachable)
我已将手机设置为关闭Wi-Fi、移动数据或开启飞行模式,避免无线信号干扰。目前测试结果如下:
- Android设备与笔记本电脑处于同一Wi-Fi网络时,通信正常
- 设备通过USB以太网直连笔记本电脑时出现上述连接错误
- 设备通过Wi-Fi所在的网络交换机有线连接时,通信正常
相关核心代码如下:
Thread t1 = new Thread(() -> { /* The important instances of the classes mentioned before */ con = null; //the connection /* Variables for storing the parameters */ int port = Integer.parseInt(etPort.getText().toString()); String ip = etIP.getText().toString(); try { InetAddress addr = InetAddress.getByName(ip);//the slave's address con = new TCPMasterConnection(addr); con.setPort(port); con.setTimeout(500); Log.d("Connection parameters", "Timeout: " + con.getTimeout() + " Port: " + port); con.connect(); runOnUiThread(() -> { tv.setText("Connected!!!"); connect.setText("Disconnect"); }); } catch (Exception e) { e.printStackTrace(); Log.e("Error!", e.toString()); runOnUiThread(() -> { tv.setText(e.toString()); }); } }); if (connect.getText().equals("Connect")) t1.start(); else { Log.d("In diconnect", "Coonection closed!"); con.close(); connect.setText("Connect"); tv.setText("Disconnected!!!"); }
内容的提问来源于stack exchange,提问作者Darshan Modi
相关产品推荐
相关产品推荐

