WIP
parent
2b1c4b27f0
commit
50913ab552
|
@ -3,6 +3,7 @@ package org.hwo.servicelink;
|
|||
import java.awt.TexturePaint;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.hwo.ByteArrayHexlifier;
|
||||
import java.io.InputStreamReader;
|
||||
|
@ -17,11 +18,13 @@ import static org.hwo.logging.LogLevel.*;
|
|||
public class SimpleLink {
|
||||
|
||||
NewSerialPort port;
|
||||
BufferedReader br;
|
||||
//BufferedReader br;
|
||||
SimpleLineReader br;
|
||||
|
||||
public SimpleLink(NewSerialPort port){
|
||||
this.port = port;
|
||||
this.br = new BufferedReader(new InputStreamReader(this.port.getInputStream()));
|
||||
//this.br = new BufferedReader(new InputStreamReader(this.port.getInputStream()));
|
||||
this.br = new SimpleLineReader(this.port.getInputStream());
|
||||
}
|
||||
|
||||
public void close(){
|
||||
|
@ -33,7 +36,7 @@ public class SimpleLink {
|
|||
}
|
||||
|
||||
public void open() throws IOException, ServiceLinkException{
|
||||
this.port.setBaudRate(BaudRate.B57600);
|
||||
//this.port.setBaudRate(BaudRate.B57600);
|
||||
this.port.open();
|
||||
}
|
||||
|
||||
|
@ -56,12 +59,12 @@ public class SimpleLink {
|
|||
}
|
||||
|
||||
|
||||
public synchronized float readFloat(int reg) throws IOException, ServiceLinkException{
|
||||
public float readFloat(int reg) throws IOException, ServiceLinkException{
|
||||
String sreg = fixhexstring(Integer.toHexString(reg),4);
|
||||
|
||||
String ol = String.format("R%s\n\r", sreg);
|
||||
port.getOutputStream().write(ol.getBytes());
|
||||
String reply = br.readLine();
|
||||
|
||||
String reply = request( ol );
|
||||
|
||||
if (reply.length() != 9){
|
||||
throw new ServiceLinkException();
|
||||
|
@ -69,37 +72,162 @@ public class SimpleLink {
|
|||
|
||||
return Float.intBitsToFloat(Integer.parseUnsignedInt(reply.substring(1), 16));
|
||||
}
|
||||
public synchronized int readInt(int reg) throws IOException, ServiceLinkException{
|
||||
String sreg = fixhexstring(Integer.toHexString(reg),4);
|
||||
public int readInt(int reg) throws IOException, ServiceLinkException{
|
||||
return readInt(new int[]{ reg })[0];
|
||||
}
|
||||
|
||||
public void writeInt(int reg,int value) throws IOException, ServiceLinkException{
|
||||
writeInt(new int[]{ reg },new int[]{value});
|
||||
}
|
||||
|
||||
public void writeFloat(int reg,float value) throws IOException, ServiceLinkException{
|
||||
|
||||
String ol = String.format("R%s\n\r", sreg);
|
||||
port.getOutputStream().write(ol.getBytes());
|
||||
String reply = br.readLine();
|
||||
}
|
||||
|
||||
public Integer[] readInt(int[] reg) throws IOException, ServiceLinkException{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Integer[] values = new Integer[reg.length];
|
||||
|
||||
if (reply.length() != 9){
|
||||
throw new ServiceLinkException("Short read: " + reply);
|
||||
sb.append("R");
|
||||
|
||||
for (int i=0;i<reg.length;i++){
|
||||
sb.append(fixhexstring(Integer.toHexString(reg[i]),4));
|
||||
}
|
||||
|
||||
return Integer.parseUnsignedInt(reply.substring(1), 16);
|
||||
sb.append("\n\r");
|
||||
|
||||
String reply = request(sb.toString());
|
||||
|
||||
for (int i=0;i<reg.length;i++){
|
||||
if (reply.charAt(1 + (i*9)) == 'V'){
|
||||
values[i] = Integer.parseUnsignedInt(reply.substring(2 + (i*9),10 + (i*9)), 16);
|
||||
};
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
public Integer[] readInt(int startReg,int no) throws IOException, ServiceLinkException{
|
||||
Integer[] values = new Integer[no];
|
||||
String tx = String.format("R+%s%s\n\r",fixhexstring(Integer.toHexString(no),2),fixhexstring(Integer.toHexString(startReg),4));
|
||||
|
||||
String reply = request(tx);
|
||||
|
||||
for (int i=0;i<no;i++){
|
||||
if (reply.charAt(1 + (i*9)) == 'V'){
|
||||
values[i] = Integer.parseUnsignedInt(reply.substring(2 + (i*9),10 + (i*9)), 16);
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
public synchronized void writeInt(int reg,int value) throws IOException, ServiceLinkException{
|
||||
String sreg = fixhexstring(Integer.toHexString(reg),4);
|
||||
String svalue = fixhexstring(Integer.toHexString(value),8);
|
||||
public boolean[] writeInt(int[] reg,int[] values) throws IOException, ServiceLinkException{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
String ol = String.format("W%s%s\n\r", sreg, svalue);
|
||||
port.getOutputStream().write(ol.getBytes());
|
||||
if (reg.length != values.length){
|
||||
throw new IllegalArgumentException("reg.length != values.length");
|
||||
};
|
||||
|
||||
sb.append("W");
|
||||
|
||||
for (int i=0;i<reg.length;i++){
|
||||
sb.append(fixhexstring(Integer.toHexString(reg[i]),4));
|
||||
sb.append(fixhexstring(Integer.toHexString(values[i]),8));
|
||||
}
|
||||
|
||||
sb.append("\n\r");
|
||||
|
||||
String reply = request(sb.toString());
|
||||
boolean[] rb = new boolean[reg.length];
|
||||
|
||||
for (int i=0;i<reg.length;i++){
|
||||
if (reply.charAt(1 + (i*5)) == 'V'){
|
||||
int rreg = Integer.parseUnsignedInt(reply.substring(2 + (i*5),6 + (i*5)), 16);
|
||||
if (reg[i] == rreg){
|
||||
rb[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rb;
|
||||
}
|
||||
|
||||
public boolean[] writeInt(int startReg,int[] values) throws IOException, ServiceLinkException{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("W+");
|
||||
sb.append(fixhexstring(Integer.toHexString(values.length),2));
|
||||
sb.append(fixhexstring(Integer.toHexString(startReg),4));
|
||||
|
||||
for (int i=0;i<values.length;i++){
|
||||
sb.append(fixhexstring(Integer.toHexString(values[i]),8));
|
||||
}
|
||||
|
||||
sb.append("\n\r");
|
||||
|
||||
String reply = request(sb.toString());
|
||||
boolean[] rb = new boolean[values.length];
|
||||
|
||||
for (int i=0;i<values.length;i++){
|
||||
if (reply.charAt(1 + (i*5)) == 'V'){
|
||||
int rreg = Integer.parseUnsignedInt(reply.substring(2 + (i*5),6 + (i*5)), 16);
|
||||
if ((startReg+i) == rreg){
|
||||
rb[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rb;
|
||||
}
|
||||
public synchronized String request(String request) throws IOException{
|
||||
long st = System.currentTimeMillis();
|
||||
log(DEBUGDETAIL,"request(): tx [%d] %s",st,request);
|
||||
port.getOutputStream().write(request.getBytes());
|
||||
log(DEBUGDETAIL,"request(): rx [+%d]",(System.currentTimeMillis()-st));
|
||||
String reply = br.readLine();
|
||||
|
||||
if ((reply.length() != 1)){
|
||||
throw new ServiceLinkException("Failed: " + reply);
|
||||
}
|
||||
if (!reply.equals("A")){
|
||||
throw new ServiceLinkException("Failed: " + reply);
|
||||
}
|
||||
log(DEBUGDETAIL,"request(): [+%d] %d bytes: %s",(System.currentTimeMillis()-st),reply.length(),reply);
|
||||
return reply;
|
||||
}
|
||||
|
||||
public synchronized void writeFloat(int reg,float value) throws IOException, ServiceLinkException{
|
||||
|
||||
|
||||
class SimpleLineReader {
|
||||
InputStream stream;
|
||||
byte[] ibuffer;
|
||||
int ipos,ilen;
|
||||
|
||||
public SimpleLineReader(InputStream stream){
|
||||
this.stream = stream;
|
||||
this.ibuffer = new byte[512];
|
||||
this.ilen = 0;
|
||||
this.ipos = 0;
|
||||
}
|
||||
|
||||
int readByte(){
|
||||
if (ipos >= ilen){
|
||||
ipos = 0;
|
||||
try {
|
||||
ilen = this.stream.read(ibuffer);
|
||||
} catch (IOException e){
|
||||
log(e);
|
||||
ilen = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return ibuffer[ipos++];
|
||||
}
|
||||
|
||||
String readLine(){
|
||||
byte[] buffer = new byte[512];
|
||||
int p = 0;
|
||||
do {
|
||||
int ch = readByte();
|
||||
if (ch == 0x0d){
|
||||
break;
|
||||
}
|
||||
buffer[p++] = (byte)ch;
|
||||
} while (p < buffer.length);
|
||||
return new String(buffer,0,p);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue