gpt4 book ai didi

java - BGS5T RS232 与 1 线传感器通信

转载 作者:行者123 更新时间:2023-12-01 12:17:52 24 4
gpt4 key购买 nike

我有金雅拓 BGS5T java 模块和 1 线温度传感器。我有java midlet,它使用RS232端口与温度传感器进行通信。但问题是,无论我发送什么,我都没有得到传感器的响应。传感器上有正确的电压,因此连接应该没问题。我尝试测试我的程序,以便将 rs232 端口连接到计算机,并使用终端(Termite)观察发送的数据是否正确并且一切看起来都应该如此。另一个测试是我将温度传感器直接连接到计算机并使用终端发送数据,它的工作原理如下。我什至得到了一些随机输入的响应,例如 9999。我检查了终端内部连接的参数并将它们复制到 java midlet,但没有成功。只有一次我得到了一些回应,但当我第二天尝试继续工作时却没有成功。终端内参数:波特率:9600数据位:8停止位:1奇偶校验:无流量控制:RTS/CTS

这是Java代码:

String strCOM = "comm:COM0;blocking=on;baudrate=9600";
commConn = (CommConnection)Connector.open(strCOM);
System.out.println("CommConnection(" + strCOM + ") opened");
System.out.println("Real baud rate: " + commConn.getBaudRate());
inStream = commConn.openInputStream();
outStream = commConn.openOutputStream();
System.out.println("InputStream and OutputStream opened");
while(1==1)
{
byte bC1 = (byte)Integer.parseInt("11000001",2);
byte C1 = hexToBin("C1");
byte bparameter2 = (byte)Integer.parseInt("00010111",2);
byte bparameter3 = (byte)Integer.parseInt("01000101",2);
byte bparameter4 = (byte)Integer.parseInt("01011011",2);
byte bparameter5 = (byte)Integer.parseInt("00001111",2);
byte bparameter6 = (byte)Integer.parseInt("10010101",2);
byte[] bArray = {bparameter2,bparameter3,bparameter4,bparameter5,bparameter6};





int ch;
try {
outStream.write(bC1);
Thread.sleep(50);
//outStream.write(bArray);
outStream.write(bparameter2);
outStream.write(bparameter3);
outStream.write(bparameter4);
outStream.write(bparameter5);
outStream.write(bparameter6);

System.err.println("inStream bytes:" + inStream.available());
if(inStream.available() > 0)
{
String msg = "";
while(inStream.available() > 0)
{
ch = inStream.read();
msg = msg + (char) ch;
}
System.out.println("Serial msg: " + msg);
}
outStream.write('9');
outStream.write('9');
outStream.write('9');
outStream.write('9');
Thread.sleep(100);
outStream.write('9');
outStream.write('9');
outStream.write('9');
outStream.write('9');
System.err.println("inStream bytes:" + inStream.available());
if(inStream.available() > 0)
{
String msg = "";
while(inStream.available() > 0)
{
ch = inStream.read();
msg = msg + (char) ch;
}
System.out.println("Serial msg: " + msg);
}

最佳答案

我必须使用零调制解调器电缆才能使其正常工作。

关于java - BGS5T RS232 与 1 线传感器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26881525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com