Click here to Skip to main content
11,494,851 members (58,911 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi All (Original Griff especially),

I have been asking a lot of Questions about how to parse a string that has been returned from a device, I have now figured out what to do (I hope!). It just the exact of how. I now can send and recieve low values correctly I am starting to suspect it is the way I am packing the rest of the byte as strange things happen with the magic numbers to my hex value, 255 or FF fine, 256 or 100 is read as 16.

This is due to the way the processor behaves below is the send reply from sending it 255 which I read back as 255
Quote:
0c FF 00 00 00 #0
#3B025301 00000001 0
0c #FF000000

Below is the responce for the value 256 or 100
Quote:
i #3B025301 00000001 0
0c #10000000

The issue is with the way the data presented I sat down with the idea that it was ( 8 bit heading (normal) 87 65 43 21 read as 21 43 65 87 which makes sense for low value as the first bit data I tried it with was 25 (base 10)
87 65 43 21: read back as: I took the headings to be 21 43 65 87
00 00 00 19 (25 in hex) 19 00 00 00 19 00 00 00

I have as I was typing this question was playing and found the problem was with the right of the value. The problem is my method for parsing the hex number is
  Value_Int = int.Parse(txtValueCredit.Text, System.Globalization.NumberStyles.HexNumber);
As the unit replies with the value in a string that is eight characters long. but in an odd format the biggest the number can be is 10,000 dec or 2710 hex, I am going over the string with a sub string chopping off the zero, which could lead to problems.
Also ordering string wrong 257 is #01010000 not #10100000
hyperTerm <</cw 0c 01 01 00 00>> prog <</cw 0c 01 01 00 00>>
I think I am going nuts. As I keep seeing what I think is the problem and then finding its not.
So either the problem is I am not writing the string correctly for values over 255, but this appeared to correct the problem Converting Number to Hex with complications[^]
or I am not reading the reply correctly String builder problems....[^]
If I manually set the value to 256 I get the echo in a spy window of #00010000 If then read this back with my software I get #00010000 If I try to set 256 with my code I get #10000000 or 16
I think the problem therefor is in the sending routine ? Sorry if this is a little large and involved for a Q&A question I was usning this as white board.
Glenn
Posted 29-Jan-13 3:38am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Glenn, looking at your data, I suspect it's a sending problem (and a reciveing problem)
0c FF 00 00 00 #0       This is from you to the card?
#3B025301 00000001 0    Response - not sure what it decodes to exactly, but I assume "OK" with some info.
0c #FF000000            I assume this is the return?
If so, then I would send hex 123 as:
0c 23 01 00 00 #0
as the data looks to be 32 bit little endian (rather than PC which is big endian, so the lowest byte is on the RHS)

If so, it would explain why you are having problems - is there any way you can check? Or point me at the interface specification manual?


"Hi,
Just sent 123(hex) as 0c 23 01 00 00 with the command /cw 0c 23 01 00 00 via HyperTerm got a #0 when I read it with a cr I get 23010000(hex) I read in as 23010000 which I tranlate to 00000123 parse to 291(dec) Meaning my reading function is OK. When I send I am sending 291 (dec) is 0c 12 30 00 00 meaning my send is wrong. I need to go back to the string parsing thanks."



If all you need is little endian <--> integer, then that's pretty easy:
        public static string IntToLEString(int i)
            {
            StringBuilder sb = new StringBuilder(4 * 2 + 3);
            sb.AppendFormat("{0:X2} ", ((i >> 00) & 0xFF));
            sb.AppendFormat("{0:X2} ", ((i >> 08) & 0xFF));
            sb.AppendFormat("{0:X2} ", ((i >> 16) & 0xFF));
            sb.AppendFormat("{0:X2} ", ((i >> 24) & 0xFF));
            return sb.ToString();
            }
        public int LEStringToInt(string s)
            {
            int result = 0;
            string byte0 = s.Substring(0, 2);
            string byte1 = s.Substring(3, 2);
            string byte2 = s.Substring(6, 2);
            string byte3 = s.Substring(9, 2);
            result = int.Parse(byte0, NumberStyles.HexNumber) |
                     int.Parse(byte1, NumberStyles.HexNumber) << 8 |
                     int.Parse(byte2, NumberStyles.HexNumber) << 16 |
                     int.Parse(byte3, NumberStyles.HexNumber) << 24;
            return result;
            }
I'd probably want to put some error checking in, but you are probably doing that to recognise the response already. (You may need to ignore the spaces on the returned values as well, but that's pretty easy)
  Permalink  
v2
Comments
glennPattonWork at 29-Jan-13 9:09am
   
Interface Specification manual...you jest sir. All I have is the Interface Documentation (really a PDF with some commands and responce) which really isn't enough and some PSION OPL code which is very hard to understand and is not the version running on the PSION. Customer shouting at Boss, Boss yelling via Email (he out of the country 'til next week) If you give me a web address I can upload the interface doc and the OPL code. They are in the public folder on my DropBox account ,I just can't get the web link working! or I could share them with you directly ?? Glenn I will give it another go!!
glennPattonWork at 29-Jan-13 9:16am
   
The Interface Definition Document:
https://dl.dropbox.com/u/66257998/INS%201003%20A%20-%20Psion%20Operation%20Guide.pdf
glennPattonWork at 29-Jan-13 9:16am
   
The OPH code:

The Opl Code https://dl.dropbox.com/u/66257998/Smeter.oph
glennPattonWork at 29-Jan-13 9:16am
   
https://dl.dropbox.com/u/66257998/Smeter.opl
glennPattonWork at 29-Jan-13 9:18am
   
The OPL code
https://dl.dropbox.com/u/66257998/Smeter.opl
glennPattonWork at 29-Jan-13 9:18am
   
You now have what I have docs wise!
OriginalGriff at 29-Jan-13 9:37am
   
Hm. Liked documentation, didn't they? :laugh:
Liked commenting as well I see...Ok, I'm having a wade through, but never seen OPL before so this may take a while...
Can you give me any clues? Like what you thing the data you are sending in the question is? And what you think the response is?
glennPattonWork at 29-Jan-13 10:20am
   
Sorry, for that our mail server went nuts didn't see your reply. First time I used drop box for sharing stuff looks like it worked. As for OPL I hadn't seen it haven't got a compiler for it so I think it works but can't say. The stuff in the reply from the PSION handheld is as your comments say it might be "OK" or more interstingly "I have found life on Mars", if you read the below reply I am trying now to put into an array and reorder that! :)
glennPattonWork at 29-Jan-13 9:54am
   
Hi,
Just sent 123(hex) as 0c 23 01 00 00 with the command /cw 0c 23 01 00 00 via HyperTerm got a #0 when I read it with a cr I get 23010000(hex) I read in as 23010000 which I tranlate to 00000123 parse to 291(dec) Meaning my reading function is OK. When I send I am sending 291 (dec) is 0c 12 30 00 00 meaning my send is wrong. I need to go back to the string parsing thanks.
OriginalGriff at 29-Jan-13 10:57am
   
Answer updated
glennPattonWork at 29-Jan-13 11:10am
   
If pass IntToLEString(); 123 an integer it complains about "NumberStyles" not existing in the current context. Also if I pass it a string it complains about expecting an int, which is why I sending it an int.
OriginalGriff at 29-Jan-13 11:21am
   
Put the cursor in the name "NumberStyles" - a small blue bar will appear at the beginning. Hover the mouse over the blue bar, and a drop-down will appear. Open the drop down, and select the "using..." option. It will add the necessary using statement to the top of your file:
using System.Globalization;
glennPattonWork at 29-Jan-13 11:33am
   
Hmm, I do see the drop down but added System.Globalization; and the WTF wiggly line disappeared still can't get it to do anything though!
OriginalGriff at 29-Jan-13 11:40am
   
Did you try calling it:
string s = IntToLEString(0x12345678);
int i = LEStringToInt(s);
glennPattonWork at 29-Jan-13 12:29pm
   
I have it working! hex to int, I am pretty sure if you gave it int j = LEStringToInt("12345678");
textBox1.Text = j.ToString();
string s = IntToLEString(j);
textBox2.Text = s.ToString();
it would choke, that is chore for tomorrow but accuratly one way!! hex to dec!! just need to go dec to hex and see if that cures my problem!!!
OriginalGriff at 29-Jan-13 13:59pm
   
:thumbsup:!

Go home, have a drink and try to forget it! :laugh:
glennPattonWork at 29-Jan-13 16:29pm
   
Hmmm, gone home could forget about and found the following: //get it swapping ints to hex sring just //needs spaces ie. 10 27 00 00
int j = LEStringToInt("10 27 00 00"); textBox1.Text = j.ToString();
string s = IntToLEString(j);
textBox2.Text = s.ToString();
Now I can sleep thank you! Cheers for that I will try it tomorrow with the hardware thanks. Glenn //int j = LEStringToInt(s);
glennPattonWork at 30-Jan-13 6:34am
   
Thanks alot, I now have it reading and writing correctly, can I ask do you think the details were a little sparse of what the system should do and how to do it?
Glenn
OriginalGriff at 30-Jan-13 6:44am
   
Just a bit sparse, yes... :laugh:
Some kind of operations manual or the original H/W spec would have helped. All you seem to have is the Users guide (which for writing a replacement is ok for the man / machine interface but useless for the software / hardware interface) and some poorly written, badly commented, undocumented code in a language you have never used.

I've worked with worse, but it always takes far, far longer than it should because you have to try and puzzle it out from first principles...

Do you even have the circuit diagrams? So you could at least ID what hardware datasheets to look at?
glennPattonWork at 30-Jan-13 6:54am
   
Data Sheets...the thing is I think this system is in for a complete redesign once we get around to it. The problem has been it uses some out date NEC processor which I would like to replace MSP430 (I like them!), mind you the best part was prizing open the first of these units and the boss reply of "it uses a (can't remember!), oh <<expletive_deleted>> its an NEC I broke the FPIP last time I used it" then time spent repairing the FPIP. the rest of the time it was 'hmm thats not on the diagram' time!
Glenn
OriginalGriff at 30-Jan-13 7:27am
   
So the best thing to do is document everything as you go along - at least it tells you what it did when you (finally) get the redesign approved. Normally when the current hardware can't be built any more and they panic about sales...
glennPattonWork at 30-Jan-13 7:33am
   
My code tends to be a mess of commented out blocks, comments
along the lines reverse this string before use, monty python & star trek quotes. I take it from some of your answers, we are in similar roles, I am an Electronics Engineer that was made the mistake of not ducking fast enough when the phrase "We need a Windows Front End for this" was mentioned!
Glenn
OriginalGriff at 30-Jan-13 7:54am
   
Just a suggestion, but back it up, rip out the commented out rubbish, take out the Trekky stuff and test that.
Then document what you have (in the source is fine) as to why it does what it does.

Think about it. You are "the man with experience with this" now.
So in a years time, when they go into a blind panic to create a replacement in no time at all, who do you think they are gonna call? 'Coz it ain't gonna be Ghostbusters! :laugh:

A bit of time now, can save you weeks of headache later. :)
glennPattonWork at 30-Jan-13 8:02am
   
Don't worry before it goes out the door I take all that stuff out and upload it to our SVN server, mind you I also try to keep all the versions I do on my machine to try to keep my thoughts intact as I have looked at code I have written (mostly embedded stuff) and though it works, but why??
glennPattonWork at 29-Jan-13 11:00am
   
Thanks for that. I am starting to talk to talk to my power supply!
OriginalGriff at 29-Jan-13 11:04am
   
I hope that's a euphemism! :laugh:
glennPattonWork at 29-Jan-13 11:11am
   
No its a nice chunky Tenma 30 amp job, I wonder where it came from!
OriginalGriff at 29-Jan-13 11:24am
   
30A variable? Over what range?

The highest I ever got anyone to buy me was a 15A 0-30V single output with +5V@ 1A...:sigh:
I don't even have an oscilloscope these days...
glennPattonWork at 29-Jan-13 11:30am
   
Well if I can't get this working I might see some how, I don't think It will be very high as it's only got girly bananna plugs on it!, I lost my scope and white board when we moved!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 Sergey Alexandrovich Kryukov 350
2 CHill60 318
3 Sascha Lefèvre 280
4 Nakul Chaudhari 236
0 Sergey Alexandrovich Kryukov 10,297
1 OriginalGriff 8,821
2 Sascha Lefèvre 3,864
3 Maciej Los 3,412
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100