应用场景:
1.称:kingbird plus 以太网台秤。说明书上写明具有自己固定的IP地址,通讯采用TCP/IP协议,通过标准 以太网RJ-45接口与网络相连。IP:192.168.1.101;port:3001
2.客户端:PC机
客户端代码如下:
class httpPortDeal implements Runnable {
Socket socket; BufferedReader bReader; InputStreamReader inputReader; InputStream input; public httpPortDeal() {
try {
socket = new Socket("192.168.1.101", 3001);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
*
* @param netAdd--IP
* 地址
* @param port--端口号
* @throws UnknownHostException
* @throws IOException
*/
public httpPortDeal(String netAdd, int port)
throws UnknownHostException, IOException {
socket = new Socket(netAdd, port);
System.out.println("成功连接!");
} public void run() {
try {
// 初始化
// setInput();
// setInputReader();
// setBufferedReader();
String data;
BigDecimal bdQty = new BigDecimal(String.valueOf(0.00));
while (true) {
setInput();
setInputReader();
setBufferedReader();
System.out.println(inputReader.ready());
System.out.println(bReader.ready());
data = bReader.readLine();
System.out.println(inputReader.read());
System.out.println(data);
if (null != data && !"".equals("")) {
bdQty = PeiSongUtil.getNullValue(data);
setRowValue(bdQty);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} private void setRowValue(BigDecimal bdQty) {
if (rowIndex != -1) {
// kdtEntrys.getRow(rowIndex).getCell("").setValue(bdQty);
txtconfirmWeight.setValue(bdQty);
txtcheckWeight.setValue(bdQty);
} else {
System.out.println("请选中行后按确认");
}
} public void close() {
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
通过 new Thread(new httpPortDeal()).start();// 开始读取网络数据
结果:只能第一次和称建立连接时读取到数据。之后无法获得数据。
3.现象:只能第一次和称建立连接时读取到数据。之后无法获得数据。在data = bReader.readLine();这个 位置停住,一直无法继续进行
1.称:kingbird plus 以太网台秤。说明书上写明具有自己固定的IP地址,通讯采用TCP/IP协议,通过标准 以太网RJ-45接口与网络相连。IP:192.168.1.101;port:3001
2.客户端:PC机
客户端代码如下:
class httpPortDeal implements Runnable {
Socket socket; BufferedReader bReader; InputStreamReader inputReader; InputStream input; public httpPortDeal() {
try {
socket = new Socket("192.168.1.101", 3001);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
*
* @param netAdd--IP
* 地址
* @param port--端口号
* @throws UnknownHostException
* @throws IOException
*/
public httpPortDeal(String netAdd, int port)
throws UnknownHostException, IOException {
socket = new Socket(netAdd, port);
System.out.println("成功连接!");
} public void run() {
try {
// 初始化
// setInput();
// setInputReader();
// setBufferedReader();
String data;
BigDecimal bdQty = new BigDecimal(String.valueOf(0.00));
while (true) {
setInput();
setInputReader();
setBufferedReader();
System.out.println(inputReader.ready());
System.out.println(bReader.ready());
data = bReader.readLine();
System.out.println(inputReader.read());
System.out.println(data);
if (null != data && !"".equals("")) {
bdQty = PeiSongUtil.getNullValue(data);
setRowValue(bdQty);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} private void setRowValue(BigDecimal bdQty) {
if (rowIndex != -1) {
// kdtEntrys.getRow(rowIndex).getCell("").setValue(bdQty);
txtconfirmWeight.setValue(bdQty);
txtcheckWeight.setValue(bdQty);
} else {
System.out.println("请选中行后按确认");
}
} public void close() {
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
通过 new Thread(new httpPortDeal()).start();// 开始读取网络数据
结果:只能第一次和称建立连接时读取到数据。之后无法获得数据。
3.现象:只能第一次和称建立连接时读取到数据。之后无法获得数据。在data = bReader.readLine();这个 位置停住,一直无法继续进行
我是说你主机发什么东西,它称会回说明东西
2.可以用TCP/UDP测试工具看看收发情况
像这种和硬件结合的,个人认为应该分步测试
首先用你的软件和像TCP/UDP这样测试工具进行通讯
成功后在用TCP/UDP测试工具于你的称通讯,两方面,都没问题了再往一起对接
3.你的程序到那里不走了应该是阻塞了,也就是说它在那一直等着硬件那边返回数据
证明连接时建立成功的,你自己确认读取的数据是正确的吧。2 之后无法获得数据
说明对方没有再次发送数据了,你应该在服务器端测试看看。readLine 需要服务器发送数据,并以\r\n结束,并且要把数据flush()才行!