Skip to content
Snippets Groups Projects
Commit 5fde48f3 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs:

  corrected encodeBoolean
  changed encodePacked32
  added a flush to end() method

lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs:
  corrected struct handling
  corrected readInt
  changed decodePacked32  
parent 1301ffcd
No related branches found
No related tags found
No related merge requests found
MODULES=LabCommDispatcher \
LabCommDecoderRegistry \
LabComm \
LabCommSample \
LabCommHandler \
LabCommEncoderRegistry \
LabCommDecoder \
LabCommType \
LabCommEncoderChannel \
LabCommEncoder \
LabCommDecoderChannel \
all: labcomm.dll
labcomm.dll: $(MODULES:%=se/lth/control/labcomm/%.cs) Makefile
mcs -out:$@ -target:library $(filter %.cs, $^)
clean:
rm labcomm.dll
...@@ -6,6 +6,7 @@ namespace se.lth.control.labcomm { ...@@ -6,6 +6,7 @@ namespace se.lth.control.labcomm {
void register(LabCommDispatcher dispatcher, void register(LabCommDispatcher dispatcher,
LabCommHandler handler); LabCommHandler handler);
bool decodeBoolean(); bool decodeBoolean();
byte decodeByte(); byte decodeByte();
short decodeShort(); short decodeShort();
...@@ -14,6 +15,7 @@ namespace se.lth.control.labcomm { ...@@ -14,6 +15,7 @@ namespace se.lth.control.labcomm {
float decodeFloat(); float decodeFloat();
double decodeDouble(); double decodeDouble();
String decodeString(); String decodeString();
int decodePacked32();
} }
......
...@@ -68,7 +68,7 @@ namespace se.lth.control.labcomm { ...@@ -68,7 +68,7 @@ namespace se.lth.control.labcomm {
} break; } break;
case LabComm.STRUCT: { case LabComm.STRUCT: {
int fields = decodePacked32(); int fields = decodePacked32();
e.encodeInt(fields); e.encodePacked32(fields);
for (int i = 0 ; i < fields ; i++) { for (int i = 0 ; i < fields ; i++) {
e.encodeString(decodeString()); e.encodeString(decodeString());
collectFlatSignature(e); collectFlatSignature(e);
...@@ -108,10 +108,11 @@ namespace se.lth.control.labcomm { ...@@ -108,10 +108,11 @@ namespace se.lth.control.labcomm {
} }
private Int64 ReadInt(int length) { private Int64 ReadInt(int length) {
int result = 0; Int64 result = 0;
ReadBytes(buf, length); ReadBytes(buf, length);
for (int i = 0 ; i < length ; i++) { for (int i = 0 ; i < length ; i++) {
result = (result << 8) + buf[i]; result = (result << 8) + buf[i];
} }
return result; return result;
} }
...@@ -163,16 +164,13 @@ namespace se.lth.control.labcomm { ...@@ -163,16 +164,13 @@ namespace se.lth.control.labcomm {
} }
public int decodePacked32() { public int decodePacked32() {
TODO: Correct byteorder
Int64 res = 0; Int64 res = 0;
byte i = 0;
bool cont = true; bool cont = true;
do { do {
byte c = decodeByte(); Int64 c = decodeByte();
res |= (uint) ((c & 0x7f) << 7*i); res = (res << 7) | (c & 0x7f);
cont = (c & 0x80) != 0; cont = (c & 0x80) != 0;
i++;
} while(cont); } while(cont);
return (int) (res & 0xffffffff); return (int) (res & 0xffffffff);
......
...@@ -7,6 +7,7 @@ namespace se.lth.control.labcomm { ...@@ -7,6 +7,7 @@ namespace se.lth.control.labcomm {
void register(LabCommDispatcher dispatcher); void register(LabCommDispatcher dispatcher);
void begin(Type c); void begin(Type c);
void end(Type c); void end(Type c);
void encodeBoolean(bool value); void encodeBoolean(bool value);
void encodeByte(byte value); void encodeByte(byte value);
void encodeShort(short value); void encodeShort(short value);
......
...@@ -35,6 +35,7 @@ namespace se.lth.control.labcomm { ...@@ -35,6 +35,7 @@ namespace se.lth.control.labcomm {
public void end(Type c) { public void end(Type c) {
bytes.WriteTo(writer); bytes.WriteTo(writer);
bytes.SetLength(0); bytes.SetLength(0);
writer.Flush();
} }
private void WriteInt(Int64 value, int length) { private void WriteInt(Int64 value, int length) {
...@@ -46,7 +47,7 @@ namespace se.lth.control.labcomm { ...@@ -46,7 +47,7 @@ namespace se.lth.control.labcomm {
} }
public void encodeBoolean(bool value) { public void encodeBoolean(bool value) {
WriteInt(value ? 0 : 1, 1); WriteInt(value ? 1 : 0, 1);
} }
public void encodeByte(byte value) { public void encodeByte(byte value) {
...@@ -89,13 +90,16 @@ namespace se.lth.control.labcomm { ...@@ -89,13 +90,16 @@ namespace se.lth.control.labcomm {
} }
public void encodePacked32(Int64 value) { public void encodePacked32(Int64 value) {
Int64 tmp = value; byte[] tmp = new byte[5];
TODO: Correct byteorder Int64 v = value & 0xffffffff;
while(tmp >= 0x80) { int i;
encodeByte( (byte) ((tmp & 0x7f) | 0x80 ) );
tmp >>= 7; for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
tmp[i] = (byte)(v & 0x7f);
}
for (i = i - 1 ; i >= 0 ; i--) {
encodeByte((byte)(tmp[i] | (i!=0?0x80:0x00)));
} }
encodeByte( (byte) (tmp & 0x7f) );
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment