我有金雅拓BGS5T java模块和单线温度传感器。我有一个java midlet,它使用RS232端口与温度传感器通信。但问题是,无论我发送什么,我都不会得到传感器的响应。传感器上有正确的电压,所以连接应该很好。我试着测试我的程序,这样我就把rs232端口连接到了电脑上,并用终端(Termite)观察发送的数据是否正确,一切看起来都应该正确。另一个测试是,我将温度传感器直接连接到计算机上,并用终端发送数据,它正常工作。我甚至收到了一些随机输入的回复,比如9999。我检查了终端内部的连接参数,并将它们复制到javamidlet中,但没有成功。只有一次我得到了一些回应,但当我第二天试图继续工作时,我没有成功。终端内部参数:波特率: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);
}
我不得不使用零调制解调器电缆使其工作。