c# - Serial ports - how do I set characters? -
c# - Serial ports - how do I set characters? -
consider:
baud rate 19200 rts on dtr on info bits=8, stop bits=1, parity=none set chars: eof=0x00, error=0x2a, break=0x2a, event=0x00, xon=0x11, xoff=0x13 handflow: controlhandshake=(dtr_control), flowreplace=(transmit_toggle, rts_control), xonlimit=0, xofflimit=4096
ok, using port scanner i've found usb device needs these settings facilitate import. can recreate of these follows:
port = new serialport("com4"); port.dtrenable = true; port.rtsenable = true; port.handshake = handshake.none; port.baudrate = 19200; port.stopbits = stopbits.one; port.parity = parity.none; port.databits = 8; port.open(); byte[] = new byte[2] { 0x0 , 0x1 }; port.write(a, 0, 1); port.write(a, 0, 1); port.write("mem"); port.write("mem"); string output = port.readexisting(); system.diagnostics.debug.writeline("found: " + output);
however, codes produced these:
set chars: eof=0x1a, error=0x00, break=0x00, event=0x1a, xon=0x11, xoff=0x13 xonlimit=1024, xofflimit=1024
how alter x limits, , each of character codes has chance of working?
the post serialport 0x1a character reading problem closest thing i've found far, don't understand it.
you can add together extension serialport in c# - see xon/xoff values in net2.0 serialport class.
for other fields can change:
dcbtype.getfield("xonchar"); // "xonchar", "xoffchar", "errorchar", "eofchar", "evtchar"
code:
using system; using system.componentmodel; using system.io.ports; using system.reflection; using system.runtime.interopservices; using system.security; using system.security.permissions; using microsoft.win32.safehandles; class programme { static void main(string[] args) { using (var port = new serialport("com1")) { port.open(); port.setxonxoffchars(0x12, 0x14); } } } internal static class serialportextensions { [securitypermission(securityaction.linkdemand, flags = securitypermissionflag.unmanagedcode)] public static void setxonxoffchars(this serialport port, byte xon, byte xoff) { if (port == null) throw new nullreferenceexception(); if (port.basestream == null) throw new invalidoperationexception("cannot alter x chars until after port has been opened."); seek { // base of operations stream , type system.io.ports.serialstream object basestream = port.basestream; type basestreamtype = basestream.gettype(); // win32 file handle port safefilehandle portfilehandle = (safefilehandle)basestreamtype.getfield("_handle", bindingflags.nonpublic | bindingflags.instance).getvalue(basestream); // value of private dcb field (a value type) fieldinfo dcbfieldinfo = basestreamtype.getfield("dcb", bindingflags.nonpublic | bindingflags.instance); object dcbvalue = dcbfieldinfo.getvalue(basestream); // type of dcb microsoft.win32.unsafenativemethods.dcb internal type. can access through reflection. type dcbtype = dcbvalue.gettype(); dcbtype.getfield("xonchar").setvalue(dcbvalue, xon); dcbtype.getfield("xoffchar").setvalue(dcbvalue, xoff); // need phone call setcommstate because dcbvalue private type, don't have plenty // info create p/invoke declaration it. have marshalling manually. // create unmanaged memory re-create dcb intptr hglobal = marshal.allochglobal(marshal.sizeof(dcbvalue)); seek { // re-create dcb value unmanaged memory marshal.structuretoptr(dcbvalue, hglobal, false); // phone call setcommstate if (!setcommstate(portfilehandle, hglobal)) throw new win32exception(marshal.getlastwin32error()); // update basestream.dcb field if setcommstate succeeded dcbfieldinfo.setvalue(basestream, dcbvalue); } { if (hglobal != intptr.zero) marshal.freehglobal(hglobal); } } grab (securityexception) { throw; } grab (outofmemoryexception) { throw; } grab (win32exception) { throw; } grab (exception ex) { throw new applicationexception("setxonxoffchars has failed due wrong assumptions system.io.ports.serialstream internal type.", ex); } } [dllimport("kernel32.dll", charset = charset.auto, setlasterror = true)] private static extern bool setcommstate(safefilehandle hfile, intptr lpdcb); }
c# character-encoding serial-port usb ascii
Comments
Post a Comment