c# - Using SetCommTimeouts on parallel port fails -


setcommtimeouts , getcommtimeouts function in kernel32 set , timeout when communicate devices.

now getcommtimeouts works me, setcommtimeouts returns error code 87 indicates parameter error.

now question whether setcommtimeouts works when talks parallel port?

if can fix it?

[dllimport("kernel32.dll")] private static extern bool setcommtimeouts(intptr hfile, ref lpcommtimeouts lpcommtimeouts); [dllimport("kernel32.dll ")] private static extern int createfile(string lpfilename, uint dwdesiredaccess, int dwsharemode, int lpsecurityattributes, int dwcreationdisposition, int dwflagsandattributes, int htemplatefile);  [structlayout(layoutkind.sequential)] private struct lpcommtimeouts {     public uint32 readintervaltimeout;     public uint32 readtotaltimeoutmultiplier;     public uint32 readtotaltimeoutconstant;     public uint32 writetotaltimeoutmultiplier;     public uint32 writetotaltimeoutconstant; } private const uint generic_write = 0x40000000; private const int open_existing = 3; phandler = createfile("lpt1", generic_write, 0, 0, open_existing, 0, 0); intptr hnd = new system.intptr(phandler); lpcommtimeouts lpcto = new lpcommtimeouts(); boolean bb = setcommtimeouts(hnd, ref lpcto); console.writeline(bb); // false here 

your declaration createfile() quite wrong , can never work in 64-bit mode. since don't of required error checking , keep plowing on, next call fail setcommtimeouts() call. complain getting bad handle value. make instead:

[dllimport("kernel32.dll", setlasterror = true, charset = charset.auto)] static extern intptr createfile(     string filename,     fileaccess desiredaccess,     fileshare sharemode,     intptr securityattributes,     filemode creationdisposition,     fileattributes flagsandattributes,     intptr templatefile); 

proper error handling looks this:

intptr hnd = createfile("lpt1", fileaccess.write, fileshare.none, intptr.zero,                          filemode.open, fileattributes.normal, intptr.zero); if (hnd == (intptr)-1) throw new system.componentmodel.win32exception(); 

additional failure modes machine not having lpt1 port, parallel ports went way of dodo long time ago. , parallel port driver have installed not supporting timeouts, used serial ports. ask vendor obtained parallel port hardware support if necessary.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -