Thursday, 15 March 2012

c# - IO exception error when using serialport.open() -



c# - IO exception error when using serialport.open() -

final update our firmware whole time. embarrassing degree, i'm happy can move forwards , can set learning java off day. reply below.

update have more or less given on this. think bug goes downwards api have neither time, resources nor skill-set bottom of it. think there exists hardware whom windows gives middle finger. have downloaded eclipse, switched java , seek see if works. if not, you'll see me here. however, absolutely love solve , if has time or inclination dig deep one, i'd love see come with. checking here time time. please create sure '@' me in comments alerted.

original post

i know there few other people dealing issue, hoping help me. trying connect com port getting io exception when seek utilize serialport.open() command:

system.io.ioexception: parameter incorrect. @ system.io.ports.internalresources.winioerror(int32 errorcode, string str) @ system.io.ports.internalresources.winioerror() @ system.io.ports.serialstream.initializedcb(int32 baudrate, parity parity, int32 databits, stopbits stopbits, boolean discardnull) @ system.io.ports.serialstream..ctor(string portname, int32 baudrate, parity parity, int32 databits, stopbits stopbits, int32 readtimeout, int32 writetimeout, handshake handshake, boolean dtrenable, boolean rtsenable, boolean discardnull, byte parityreplace) @ system.io.ports.serialport.open() @ *programtitlehere.cs*:line 90

i using stellaris lm4f232 emulate com port. can open, access , results using termite (a terminal program) whenever seek visual studio won't connect , error. don't know error means , despite trying read elsewhere still sense lost.

can explain me happening here , maybe can begin seek figure out? can include more code, honest there isn't much there; properties of serialport device normal, , happening device (i can utilize msp430 no problem same details).

my code shown below people see (note 'sandbox', not actual programme symptoms identical):

seek { serialport1.portname = "com5"; serialport1.open(); if (serialport1.isopen == true) { textbox1.text = "connected"; } else { textbox1.text = "not connected"; } } grab (exception ex) { messagebox.show("error: " + ex.tostring(), "error"); }

and other settings done property manager (only difference baud set 230400, others on default). can open com4 (an msp430) intents , purposes identical device. can open com5 termite know connection good). , no, not trying open them @ same time. if need more info allow me know , can post more.

thanks!

edit: i'm on day 3 of trying figure out , still no luck. don't understand why can access com through terminal programme , not own when near can see there absolutely no difference. know of programme can 'examine' com port see properties of (besides windows manager mean)? i'm getting pretty frustrated , sort of @ stand still in project until figure out...

edit2: i've found apparent workaround, i've yet work here. few different io errors, @ to the lowest degree motion (not sure if progress). i've learned .net bug, has existed since 2.0. i'd still love help, if figure out study back. zach's code (the workaround linked above) shown below:

using system; using system.io; using system.io.ports; using system.runtime.interopservices; using system.text; using microsoft.win32.safehandles; namespace serialporttester { public class serialportfixer : idisposable { public static void execute(string portname) { using (new serialportfixer(portname)) { } } #region idisposable members public void dispose() { if (m_handle != null) { m_handle.close(); m_handle = null; } } #endregion #region implementation private const int dcbflagabortonerror = 14; private const int commstateretries = 10; private safefilehandle m_handle; private serialportfixer(string portname) { const int dwflagsandattributes = 0x40000000; const int dwaccess = unchecked((int) 0xc0000000); if ((portname == null) || !portname.startswith("com", stringcomparison.ordinalignorecase)) { throw new argumentexception("invalid serial port", "portname"); } safefilehandle hfile = createfile(@"\\.\" + portname, dwaccess, 0, intptr.zero, 3, dwflagsandattributes, intptr.zero); if (hfile.isinvalid) { winioerror(); } seek { int filetype = getfiletype(hfile); if ((filetype != 2) && (filetype != 0)) { throw new argumentexception("invalid serial port", "portname"); } m_handle = hfile; initializedcb(); } grab { hfile.close(); m_handle = null; throw; } } [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)] private static extern int formatmessage(int dwflags, handleref lpsource, int dwmessageid, int dwlanguageid, stringbuilder lpbuffer, int nsize, intptr arguments); [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)] private static extern bool getcommstate(safefilehandle hfile, ref dcb lpdcb); [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)] private static extern bool setcommstate(safefilehandle hfile, ref dcb lpdcb); [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)] private static extern bool clearcommerror(safefilehandle hfile, ref int lperrors, ref comstat lpstat); [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)] private static extern safefilehandle createfile(string lpfilename, int dwdesiredaccess, int dwsharemode, intptr securityattrs, int dwcreationdisposition, int dwflagsandattributes, intptr htemplatefile); [dllimport("kernel32.dll", setlasterror = true)] private static extern int getfiletype(safefilehandle hfile); private void initializedcb() { dcb dcb = new dcb(); getcommstatenative(ref dcb); dcb.flags &= ~(1u << dcbflagabortonerror); setcommstatenative(ref dcb); } private static string getmessage(int errorcode) { stringbuilder lpbuffer = new stringbuilder(0x200); if ( formatmessage(0x3200, new handleref(null, intptr.zero), errorcode, 0, lpbuffer, lpbuffer.capacity, intptr.zero) != 0) { homecoming lpbuffer.tostring(); } homecoming "unknown error"; } private static int makehrfromerrorcode(int errorcode) { homecoming (int) (0x80070000 | (uint) errorcode); } private static void winioerror() { int errorcode = marshal.getlastwin32error(); throw new ioexception(getmessage(errorcode), makehrfromerrorcode(errorcode)); } private void getcommstatenative(ref dcb lpdcb) { int commerrors = 0; comstat comstat = new comstat(); (int = 0; < commstateretries; i++) { if (!clearcommerror(m_handle, ref commerrors, ref comstat)) { winioerror(); } if (getcommstate(m_handle, ref lpdcb)) { break; } if (i == commstateretries - 1) { winioerror(); } } } private void setcommstatenative(ref dcb lpdcb) { int commerrors = 0; comstat comstat = new comstat(); (int = 0; < commstateretries; i++) { if (!clearcommerror(m_handle, ref commerrors, ref comstat)) { winioerror(); } if (setcommstate(m_handle, ref lpdcb)) { break; } if (i == commstateretries - 1) { winioerror(); } } } #region nested type: comstat [structlayout(layoutkind.sequential)] private struct comstat { public readonly uint flags; public readonly uint cbinque; public readonly uint cboutque; } #endregion #region nested type: dcb [structlayout(layoutkind.sequential)] private struct dcb { public readonly uint dcblength; public readonly uint baudrate; public uint flags; public readonly ushort wreserved; public readonly ushort xonlim; public readonly ushort xofflim; public readonly byte bytesize; public readonly byte parity; public readonly byte stopbits; public readonly byte xonchar; public readonly byte xoffchar; public readonly byte errorchar; public readonly byte eofchar; public readonly byte evtchar; public readonly ushort wreserved1; } #endregion #endregion } internal class programme { private static void main(string[] args) { serialportfixer.execute("com1"); using (serialport port = new serialport("com1")) { port.write("test"); } } } }

edit3: day 6: i'm still plugging away @ this. water rations low still struggle on. sense help must certainly on horizon. whoever finds journal bring remains canada , find nicole. tell love her.

but seriously, have no thought causing problem. i'm wondering if purely on embedded side; maybe because usb-to-go, or because device capable of beingness host also. has run problem? doesn't explain why can utilize termite (a terminal program, viewers joining us) though. have been trying find open source terminal programme a) works , b) see a). per usual, study if find issue here have found countless forums sounds people have had issue dating 2006.

edit4: per advice given, downloaded port monitoring software app (i got eltima serial port monitor) , baud issue:

but strangely no matter baud set still fails. , can explain up/down thing means? tried googling keywords general. usual, maintain reporting changes.

also, record, can connect using eltima @ baud of 115200 (same termite). unfortunately not work in visual studio.

edit5: our plot takes surprise twist. monitoring happens when termite connects com port in question , blam! termite throws exact same error program, ignores it. genius, right? sloppy, works. need larn how ignore ioexceptions. i'll study when figured out.

edit6: turns out baud rate issue, goes deeper. have been using eltima serial port monitoring software , intuitive , easy use. recommend it. after some research have learned cannot ignore exception , still connect serial port using .nets library. have go deeper win32 api , write own. have found few pages touch on this, honest have never done before may while before study figure out , everyone. there way many suffer problem. have found quite few forums , websites can see exact same symptoms nobody has done much besides 'yeah, .net sucks'. plan on writing total static library class , publish either on website, here , wherever else can. .net take notice (this bug has existed since 2.0). i'll study when done!

edit7: lot harder thought be.

edit8: don't know if next or not wanted still @ out of town week on business trip. i'm still happy hear suggestions , alternative ideas though!

this comes serial port driver, unhappy 1 of settings. baudrate beingness candidate, drivers tend allow 115200. albeit should not restriction when dedicated can bus product.

the best way tackle using sysinternals' portmon utility, can see beingness sent driver. observe terminate first, that's known-to-work baseline. tinker serialport properties until initialization commands, see them in portmon, sent programme matches termite's. values, not order. if doesn't pan out either take parking lot , on auto several times , purchase brand.

update: looks baudrate problem. that's issue in .net, not going ignore driver's error homecoming code terminal emulator programs do. actual value should not matter since talking emulated serial port. there possible issue can-bus speed, rates variable , isn't clear me how negotiated. tended done dip switches in olden days, may driver wants specify speed through baudrate setting. there ought on box or in manual. typical speeds 40, 250 or 500 kbps. manufacturer know, give them call.

c# winapi serial-port ioexception baud-rate

No comments:

Post a Comment