6410 serial port Software

Source: Internet
Author: User

I used the serial port application program compiled in vs2005 and synchronized it to the 6410 board.

...
// Open the serial port
Bool cserial1dlg: openport (lpctstr port, int baudrate, int databits, int stopbits, int parity)
{
Commtimeouts; // create a timeout Variable Parameter
DWORD error; // get error code
// Open the serial port
Hcomm = createfile (port, generic_read | generic_write, 0, 0, open_existing, 0, 0 );
If (hcomm = invalid_handle_value)
{
Error = getlasterror ();
MessageBox (_ T ("the port cannot be opened or the port has already been opened! Check whether it is in use ."));
Return false;
}
Getcommstate (hcomm, & DCB);/* read the DCB of the serial port */
DCB. baudrate = baudrate;/* set the baud rate */
DCB. bytesize = databits;/* set the data bit */
DCB. Parity = parity;/* set the check bit */
DCB. stopbits = stopbits;/* set the stop bit */
DCB. fparity = false;/* disable parity */
DCB. fbinary = true;
DCB. foutx = 0;
DCB. finx = 0;
// Set status parameters
Setcommmask (hcomm, ev_rxchar);/* serial port event: receives a character */
Setupcomm (hcomm, 16384,163 84);/* set the buffer size for receiving and sending */
Setcommstate (hcomm, & DCB );
Bool issucces = setcommstate (hcomm, & DCB);/* set the DCB of the serial port */
If (! Issucces)
{
Error = getlasterror ();
MessageBox (_ T ("the port cannot be configured according to the current parameter. Please check the parameter! "));
Closeport ();
Return false;
}

Getcommtimeouts (hcomm, & commtimeouts);/* Get the timeout parameter */
// Set the timeout Parameter
Commtimeouts. readintervaltimeout = 100;/* maximum interval between received characters */
Commtimeouts. readtotaltimeoutmultiplier = 1;
Commtimeouts. readtotaltimeoutconstant = 100;/* constant of the total read timeout */
Commtimeouts. writetotaltimeoutmultiplier = 0;
Commtimeouts. writetotaltimeoutconstant = 0;
If (! Setcommtimeouts (hcomm, & commtimeouts ))
{
MessageBox (_ T ("the timeout parameter cannot be set! "));
Closeport ();
Return false;
}
Purgecomm (hcomm, purge_txclear | purge_rxclear);/* clear the receiving/sending buffer */
Return true;
}

Bool cserial1dlg: closeport (void)
{
If (hcomm! = Invalid_handle_value)
{
Setcommmask (hcomm, 0); // notifies the thread to exit
Purgecomm (hcomm, purge_txclear | purge_rxclear);/* clear the receiving/sending buffer */
Closehandle (hcomm);/* close the serial port operation handle */
Hcomm = invalid_handle_value;
Return true;
}
Return false;
}

Void callback cserial1dlg: oncomrecv (cwnd * pwnd, char * Buf, int buflen)
{
Cstring TMP;
Cserial1dlg * pdlg = (cserial1dlg *) pwnd;
Cedit * precvstredit = (cedit *) pdlg-> getdlgitem (idc_rev); // get the receiver pointer
For (INT I = 0; I <buflen; I ++, Buf ++)
{
TMP. Format (_ T ("% C"), * BUF);/* convert the character to a string */
Pdlg-> strrecdisp + = TMP;
}
Precvstredit-> setwindowtext (pdlg-> strrecdisp);/* display in window */
}

DWORD cserial1dlg: comrecvthread (lpvoid lparam)
{
DWORD dwlength;
Char * recvbuf = new char [1024];
Cserial1dlg * pdlg = (cserial1dlg *) lparam;

While (true)
{
/* Wait for the thread to exit */
If (waitforsingleobject (pdlg-> exitthreadevent, 0) = wait_object_0)
Break;

If (pdlg-> hcomm! = Invalid_handle_value)
{/* Read data from the serial port */
Bool freadstate = readfile (pdlg-> hcomm, recvbuf, 1024, & dwlength, null );
If (! Freadstate)
{
MessageBox (_ T ("data cannot be read from the serial port! "));
}
Else
{
If (dwlength! = 0)
Oncomrecv (pdlg, recvbuf, dwLength-2);/* Receives successfully called callback function */
}
}
}
Delete [] recvbuf;
Return 0;
}

Void cserial1dlg: onopencom ()
{
DWORD idthread;
Handle hrecvthread;
Updatedata (true);/* pass the control value to the variable */
Cstring strport = _ T ("COM1:");/* set the serial port number */
DWORD baud = 115200;/* set the baud rate */
DWORD databit = 8;/* set the data bit */
Byte stopbit = onestopbit;/* set the stop bit */
Byte parity = noparity;/* set the check bit */
Bool ret = openport (strport, baud, databit, stopbit, parity);/* Open the serial port */
If (ret = false)
Return;
Exitthreadevent = createevent (null, true, false, null);/* Create a serial port receiving thread exit event */
Hrecvthread = createthread (0, 0, comrecvthread, this, 0, & idthread );
If (hrecvthread = NULL)
{
MessageBox (_ T ("An error occurred while creating the receiving thread! "));
Return;
}
Closehandle (hrecvthread );
Butopen. enablewindow (false);/* open the port and press the key to disable */
Butclose. enablewindow (true);/* close the port key to enable */
MessageBox (_ T ("successfully opened the serial port COM1! "));

}
// "Close serial port" Message Processing
Void cserial1dlg: onclosecom ()
{
If (exitthreadevent! = NULL)
{
Setevent (exitthreadevent);/* notifies the thread to exit */
Sleep (1000 );
Closehandle (exitthreadevent );
Exitthreadevent = NULL;
}

Butopen. enablewindow (true);/* open the port and press the key to disable */
Butclose. enablewindow (false);/* close the port key to enable */
Closeport ();
}
// "Send" Message Processing
Void cserial1dlg: onsend ()
{
DWORD dwactlen;
Char * recvbuf = new char [1024];
If (hcomm = invalid_handle_value)
{
MessageBox (_ T ("the serial port is not enabled! "));
Return;
}
Updatedata (true );
Int Len = strsendedit. getlength ();/* Get the length of the input string */
Char * psendbuf = new char [Len];

For (INT I = 0; I <Len; I ++)
Psendbuf [I] = (char) strsendedit. getat (I);/* convert to single-byte character */

Writefile (hcomm, psendbuf, Len, & dwactlen, null);/* send data from the serial port */

Delete [] psendbuf;
}
// Function called after the program ends
Void cserial1dlg: ondestroy ()
{
Cdialog: ondestroy ();

If (exitthreadevent! = NULL)
{
Setevent (exitthreadevent);/* notifies the serial receiving thread to exit */
Sleep (1000 );
Closehandle (exitthreadevent);/* close the handle of the event for receiving thread exit */
Exitthreadevent = NULL;
}
Closeport ();/* close the serial port */

}

...

Related Article

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.