2
我試圖將數據發送到串口,並將串口設備連接到我的電腦。在我的個人電腦中,我試圖通過終端應用程序接收數據。該設備使用J2ME,下面給出了我用來連接到com端口的代碼。通過串口發送數據
public boolean connect() {
if (bConnected) {
return true;
} else {
try {
StringBuffer strCom = new StringBuffer(80);
strCom.append("comm:" + strCOMPort
+ ";blocking=on;autocts=off;autorts=off;stopbits=");
//autocts=on;autorts=on;
strCom.append(stopbits);
strCom.append(";bitsperchar=");
strCom.append(bitsperchar);
//strCom.append(";parity=");
//strCom.append(parity);
strCom.append(";baudrate=");
strCom.append(baudrate);
commConn = (CommConnection) Connector.open(strCom.toString());
writeStatusToFile(
"CommConnection(" + strCom.toString()
+ ") with device opened.");
strCom = null;
inStream = commConn.openDataInputStream();
outStream = commConn.openOutputStream();
inStream.skip(inStream.available());
bConnected = true;
return true;
} catch (Exception IOe) {
writeStatusToFile(
"Opening COM0 IOException :" + IOe.getMessage());
return false;
}
}
}
我正在使用將數據寫入串行端口的代碼如下。
public void sendData(short[] message){
String bytedata = "";
try
{
System.out.println("Length of message array: " + message.length);
for(int i = 0; i<message.length; i++){
System.out.println("Data: " +message[i]);
bytedata += message[i];
outStream.write(message[i]);
System.out.println("Done");
}
//outStream.write(message);
outStream.flush();
}
catch(Exception ex)
{
System.out.println("Exception during sending bytes--->" + ex.toString());
ex.printStackTrace();
}
System.out.println(
"Data flushed to output stream: " + bytedata);
}
該設備的COM設置是COM0,波特率是4800,奇偶校驗是無,每字符8位和停止位是1(這些值被初始化globaly)。我在終端應用程序中設置了相同的功能,以便從COM端口接收數據。
我面對的問題是,當我連接到串行端口時,我沒有在PC中接收任何東西。我想知道我是否在代碼邏輯中犯了錯誤。任何可以幫助我分析問題的建議都是值得歡迎的。如果需要其他信息,請說明。