Thursday, December 11, 2008

Printing with an Usb-Printer in .Net CF

During the last days, I tried to print with an Usb-printer from an Symbol/Motorola MC 70.

The .Net CF don't support printing like the full framework. OK, what should happen now, I had to fix that.

When I plugged the Usb-cable from the printer into the craddle while the MC 70 is in it, the operating system (Windows Mobile 5.0) creates a virutal port for the printer. The port is called "LPT1:".

That's OK for the beginning... I tried to open that port with System.IO.Ports.SerialPort, but it wouldn't work. At one time, I got an IOException without an reason, at another time, I got an IOException with a reason (the enum-value for the StopBits were not in the correct range(!?)). It wouldn't work. Damn...

So, I asked Google for a little help and I found an entry on Orkun Gedik had nearly the same problem a couple of years ago and so her created a workaround with a lot of P/Invokes for the old .Net CF v.1.0... Wow, that works still today. I am very interested in such things, so I looked in the source code... Hm... OK...Orkun tried it with a file, named like the portname...

Why that shouldn't work with System.IO.FileStream? I tried it and... BINGO... that works, too. Just create a new instance from the FileStream-object and send your data as a byte-array. Everything is done! Thank you, Orkun,