Posted 18 Dec 2006 Hi one & all, I want to covert ascii to hex, add 1 then back to ascii. After I've scanned a barcode - 30160677KY, I want to add 1 , to achieve, following :- 30160678KY, 30160679KY, 30160680KY, and then print all four (4) barcodes. I require help with maths. thanks Smo Share this post Link to post Share on other sites
Posted 18 Dec 2006 We really need to know what plc you are using. This will help to provide the most correct answer. Share this post Link to post Share on other sites
Posted 19 Dec 2006 Hello Smo, As GT mentions, knowing the PLC you are using will help us. However, this may be complicated, because simply extracting the Bytes that you wish to manipulate and adding 1 to the converted number then converting back to ASCII will not increment the ASCII value as expected. For example, adding 1 to HEX 39 (ASCII = 9) will give HEX 40 (ASCII = @). This will need quite a bit of Byte manipulation and interrogation. With a bit of luck, you have CX-P v7.0, which now supports STRING manipulation within Structured Text in FBs, which may be the easiest way to handle this. Will investigate if I get time a bit later. Regards anonymous Share this post Link to post Share on other sites
Posted 19 Dec 2006 Ooops sorry the plc is a Omron CJ1M, CPU11-ETN, hope this helps.! smo Share this post Link to post Share on other sites
Posted 19 Dec 2006 All you would need to do is take the data memory location where the incrementing number would be and do a ++ operation. For example if you have 77 in your second to last DM, you could just add 1 and output the simple ASCII. I could be wrong, but the ladder isn't really "smart" enough to know what type of number is in a DM, so this should work.. The problem will be when you get to 100 or 1000 in that DM location... Good luck! Tig Share this post Link to post Share on other sites
Posted 20 Dec 2006 (edited) ... Ladder Code Approach: (Possible with any CJ1 series) 1. use the HEX(162) function to convert the ascii to a hex (BCD) number - the best result would be to extract a double precision (32bit) hex value (8 digit BCD value 2. using either +BL(401) or ++BL(591) functions create the three subsequent bar codes (BCD addition / increment) 3. use the ASCII(086) function to convert the BCD value back to ascii - note that some manipulation of data may be required to shift bytes etc depending on how ascii arrives into PLC memory, some study into operation of HEX/ASC functions will be required. see manual W340 section 3.12 for help with these functions. The Structured Text Function Block Approach (Possible with CX-Programmer V7) CJ1 CPU) as suggested by anonymous may be a good way to go, but will not work with the early V1,V2 (or V3?) CJ1 CPU's. The ladder code approach shouldn't be too difficult for this application. Nibroc Edited 20 Dec 2006 by Nibroc Share this post Link to post Share on other sites
Posted 20 Dec 2006 ... Example ladder code attached ... This code performs 'maths' as you describe ... Nibroc ASCii.cxp Share this post Link to post Share on other sites
Posted 20 Dec 2006 Nice one Nibroc, Don't know why, but I was looking at this far more complicatedly . (Spending waaaay too much time with text based languages recently!) Regards anonymous Share this post Link to post Share on other sites