Writefile method error serial port -
i'm trying write serial port com 1: (to atmega8 mcu) using escapecommfunction create file method, write file
dcb dcb = new dcb(); [dllimport("kernel32.dll")] static extern bool setcommstate(intptr hfile, [in] ref dcb lpdcb); [dllimport("kernel32.dll", setlasterror = true)] public static extern bool closehandle(intptr handle); [dllimport("kernel32.dll", setlasterror = true)] private static extern bool escapecommfunction(intptr hfile, int dwfunc); intptr porthandle; int setrts = 3; [dllimport("kernel32.dll", setlasterror = true, charset = charset.unicode)] private static extern intptr createfile(string lpfilename, system.uint32 dwdesiredaccess, system.uint32 dwsharemode, intptr psecurityattributes, system.uint32 dwcreationdisposition, system.uint32 dwflagsandattributes, intptr htemplatefile); [dllimport("kernel32.dll")] static extern bool writefile(intptr hfile, byte[] lpbuffer, uint nnumberofbytestowrite, out uint lpnumberofbyteswritten, [in] ref nativeoverlapped lpoverlapped); [structlayout(layoutkind.sequential, pack = 8)] public struct nativeoverlapped { private intptr internallow; private intptr internalhigh; public long offset; public intptr eventhandle; } nativeoverlapped overlap = new nativeoverlapped();
mainform load:
private void mainform_load(object sender, eventargs e) { try { porthandle = createfile("com1", 0x80000000 | 0x40000000, 0x00000000, intptr.zero, 4, 0x40000000, intptr.zero); dcb.baudrate = 9600; dcb.parity = 0; dcb.bytesize = 8; dcb.stopbits = 1; setcommstate(porthandle, ref dcb); messagebox.show(escapecommfunction(porthandle, 3).tostring()); } catch (exception ex) { messagebox.show(ex.message); } }
and i'm trying send data
private void btnsend_click(object sender, eventargs e) { lpnumberofbyteswritten = 0; byteslen = 0; byte[] message = new byte[11]; //creating message byteslen = (uint)message.length; messagebox.show(writefile(porthandle, message, byteslen, out lpnumberofbyteswritten, ref overlap).tostring()); }
escapecommfunction come true, writefile come false. wrong?
Comments
Post a Comment