C + + serial computer debugging assistant summary and part of the code

Source: Internet
Author: User

In the initialization function, add:

Todo:add Extra initialization here

M_bautosend=false; M_bautosend as a global variable
m_ncycletime=1000;

M_gpscom.   Setcommport (1); Select COM1
M_gpscom. Setinputmode (1); Input in binary mode
M_gpscom. Setinbuffersize (1024); Setting the input buffer size
M_gpscom. Setoutbuffersize (512); Setting the output buffer size
M_gpscom. Setsettings ("9600,n,8,1"); Baud rate 115200, no checksum, 8 data bits, 1 stop bits
if (m_gpscom. Getportopen ())//If the serial port is open
M_gpscom. Setportopen (FALSE);//Turn off the serial port

The parameter 1 indicates that there are more than
or equal to 1 characters, raises a OnComm event that receives data
M_gpscom. Setrthreshold (1);
M_gpscom.  Setinputlen (0); Sets the current receive area data length to 0
M_gpscom.    GetInput (); Pre-read buffers to clear residual data

Show what is received

The variables created by M_rxdata are CEIDTL types, not CString

Static long  rxcount=0;
void cgps_com1dlg::ongpscom ()           //edit box receive function-- Used to display data received by the serial port
{
 //todo:add your control notification handler code here
 variant VARIANT_INP;
 colesafearray SAFEARRAY_INP;
 long len,k;
 byte rxdata[2048];//Set BYTE array an 8-bit integerthat are not signed.
 cstring strtemp;
 cstring Strrxdata;
  
 strrxdata.empty ();
    if (m_gpscom. Getcommevent () ==2)     //Event value 2 indicates that there is a character
 {
  variant_inp=m_gpscom in the receive buffer. GetInput ();  //Read buffer
  safearray_inp=variant_inp;       The     //variant variable is converted to the COleSafeArray type variable
  LEN=SAFEARRAY_INP. Getonedimsize (); Gets the valid data length
  for (k=0;k<len;k++)
   safearray_inp. GetElement (&k,rxdata+k);//Convert to byte array

  for (k=0;k<len;k++)             // Convert an array to a CString variable
  {
   byte bt=* (char*) (rxdata+k);      //character type
   if (m_hexdisplay. Getcheck ())
   {
    strtemp. Format ("%02x", BT);        //send hex into temporary variable strtemp hold
    }
   else
   {
    strtemp. Format ("%c", BT);        //characters are fed into the temporary variable strtemp hold
   }
   strRXData=strRXData+strtemp;   //Join the Receive edit box corresponding to the string  
   
  }

M_rxdata. Replacesel (Strrxdata);

Rxcount=rxcount+len;
CString Rxdatacount;
Rxdatacount. Format ("%ld", Rxcount);
Rxdatacount= "Receive:" +rxdatacount;

M_rxdatacount.setwindowtext (Rxdatacount); Show Receive Count

}
}

Without UpdateData (), the benefit of this writing is that it can be sent freely without being affected by the UpdateData () function when it is received.

The serial port turns on or off

void Cgps_com1dlg::onbutton1 ()//serial port turned on or off
{
Todo:add your control notification handler code here
if (M_ctrlautosend.getcheck ())
{
AfxMessageBox ("Please turn off the automatic transmission before you can turn off the serial port");
}
Else
{

if (!m_gpscom. Getportopen ())
{
M_gpscom. Setportopen (TRUE);//Open the serial port
Setdlgitemtext (Idc_button1, "Close serial port");

}
Else
{
M_gpscom. Setportopen (FALSE);//Turn off the serial port
Setdlgitemtext (Idc_button1, "Open serial port");
}
}
}

The variables created by M_txdata are CEIDTL types, not CString

Long tx_count=0;
void Cgps_com1dlg::onbutton2tx ()//Data Send function
{
Todo:add your control notification handler code here
if (!m_gpscom. Getportopen ())
{
AfxMessageBox ("Please open the serial port first");
}
Else
{
UpdateData (TRUE); Reading edit box Contents
if (M_strhexsend.getcheck ())
{
CByteArray Hexdata;
int Len=string2hex (m_txdata,hexdata); The Len returned here can be used to calculate the number of hexadecimal digits sent
M_gpscom. Setoutput (COleVariant (hexdata)); Send hex Data
tx_count+= (Long) ((M_txdata. GetLength () +1)/3); The hexadecimal count should pay attention to the algorithm
}
Else
{
M_gpscom. Setoutput (COleVariant (M_txdata));//send ASCII character data
Tx_count+=m_txdata. GetLength ();
}
CString strtemp;
Strtemp.format ("%d", tx_count);
M_txcount.setwindowtext (strtemp);
}
}

void Cgps_com1dlg::onbutton3clear ()//clear receive edit box
{
Todo:add your control notification handler code here
M_rxdata. SetWindowText ("");//Clears the data from the Receive dialog box


}

void Cgps_com1dlg::oncheck1hexdisplay ()
{
Todo:add your control notification handler code here

}


void Cgps_com1dlg::oncomchose ()
{
Todo:add your control notification handler code here
Char str[10];

int Ipos=m_comchose.getcursel ();
int Inum=m_comchose.getlbtext (IPos, (LPTSTR) str);
int I=atoi (STR+3);
if (m_gpscom. Getportopen ())
{
AfxMessageBox ("Please close the serial port, then select the COM port");
}
if (!m_gpscom. Getportopen ())
{
M_gpscom.   Setcommport (i); Select COM port

}

}

void Cgps_com1dlg::onhexsend ()
{
Todo:add your control notification handler code here


}


void Cgps_com1dlg::ontimer (UINT nidevent)
{
Todo:add your message handler code here and/or call default
Switch (nidevent)
{
Case 1:onbutton2tx ();
Break
/* Case 2:
M_strcurpath.setwindowtext (M_strcurpath); Re-display the path
KillTimer (2);//Off Timer

*/
Default:;
Break

}
Cdialog::ontimer (nidevent);
}

void Cgps_com1dlg::oncheck2_autosend ()  
{
 //todo:add your control notification handler code here< br> 
 m_bAutoSend=!m_bAutoSend; //Flag Open Auto Send
 if (m_bautosend)
 {
   if (!m_gpscom. Getportopen ())
  {
   m_bautosend=!m_bautosend;
   m_ctrlautosend.setcheck (0);
   afxmessagebox ("The serial port is not open, please open the serial port");
   return;
  }
  else
   settimer (1,m_ncycletime,null);  //Set timer 1 m_ncycletime
 }
 else
 {
  killtimer (1);   //"Kill" Timer 1
 }
}

void Cgps_com1dlg::onchangeedit_cycletime ()
{
Todo:if This is a RICHEDIT control, the control would not
Send this notification unless you override the CDialog::OnInitDialog ()
function and call CRichEditCtrl (). SetEventMask ()
With the ENM_CHANGE flag ORed into the mask.

Todo:add your control notification handler code here

cedit* pedit= (cedit*) GetDlgItem (IDC_EDIT1);
CString StrText;
Pedit->getwindowtext (StrText);
M_ncycletime=atoi (StrText);


}

void Cgps_com1dlg::onclearcount ()
{
Todo:add your control notification handler code here
rxcount=0;
tx_count=0;
M_rxdatacount.setwindowtext ("");//Clears the data from the Receive dialog box
M_txcount.setwindowtext ("");//Clears the data from the Receive dialog box
}

void Cgps_com1dlg::onbuttonsavedata ()
{
Todo:add your control notification handler code here
UpdateData (TRUE);

CFile M_rfile;
LPCSTR lpszpath= "C:\\comdata"; Disk path is C:\\comdata
SetCurrentDirectory (Lpszpath);

CString m_filename,m_name1;
M_name1=m_strfilename; Fill in the name
m_filename=m_name1+ ". txt"; stored in TXT format
if (0==m_strfilename.getlength ())
{
AfxMessageBox ("Please enter the saved file name first");
}
Else
{
if (!m_rfile.open (m_filename,cfile::modecreate | Cfile::modewrite))
{
AfxMessageBox ("Failed to create record file! ");
Return
}

Char txttemp[60000]; The length of the string
int maxsize=60000; //
int I=m_rxdata. GetWindowText (txttemp,maxsize);//Take the data from the edit box and put it in the array txttemp

Write the save date at the beginning of the file
CTime T=ctime::getcurrenttime ();
CString Str=t.format ("%y years%m month%d%h when%m minutes%s seconds \ r \ n");
M_rfile.write ((LPCTSTR) str,str. GetLength ());
Save data
M_rfile.write ((LPCTSTR) txttemp,m_rxdata. Getwindowtextlength ());
M_rfile.flush ();
M_rfile.close (); Close File
AfxMessageBox ("saved successfully");
UpdateData (FALSE);
}
}

To declare these two functions in the header file

  //because of the format limitations of this conversion function, the 16-character characters in the Send box should be inserted between each two characters an empty
  //such as: A1 0B,
  // CByteArray is a dynamic byte array and can be see MSDN help
int cgps_com1dlg::string2hex (CString str, CByteArray &senddata)
{
  int Hexdata,lowhexdata;
  int hexdatalen=0;
  int len=str. GetLength ();
  senddata. SetSize (LEN/2);
  for (int i=0;i<len;)
  {
   char lstr,hstr=str[i];
   if (hstr== ")
   {
    i++;
    continue;
   }
   i++;
   if (I>=len)
   break;

   lstr=str[i];
   hexdata=converthexchar (HSTR);
   lowhexdata=converthexchar (LSTR);
   if ((hexdata==16) | | (lowhexdata==16))
    break;
   else 
    hexdata=hexdata*16+lowhexdata;
   i++;
   senddata[hexdatalen]= (char) hexdata;
   hexdatalen++;
  }
  senddata. SetSize (Hexdatalen);
  return Hexdatalen;
}

This is a function that converts a character to the corresponding hexadecimal value
A lot of C books can be found.
Function: If the character between 0-f, then convert to the corresponding hexadecimal character, otherwise return-1
Char Cgps_com1dlg::converthexchar (char ch)
{
if ((ch>= ' 0 ') && (ch<= ' 9 '))
return ch-0x30;
else if ((ch>= ' A ') && (ch<= ' F '))
Return ch-' A ' +10;
else if ((ch>= ' a ') && (ch<= ' F '))
Return ch-' a ' +10;
else return (-1);
}

C + + serial computer debugging assistant summary and part of the code

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.