How to debug RS485 serial ports under WinCE

Source: Internet
Author: User

 

Wince debugging RS485 serial port sense (Serial library implementation of the source program in: http://download.csdn.net/detail/dijkstar/4072407 download, 2012-02-16 add; use Microsoft serial control MSComm Implementation of the source program in: http://download.csdn.net/detail/dijkstar/4199667download, 2012-04-04add) this article continues with "RS485 half duplex software programming implementation. As mentioned above, RS485 is a half-duplex protocol. Some Industrial Control motherboard manufacturers fully implement automatic RTS when making RS485 ports, so our programmers do not need to care about underlying hardware changes, it is used just like RS232 serial port, which includes the "Bosch head" of 232 to 422 or 485 we bought in the electronic market, which has automatically implemented automatic RTS internally; some manufacturers do not implement automatic RTS, so we need to implement it ourselves. As mentioned above, in Win32, you only need to care about the DCB Data Structure FrtscontrolChange the value to rts_control_toggle, as shown in the following program: Initialization part:Hcom = createfile ("COM1", // COM1 port generic_read | generic_write, // allow read and write 0,
// The exclusive mode is null, open_existing,
// Open rather than create 0,
// Synchronization method null); If (hcom = (handle)-1) {afxmessagebox ("failed to open com! "); Return false;} setupcomm (hcom, 100,100 );
// The size of the input buffer and output buffer is commtimeouts timeouts; // set the read timeout timeouts. readintervaltimeout = maxdword; timeouts. readtotaltimeoutmultiplier = 0; timeouts. readtotaltimeoutconstant = 0; // after reading the content of the input buffer, the read operation returns immediately, regardless of whether the required characters are read. // Set the write timeout timeouts. writetotaltimeoutmultiplier = 100; timeouts. writetotaltimeoutconstant = 500; setcommtimeouts (hcom, & Timeouts); // set the timeout DCB; getcommstate (hcom, & DCB); DCB. baudrate = 9600;
// The baud rate is DCB. bytesize = 8;
// Each byte has a bit of DCB. Parity = noparity;
// No parity bit DCB. stopbits = twostopbits;
// Two stop bits DCB. frtscontrol = rts_control_toggle;Setcommstate (hcom, & DCB); purgecomm (hcom, purge_txclear | purge_rxclear ); Write part:Char lpoutbuffer [7]; memset (lpoutbuffer, '/0', 7); // first clears the previous byte lpoutbuffer [0] ='/x11 '; // The first byte of the sending buffer is DC1 lpoutbuffer [1] = '0'; // The first byte is the character (30 h) lpoutbuffer [2] = '0 '; // The first byte is the character (30 h) lpoutbuffer [3] = '1 ';//
The first byte is the character (31 H) lpoutbuffer [4] = '0'; // The first byte is the character (30 h) lpoutbuffer [5] = '1 '; // The first byte is the character (31 H) lpoutbuffer [6] = '/x03'; // The first byte is the character etx // you can see from this section of code, the instrument mailing address is DWORD protocol = 7; COMSTAT; DWORD dwerrorflags; bool bwritestat; clearcommerror (hcom, & dwerrorflags, & COMSTAT); bwritestat = writefile (hcom, lpoutbuffer, callback, & dwbyteswrite, null); If (! Bwritestat) {afxmessagebox ("failed to write the serial port! ");} Read part:Char STR [100]; memset (STR, '/0', 100); DWORD wcount = 100; // number of bytes read bool breadstat; breadstat = readfile (hcom, STR, wcount, & wcount, null); If (! Breadstat) afxmessagebox ("failed to read the serial port! "); Purgecomm (hcom, purge_txabort | purge_rxabort | purge_txclear | purge_rxclear); m_disp = STR; updatedata (false); the above Code passes in XP, but the same program is transplanted to wince, the maximum byte of the sent content is always 0xff. I found an article on msdn to explain the reasons as follows:
PRB: Using setcommstate () to toggle DTR/RTS for Flow ControlID: q99861

The information in this article applies:
  • Microsoft Windows software development kit (SDK) 3.1
SymptomsWhen setcommstate () is used to raise the DTr andrts lines for hardware flow control, the first several incoming characters may be lost. CauseSetcommstate () CILS the communications driver's setcom function, which CILS $ setcom. $ setcom disables interrupts from the specified port by clearing the UART's interrupt enable register (IER). After changing
The state of the uart and the dtr and
RTS lines, $ setcom delays while interrupts from the UART are still disabled. in Windows 3.1, this delay is approximately 200 milliseconds; in Windows 3.0, the delay is approximately 55 milliseconds. if the DTr andrts
Lines are raised from low to high, any characters that arrive before interrupts from the UART are enabled will be lost. ResolutionSetcommstate () shocould not be used to toggle the states of the DTr andrts lines for hardware flow control.

Use escapecommfunction () to toggle the states of the DTr and
RTS lines because it does not delay while interrupts are disabled. Additional query words: 3.00 3.10 commkeywords: kb16bitonly
Version: Windows: 3.1
Platform: Windows
Issue type: DCB is used. frtscontrol = rts_control_toggle; setcommstate (hcom, & DCB); if there is a problem, you need to use: escapecommfunction (hcom, clrrts) to clear the RTS bit, or escapecommfunction (hcom, setrts) to set the RTS bit. Considering the above program structure, you can always "setrts" (that is, the allowed sending status) before sending, and "clrrts" after sending (that is, set to the allowed receiving status ). For example, add hcom = createfile ("COM1", // COM1 port generic_read | generic_write, // allow read and write 0,
// The exclusive mode is null, open_existing,
// Open rather than create 0,
// Synchronization method null); If (hcom = (handle)-1) {afxmessagebox ("failed to open com! "); Return false;} setupcomm (hcom, 100,100 );
// The size of the input buffer and output buffer is commtimeouts timeouts; // set the read timeout timeouts. readintervaltimeout = maxdword; timeouts. readtotaltimeoutmultiplier = 0; timeouts. readtotaltimeoutconstant = 0; // after reading the content of the input buffer, the read operation returns immediately, regardless of whether the required characters are read. // Set the write timeout timeouts. writetotaltimeoutmultiplier = 100; timeouts. writetotaltimeoutconstant = 500; setcommtimeouts (hcom, & Timeouts); // set the timeout DCB; getcommstate (hcom, & DCB); DCB. baudrate = 9600;
// The baud rate is DCB. bytesize = 8;
// Each byte has a bit of DCB. Parity = noparity;
// No parity bit DCB. stopbits = twostopbits;
// Two stop bits // DCB. frtscontrol = rts_control_toggle; setcommstate (hcom, & DCB); purgecomm (hcom, purge_txclear | purge_rxclear );
If (! Escapecommfunction (hcom, clrrts) {afxmessagebox ("clrrts cannot be set");} in the write section:
If (! Escapecommfunction (hcom, setrts) {afxmessagebox ("setrts cannot be set");} Char lpoutbuffer [7]; memset (lpoutbuffer, '/0', 7 ); // The first byte first clears lpoutbuffer [0] = '/x11'; // the first byte of the sending buffer is DC1 lpoutbuffer [1] = '0 '; // The first byte is the character (30 h) lpoutbuffer [2] = '0'; // The first byte is the character (30 h) lpoutbuffer [3] = '1 '; //
The first byte is the character (31 H) lpoutbuffer [4] = '0'; // The first byte is the character (30 h) lpoutbuffer [5] = '1 '; // The first byte is the character (31 H) lpoutbuffer [6] = '/x03'; // The first byte is the character etx // you can see from this section of code, the instrument mailing address is DWORD protocol = 7; COMSTAT; DWORD dwerrorflags; bool bwritestat; clearcommerror (hcom, & dwerrorflags, & COMSTAT); bwritestat = writefile (hcom, lpoutbuffer, callback, & dwbyteswrite, null); If (! Bwritestat) {afxmessagebox ("failed to write the serial port! ");} Sleep (10 );
If (! Escapecommfunction (hcom, clrrts) {afxmessagebox ("clrrts cannot be set");} The read part does not need to be changed. The sleep function in the write action is tested in the pilot test. After the above changes, the serial port program under wince can be sent and received.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.