Re: 關於 RS232 傳輸接收資料
謝謝你的解答,現在我還在困惑,要怎麼解決!
eg: "88 7F 00 00 07 82"
我把他中間的空白自動split
String str= output.getText();
String sentBuf[] = str.split(" ");
理論上 sentBuf[0]="88".......sentBuf[5]="82"
因為中間有"7F"
我又做了這樣轉換
For(0 to last)
int k=Integer.parseInt(sentBuf[i],16);
sentBuf[i]=Integer.toString(k);
經過這段code:sentBuf[1]= "7F" 應該會被轉換成 sentBuf[1]="127"
接著我就把split好且轉成的十進位丟進去給他吃。
tmp=sentBuf[i].getBytes();
outputStream.write(tmp);
//EndFor
剛剛板友,您提到要做轉換以及 LowByte HighByte的Endian這點我不懂要如何做
tmp=sentBuf[i].getBytes();
比如說我在做這一行code 假設i=0;
sentBuf[0]="88" <-應該是字串型態
透過 tmp =sentBuf[0].getBytes();
此時tmp陣列應該是什麼呢?
Case1: tmp[0]=88? (<-應該不是這樣,因為這樣一個Byte放不下)
Case2: tmp[0]='8' (<-其實應該是存ascii碼?)
tmp[1]='8'
因為直接把tmp印出來看似乎亂碼且每次不一樣,他只是存記憶體位置嗎?
按照板友的說法:
在這邊程式碼我是不是做個回文比較好?
sentBuf[1]="7F" 想辦法把他回文成 "F7"字串存回去?
============================================================
下面是部分程式碼
String str= output.getText();
String sentBuf[] = str.split(" ");
try{
if(sentByHex == false)
{
for(int i=0;i<sentBuf.length;i++)
{
byte[] tmp;
int k=Integer.parseInt(sentBuf[i],16);
sentBuf[i]=Integer.toString(k);
tmp=sentBuf[i].getBytes();
for(int m=0;m<tmp.length;m++)
{
System.out.print(tmp[m]+" ");
}
outputStream.write(tmp);
}
System.out.println();
}
=======================================================================
我丟 88 7F 00 00 07 82
然後system.out出來居然是 49 51 54 49 50 55 48 48 55 49 51 48
還蠻不知道怎樣
※ 引述《tkcn (小安)》之銘言:
: ※ 引述《poc7667 (poc)》之銘言:
: : 再寫有關這類的程式
: : 我輸入的資料是從Java Text Area讀取 "86 00 00 00 01 82 05 "
: : 類似這樣的字串
: : 然後就直接給他寫入
: : outputStream.write(output.getText().getBytes());
: : 在Java Text Area資料格式應該是字串,RS232應該是吃"16進位數值資料"
: : 這樣直接丟過去會不會有錯呢?or應該如何做個轉換才好。
: 你自己都說是收數值資料囉 :)
: 以 "86" 來說,這是兩個 unicode 字元,
: 在記憶體中的值為 0x0038 和 0x0036
: ( 印象中 Java 是 big-endian? 若有錯請指正 )
: 而所謂的數值 86,
: 就真的是 16 進位中的 86,
: 在記憶體中的值就是 0x86
: 所以你寫入的值確實是有問題的,
: 如果要自己轉成 "數值" 86,
: 你可能會需要用到 String 和 Integer 所提供的方法。
: 讀的部份也是一樣,
: 從 RS232 收到的是數值,
: 必須自己轉成 unicode,
: 才會是你想要看到的數字字串。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.199.54
※ 編輯: poc7667 來自: 140.113.199.54 (07/31 09:48)
討論串 (同標題文章)