1

I've modified an example to send & receive from serial, and that works fine.

The device I'm connecting to has three commands I need to work with. My experience is with C.


MAP - returns a list of field_names, (decimal) values & (hex) addresses I can keep track of which values are returned as decimal or hex. Each line is terminated with CR :: Example:

MEMBERS:10 - number of (decimal) member names

NAME_LENGTH:15 - (decimal) length of each name string

NAME_BASE:0A34 - 10 c-strings of (15) characters each starting at address (0x0A34) (may have junk following each null terminator)

etc.


GET hexaddr hexbytecount - returns a list of 2-char hex values starting from (hexaddr).

The returned bytes are a mix of bytes/ints/longs, and null terminated c-strings terminated with CR :: Example::

get 0a34 10 -- will return

0A34< 54 65 73 74 20 4D 65 20 4F 75 74 00 40 D3 23 0B

This happens to be 'Test Me Out'(00) followed by junk etc.


PUT hexaddr hexbytevalue {{value...} {value...}} sends multiple hex byte values separated by spaces starting at hex address, terminated by CR/LF

These bytes are a mix of bytes/ints/longs, and null terminated c-strings :: Example:

put 0a34 50 75 73 68 - (ascii Push)

Will replace the first 4-chars at 0x0A34 to become 'Push Me Out'

SAVED OK

Servy
  • 202,030
  • 26
  • 332
  • 449
M C
  • 61
  • 7

1 Answers1

0

See my answer previously about serial handling, which might be useful Serial Port Polling and Data handling

to convert your response to actual text :-

var s = "0A34 < 54 65 73 74 20 4D 65 20 4F 75 74 00 40 D3 23 0B";

var hex = s.Substring(s.IndexOf("<") + 1).Trim().Split(new char[] {' '});
var numbers = hex.Select(h => Convert.ToInt32(h, 16)).ToList();
var toText = String.Join("",numbers.TakeWhile(n => n!=0)
    .Select(n => Char.ConvertFromUtf32(n)).ToArray());
Console.WriteLine(toText);

enter image description here

which :- skips through the string till after the < character, then splits the rest into hex string

then, converts each hex string into ints ( base 16 )

then, takes each number till it finds a 0 and converts each number to text (using UTF32 encoding)

then, we join all the converted strings together to recreate the original text

alternatively, more condensed

var hex = s.Substring(s.IndexOf("<") + 1).Trim().Split(new char[] {' '});
var bytes = hex.Select(h => (byte) Convert.ToInt32(h, 16)).TakeWhile(n => n != 0);
var toText = Encoding.ASCII.GetString(bytes.ToArray());

for converting to hex from a number :-

Console.WriteLine(123.ToString("X"));
Console.WriteLine(123.ToString("X4"));
Console.WriteLine(123.ToString("X8"));
Console.WriteLine(123.ToString("x4"));

enter image description here

also you will find playing with hex data is well documented at https://msdn.microsoft.com/en-us/library/bb311038.aspx

Community
  • 1
  • 1
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • Much appreciated Keith - I need to plug this in and see what I learn! Yes - generating the hex shouldn't be so difficult learning new function calls!. Just starting from scratch on everything at once is something I haven't dome for a (few) years! – M C Dec 14 '16 at 23:25
  • These are what I have to learn!! SO many pre-canned functions in NET – M C Dec 15 '16 at 01:39