View Full Version : Passing Byte Array From Gateway
mwhitley
07-01-2008, 01:47 PM
I'm trying to talk between C# and an end device in hex (byte array). I want to have that node "print" the byte array as is (not converted to ASCII) over the UART.
I have a function on the node from another post on the forum
def toSmartDevice(dataStr):
# characters have come in over the radio
# send them out to the "smart device" on UART0
print dataStr,
pulsePin(1,100,True)If I call this from Portal with an argument of "\xAA\xBB\xCC" it will send out those hex values (not ASCII) over the UART.
I'm just flashing an LED to let me know I'm sending data.
However, if I call this from C# using
proxy.BeginRpc(this.getSnapNetAddr(), new byte[] { Convert.ToByte("00", 16), Convert.ToByte("14", 16), Convert.ToByte("59", 16) },
"toSmartDevice",
new object[] { "\xAA\xBB\xCC" },
RpcCallback);I think the node is intrepreting this as a string, not three bytes. Can you help me out with type conversion?
mgenti
07-01-2008, 02:43 PM
Yes, you are correct it is trying to pass it as a string instead of a byte array. An easy way to create a byte array to pass is just like we do when we pass the node's network address:
new byte[] { Convert.ToByte("00", 16), Convert.ToByte("14", 16), Convert.ToByte("59", 16) }
If you have a C# string with non-printable ASCII characters than you can use this method to get an array of bytes:
System.Text.Encoding enc = new System.Text.UTF8Encoding();
enc.GetBytes(myString);
mwhitley
07-01-2008, 03:34 PM
I tried your method to create a byte array (like the node address) using
proxy.BeginRpc(this.getSnapNetAddr(), new byte[] { Convert.ToByte("00", 16), Convert.ToByte("14", 16), Convert.ToByte("59", 16) },
"toSmartDevice",
new object[] { new byte[] { Convert.ToByte("AA", 16), Convert.ToByte("BB", 16), Convert.ToByte("CC", 16) }},
RpcCallback); but the LED doesn't flash when I issue that command, so it's telling me that the node doesn't know what to do when the function is called with a byte array.
If I issue this RPC
proxy.BeginRpc(this.getSnapNetAddr(), new byte[] { Convert.ToByte("00", 16), Convert.ToByte("14", 16), Convert.ToByte("59", 16) },
"toSmartDevice",
new object[] { "hello" },
RpcCallback);then the LED flashes, so I know that I'm calling the right function, it's just something to do with the argument type.
Thanks for the tip on converting a C# string with non-printables to a byte array.
mgenti
07-01-2008, 03:45 PM
Can you email me your log file from the SNAP Gateway so I can look through it to see if there are any errors when you call this function?
mwhitley
07-01-2008, 04:08 PM
I cleared out the SnapGatewayTrace file and then ran the code with the byte array (which didn't work) then the "hello" which did work. The file contents are below.
15:07:06:230 INFO Gateway Now listening for XML-RPC connections on port 8080
15:07:22:013 DEBUG async.AsyncXMLRPCServer RPC connection from ('127.0.0.1', 3786)
15:07:22:168 DEBUG async.AsyncXMLRPCServer sending response: <?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><boolean>1</boolean></value>
</param>
</params>
</methodResponse>
15:07:23:076 DEBUG async.AsyncXMLRPCServer RPC connection from ('127.0.0.1', 3787)
15:07:24:250 DEBUG async.AsyncXMLRPCServer RPC connection from ('127.0.0.1', 3788)
15:07:24:250 DEBUG async.AsyncXMLRPCServer sending response: <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string><type 'exceptions.TypeError'>:unhashable instance</string></value>
</member>
</struct></value>
</fault>
</methodResponse>
15:08:01:963 DEBUG async.AsyncXMLRPCServer Closing connection
15:08:20:891 DEBUG async.AsyncXMLRPCServer RPC connection from ('127.0.0.1', 3789)
15:08:21:032 DEBUG async.AsyncXMLRPCServer sending response: <?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><boolean>1</boolean></value>
</param>
</params>
</methodResponse>
15:08:22:502 DEBUG async.AsyncXMLRPCServer RPC connection from ('127.0.0.1', 3790)
15:08:24:239 DEBUG async.AsyncXMLRPCServer RPC connection from ('127.0.0.1', 3791)
15:08:24:239 DEBUG async.AsyncXMLRPCServer sending response: <?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><boolean>1</boolean></value>
</param>
</params>
</methodResponse>
admin
07-01-2008, 05:07 PM
I believe this is the same issue that has been affecting a lot of people lately with the SNAP Gateway 2.0.31 which has to do with non-printable ASCII characters. I will see about updating the beta version the SNAP Gateway.
mwhitley
07-01-2008, 09:10 PM
The beta version of gateway cleared up the problem - thanks!
vBulletin® v3.8.0, Copyright ©2000-2012, Jelsoft Enterprises Ltd.