Irv: Re: For loop and a newbie
- Posted by Michael Sabal <mikes at notations.com> Mar 26, 2001
- 456 views
I've been doing this like this quite a bit lately. You're looking for a = chr() routine, but in Euphoria, we don't need one. The asc() routine is a = little more difficult, but Irv's example with value() will fill in nicely. For the moment, we'll assume your device is connected to LPT1: in a DOS = environment. All file pointers are atoms, so we'd open it for writing = using 2 lines of code, and a little error-checking: atom fp fp =3D open ("lpt1:","wb") -- If we're sending control codes,=20 -- it's better to us binary. if fp =3D -1 then abort(1) end if -- a file pointer of -1 indicates an = error. I'm going to use HP PCL5 control codes to demonstrate printing a 10x10 = black box at coordinate (100,100) on the paper: puts(fp,27&"*p100x100Y"&27&"*c10a10b0P") The ampersand (&) in Euphoria is for concatenation. Since every ASCII character is simply a number from 0 to 255, and a string is just an = array (i.e., sequence) of numbers; we just have to concatenate the = non-readable numbers to the string of readable numbers. I could have done = the same line a little lengthier using an intermediate sequence: constant RAWDATA =3D {27,"*p100x100Y",27,"*c10a10b0P"} -- I'm using a constant for demo, but this could just as easily come -- from the command line or a data file. sequence printstring printstring =3D "" -- No variable is pre-initialized in Euphoria. for ctr =3D 1 to length(RAWDATA) do -- except for-loop variables. printstring &=3D RAWDATA[ctr] end for puts(fp,printstring) -- I assume fp has been opened elsewhere=20 -- in the program. Also, don't forget to close any buffered data streams before you end your = program. Most spooled or queued printers won't print a single byte until = the stream is closed: close(fp) Hopefully this will answer a few of your questions. Michael J. Sabal >>> jc at cknet.net 03/26/01 11:21AM >>> This little routine (that I'd written years ago in pascal) is to send = control codes to printers or cash drawers.