Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: CReese on July 08, 2013, 06:48:48 PM

Title: Strings and ascii encoding
Post by: CReese on July 08, 2013, 06:48:48 PM
Hello again.

I've got a string struct that I'd like to convert into a number that I can publish to a MHR register, then read it from a screen and interpret as a string.

How do I suitably encode my string? STR2INT does not seem to be what I'm looking for, and PUBLISH doesn't appear to take string inputs.

Thanks,
C
Title: Re: Strings and ascii encoding
Post by: BobO on July 08, 2013, 07:54:19 PM
Use STRGETB to copy raw bytes out of the string. We are looking at adding some string friendly Modbus master instructions, but that will be down the road.
Title: Re: Strings and ascii encoding
Post by: plcnut on July 09, 2013, 07:19:08 AM
You can also use STRSUB to extract the data from the string, and then STR2INT to convert it. Take a look in the sample programs: http://forum.hosteng.com/index.php/topic,1013.0.html
at Bobo's http client example to see how it is implemented.
Title: Re: Strings and ascii encoding
Post by: BobO on July 09, 2013, 10:07:22 AM
Don't know for sure what he is actually needing, but he stated that he wanted to publish to an MHR a value that would be interpreted as a string. I took that to mean ASCII data, which it is already, so should just require extraction from the string. But there are a dizzying array of ways and formats with numeric and/or string data, so I could be very wrong. I'm sure Do-more can handle it though. ;)
Title: Re: Strings and ascii encoding
Post by: plcnut on July 09, 2013, 10:11:05 AM
and interpret as a string.
Ummm, that little line COULD make a little bit of a difference, couldn't it?!
 ::)
Title: Re: Strings and ascii encoding
Post by: CReese on July 09, 2013, 10:40:32 AM
Bob was right. Now I just need my screen to play nice. Are we doing one byte per character?
Title: Re: Strings and ascii encoding
Post by: CReese on July 09, 2013, 10:47:48 AM
I'm a little confused. Typically you would use a byte per character. However, the MHR registers are signed words that are 15 bits plus a sign byte. It looks, however, as if I'm getting two characters into each MHR register. How does this work?
Title: Re: Strings and ascii encoding
Post by: plcnut on July 09, 2013, 11:10:10 AM
Each character uses 1 byte, each MHR is 1 Word (2 bytes in a word).
If you only want 1 character per word then use casting ie: MHR:B0 or MHR:B1
Go to the Element browser and click the "More" button at the bottom to see the options.
Title: Re: Strings and ascii encoding
Post by: b_carlton on July 09, 2013, 11:11:54 AM
it seems like there is some confusion all around.
Quote
I've got a string struct
Does this contain the ASCII representation of a number?

If it is a number do you want an HMI
Quote
then read it from a screen
to be the final destination of this information?

It seems like you were suggesting using STR2INT (SSx -> Dx) but said
Quote
does not seem to be what I'm looking for
. Why? Did it not work?

Quote
PUBLISH doesn't appear to take string inputs
Why are you talking about strings at this point? Wouldn't you PUBLISH (Dx -> MHRx).

Then the 'screen' would use the Modbus protocol to read MHRx and display its value.

Title: Re: Strings and ascii encoding
Post by: BobO on July 09, 2013, 11:17:42 AM
I'm a little confused. Typically you would use a byte per character. However, the MHR registers are signed words that are 15 bits plus a sign byte. It looks, however, as if I'm getting two characters into each MHR register. How does this work?

MHR registers are 16 bit two's complement...not 15 bit plus sign. But the integer encoding isn't relevant when the memory is being reinterpreted as string. The key point is 16 bit = 2 bytes = 2 characters.
Title: Re: Strings and ascii encoding
Post by: CReese on July 09, 2013, 11:36:34 AM
it seems like there is some confusion all around.
Quote
I've got a string struct
Does this contain the ASCII representation of a number?

If it is a number do you want an HMI
Quote
then read it from a screen
to be the final destination of this information?

It seems like you were suggesting using STR2INT (SSx -> Dx) but said
Quote
does not seem to be what I'm looking for
. Why? Did it not work?

Quote
PUBLISH doesn't appear to take string inputs
Why are you talking about strings at this point? Wouldn't you PUBLISH (Dx -> MHRx).

Then the 'screen' would use the Modbus protocol to read MHRx and display its value.


The data are string format within the plc. STR2INT would be useful if I had a string representation of a numerical value I wished to manipulate, e.g. '123456' within SS0 or otherwise. That is not the case. I need to take string format strings, encode them into ASCII byte representations, and then have the screen read the data from the MHR registers.
Title: Re: Strings and ascii encoding
Post by: CReese on July 09, 2013, 11:39:20 AM
Last question: what is the easiest way to reverse the bytes within each of the word (convert to reverse ASCII)? My screen is reading strings like '01234567' as '10325476' and it has no option to reverse on read.

Thanks guys!
C
Title: Re: Strings and ascii encoding
Post by: plcnut on July 09, 2013, 11:40:24 AM
SWAPB
Title: Re: Strings and ascii encoding
Post by: CReese on July 09, 2013, 11:51:45 AM
Rather than do that byte by byte, I used an intermediate ascii name block, then reversed using PUBLISH to the MHR registers. Triple the required memory, but I'll clean it up later if necessary.
Title: Re: Strings and ascii encoding
Post by: BobO on July 09, 2013, 11:59:10 AM
SWAPB does a range, but would still require the source and destination to be different. It's pretty much a wash.

Part of our task to look into better string handling in Modbus was also to consider how we could do a better job of simplifying what you are doing right now. It seems to be a pretty common theme, and I'm sure we could hack together an extension to PUBLISH/SUBSCRIB or a dedicated instruction to move bytes from strings into other memory and swap bytes as required. But for now, it is a 'n' step process.
Title: Re: Strings and ascii encoding
Post by: fluidpowerman on July 09, 2013, 05:51:29 PM
I am trying to do something similar using a C-more HMI to DO-more to Kepdirect and out to Datanet to create a data base that I can print forms etc.  I want to be able to enter a number of Text/number entry fields (ASCII Strings)ie. Customer Name, customer address, Job number etc. I have no problem going from ASCII to SS memory in the Do-More, but converting the SS memory to DLV memory that I can use with KEPTDIRECT.....oh boy. I am somewhat familiar with the STRGETB, however the number of characters in each field can vary, I'm not sure how to structure my code.   Does anyone know if there is a sample program or an easier what to do this.

I really appreciate any help and input

Thank you
Bryan :)
Title: Re: Strings and ascii encoding
Post by: Controls Guy on July 09, 2013, 06:32:05 PM
For variable length strings, you can prefill the target string with nulls or spaces before writing your real string.  Then either your HMI will be smart enough to see the first null or space and stop displaying or else it will just "display" them as blank space.  That usually works fine, or you might need to terminate the strings with an 0x0D, 0x0A or both.  Look at STRTRIM and STRFIND.  Those may be of use to you.  STRTRIM doesn't trim nulls at the moment, but will from v1.1 I believe.
Title: Re: Strings and ascii encoding
Post by: plcnut on July 09, 2013, 07:20:15 PM
You can check the string length with SS0.length etc. and be able to know how many characters/bytes you are dealing with.
Title: Re: Strings and ascii encoding
Post by: CReese on July 10, 2013, 10:34:42 AM
Short answer is that it is not a lot of fun.

If you have strings of length N in a string field:

Set up a target ascii converted block, e.g. 'stringascii'. Make it type signed word. It will need to be of length N/2 words.

Clear out the ascii target using SETNUMR with 0 value.

STRGETB your string into your ascii block.

If you need to reverse ascii (as I did for my HMI), you can publish using reverse bytes to an MHR register, or SWAPB to reverse the bytes in each word.

I iterated over this with a loop to convert a long string block into a long word block, and then just pointed my HMI to the correct offsets (N/2 for each string) in the MHR registers.

HTH,
C
Title: Re: Strings and ascii encoding
Post by: fluidpowerman on July 10, 2013, 02:27:28 PM
Thanks for the input.....I will give it a shot.