關於 RS232 傳輸接收資料
再寫有關這類的程式
我輸入的資料是從Java Text Area讀取 "86 00 00 00 01 82 05 "
類似這樣的字串
然後就直接給他寫入
outputStream.write(output.getText().getBytes());
在Java Text Area資料格式應該是字串,RS232應該是吃"16進位數值資料"
這樣直接丟過去會不會有錯呢?or應該如何做個轉換才好。
而我接收資料程式是這樣寫
readBuffer = new byte[1024];
try {
int numBytes = 0;
StringBuffer readBuffer = new StringBuffer();
int c;
while (inputStream.available()>0){
c=inputStream.read();
readBuffer.append((char) c);
input.append(Integer.toHexString(c));
}
input.append("\n");
} catch (ArrayIndexOutOfBoundsException g) {}
我測試的機器有附一個軟體(當然是正確的)
輸出資料:86 00 00 00 01 82 05
從RS232得到資料:C6 00 00 00 02 82 00 46
而我自己寫的程式
輸出資料(寫在TextArea): 86 00 00 00 01 82 05
得到資料:c00002ff13c
不知道哪裡出問題了 煩請解惑
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.198.118
推
07/30 22:35, , 1F
07/30 22:35, 1F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 7 篇):