Rewrite serial port communicationf rom C++ to C# -
i want know if correctly mapped c++ code establishes serial port communication c#.
void func(...) { *hdev = createfile(portnameunc, generic_read|generic_write, 0, null, open_existing, file_attribute_normal, null); if(*hdev == invalid_handle_value) return false ; dcb *dcb = new dcb ; memset(dcb, 0x00, sizeof(dcb)) ; dcb->dcblength = sizeof(dcb); dcb->baudrate = baudrate; dcb->parity = parity; dcb->stopbits = stopbits; dcb->bytesize = bytesize; dcb->fbinary = true; dcb->fdsrsensitivity = 0; dcb->fdtrcontrol = (dtr ? dtr_control_enable : dtr_control_disable) ; dcb->frtscontrol = (rts ? rts_control_enable : rts_control_disable) ; dcb->foutxctsflow = (cts ? 1 : 0) ; dcb->foutxdsrflow = (dsr ? 1 : 0) ; dcb->foutx = (xonnxoff ? 1 : 0) ; dcb->finx = 0 ; if(!setcommstate(*hdev, dcb)) { delete dcb ; closehandle(*hdev) ; *hdev = invalid_handle_value ; return false; } if(!settimeout(readtimeout, readintervaltimeout) || !reset()) { closehandle(*hdev) ; *hdev = invalid_handle_value ; return false; } } bool serial::settimeout(dword readtimeout, dword readintervaltimeout) { commtimeouts *timeouts = new commtimeouts ; memset(timeouts, 0, sizeof(commtimeouts)) ; timeouts->readintervaltimeout = readintervaltimeout ; timeouts->readtotaltimeoutmultiplier = 5 ; timeouts->readtotaltimeoutconstant = readtimeout ; //... timeouts->writetotaltimeoutconstant = 0 ; // timeouts->writetotaltimeoutmultiplier = 2*baudrate/8 ; const bool ret = setcommtimeouts(*hdev, timeouts) != false ; delete timeouts ; m_currenttimeout = readtimeout ; return ret ; }
this c# mapping:
public serialporthasp(string portname, int baudrate, parity parity, int databits, stopbits stopbits, bool dtr, bool rts, bool xonxoff) { m_port = new serialport(portname, baudrate, parity, databits, stopbits); // store parameters user entered m_portname = portname; m_baudrate = baudrate; m_parity = parity; m_databits = databits; m_stopbits = stopbits; m_xonxoff = xonxoff; m_dtr = dtr; m_rts = rts; // set xonxoff if set if (xonxoff) m_port.handshake = handshake.xonxoff; // set dtr/rts m_port.dtrenable = dtr; m_port.rtsenable = rts; m_port.readtimeout = 500; // or other values m_port.writetimeout = 500; // open port communications m_port.open(); }
i need implement other read write operations (according protocol) till can test if mapping right, why want double check if took correct approach in mapping above code c#? or should take this route pinvoke calls? or still try current approach?
ps. thing can see way can't replicate parameters c++ calls allow 1 make
i add initialization:
m_port.datareceived += _serial_datareceived;
the event obtain data port. need async working.
and
void _serial_datareceived(object sender, serialdatareceivedeventargs e) { int _len = _serial.bytestoread; byte[] _out = new byte[_len]; _serial.read(_out, 0, _len); }
Comments
Post a Comment