作者poc7667 (poc)
看板java
标题Re: 关於 RS232 传输接收资料
时间Thu Jul 31 09:37:36 2008
谢谢你的解答,现在我还在困惑,要怎麽解决!
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)