Home > Software engineering >  MFC serial communication
MFC serial communication

Time:11-10

Students in original to write an article about matlab procedures, serial communication is used to control the instrument, but asked to use MFC to write a set of now, the original matlab code is as follows:
Delete (instrfindall);
% % define rotation time,
T_rotateh=40; % % rotator rotate for a week, and set aside a certain allowance,
T_rotatev=45; % % rotator vertical rotating a week, and set aside a certain allowance,
R_t=Max (t_rotateh/360 * (B - A), t_rotatev/90 * r_C) + 2; % % rotator vertical rotating a step diameter, horizontal rotation Angle at the same time, by using the maximum time,
T=t_rotateh/360 * C + 2; % % rotator rotate for a time,
% t_file=1;
Note 1: % % avoid removing variables,
% the clear all
% CLC
% 2: port Settings and open ports,
Scom=serial (port);
Scom. BaudRate=9600;
Scom. InputBufferSize=512;
Scom. BytesAvailableFcnMode='terminator'; % 'byte'
% scom. The terminator=LF;
Scom. Timeout=10; % the read/write wait time
Fopen (scom);
% % note 3: the input Settings,
% % horizontal direction:
N=floor ((B - A)/C) + 1;
Xmatrix=zeros (1, n); Ymatrix=zeros (1, n); Zmatrix=zeros (1, n);
% % attention to check whether the same variable using the same number, because there may be spelling mistakes,
For 2=1: n
Matrix=A + 2 * C - C;
Xmatrix (ii)=floor (matrix/100);
Ymatrix (ii)=floor (* xmatrix (matrix - 100 (ii))/10);
Zmatrix (ii)=floor (* xmatrix (matrix - 100 (ii) - 10 * ymatrix (ii))/1);
End
% % vertical direction:
R_n=floor ((r_B - r_A)/r_C) + 1;
% r_xmatrix=zeros (1, r_n); R_ymatrix=zeros (1, r_n); R_zmatrix=zeros (1, r_n);
For 2=1: r_n
Matrix * r_C=r_A + ii - r_C;
R_xmatrix (ii)=floor (matrix/100);
R_ymatrix (ii)=floor (* r_xmatrix (matrix - 100 (ii))/10);
R_zmatrix (ii)=floor (* r_xmatrix (matrix - 100 (ii) - 10 * r_ymatrix (ii))/1);
End
Note 4: % back to the initial position,
T1=clock;
Fprintf (scom, '% s \ r,...
[' W ', num2str (xmatrix (1)), num2str (ymatrix (1)), num2str (zmatrix (1)), ' ', num2str (r_xmatrix (1)), num2str (r_ymatrix (1)), num2str (r_zmatrix (1)), ' ']);
T2=clock;
T3=t2 - t1;
T4=t3 (3) * 24 * 24 * 24 * 60 * 60 + t3 (4) + t3 (5) * 60 + t3 (6);
While (t4 & lt;=t_rotateh)
T2=clock;
T3=t2 - t1;
T4=t3 (3) * 24 * 24 * 24 * 60 * 60 + t3 (4) + t3 (5) * 60 + t3 (6);
End
% %, vector network measurement and save the data,
For ii=1:1: r_n
Note 5: % vertical rotation,
T1=clock;
Fprintf (scom, '% s \ r', [' W ', num2str (xmatrix (1)), num2str (ymatrix (1)), num2str (zmatrix (1)), ' ', num2str (r_xmatrix (ii)), num2str (r_ymatrix (ii)), num2str (r_zmatrix (ii)), ' ']);
T2=clock;
T3=t2 - t1;
T4=t3 (3) * 24 * 24 * 24 * 60 * 60 + t3 (4) + t3 (5) * 60 + t3 (6);
While (t4 & lt;=r_t)
T2=clock;
T3=t2 - t1;
T4=t3 (3) * 24 * 24 * 24 * 60 * 60 + t3 (4) + t3 (5) * 60 + t3 (6);
End
For jj=1: n
Note 6: % level rotation,
T1=clock;
Fprintf (scom, '% s \ r', [' W ', num2str (xmatrix (jj)), num2str (ymatrix (jj)), num2str (zmatrix (jj)), ' ', num2str (r_xmatrix (ii)), num2str (r_ymatrix (ii)), num2str (r_zmatrix (ii)), ' ']);
T2=clock;
T3=t2 - t1;
T4=t3 (3) * 24 * 24 * 24 * 60 * 60 + t3 (4) + t3 (5) * 60 + t3 (6);
While (t4 & lt;=t)
T2=clock;
T3=t2 - t1;
T4=t3 (3) * 24 * 24 * 24 * 60 * 60 + t3 (4) + t3 (5) * 60 + t3 (6);
End
End




Now written in c + + MFC type after don't know how to control instruments,
Void CHelloDlg: : OnBnClickedBtnopenport ()
{
//TODO: add the control notification handler code

Int t_rotateh=40;//rotator rotate for a week, and set aside a certain allowance,
Int t_rotatev=45;//rotator vertical rotating a week, and set aside a certain allowance,
Int A, B, C, r_A, r_B, r_C;
Cstrings strEDIT1 strEDIT2, strEDIT3 strEDIT4, strEDIT5, strEDIT6;
GetDlgItem (IDC_EDIT1) - & gt; GetWindowText (strEDIT1);
GetDlgItem (IDC_EDIT2) - & gt; GetWindowText (strEDIT2);
GetDlgItem (IDC_EDIT3) - & gt; GetWindowText (strEDIT3);
GetDlgItem (IDC_EDIT4) - & gt; GetWindowText (strEDIT4);
GetDlgItem (IDC_EDIT5) - & gt; GetWindowText (strEDIT5);
GetDlgItem (IDC_EDIT6) - & gt; GetWindowText (strEDIT6);
A=_ttol (strEDIT1);
B=_ttol (strEDIT2);
C=_ttol (strEDIT3);
R_A=_ttol (strEDIT4);
R_B=_ttol (strEDIT5);
R_C=_ttol (strEDIT6);

Int r_t=Max (t_rotateh/360 * (B - A), t_rotatev/90 * r_C) + 2;//rotator rotating a step diameter, vertical and horizontal rotation Angle, by using the maximum time,
Int t=t_rotateh/360 * C + 2;//rotator rotate for a time,





If (m_cCom get_PortOpen ())//if a serial port is open, the line close a serial port
{
M_cCom. Put_PortOpen (FALSE);
}

M_cCom. Put_CommPort (3);//select COM3
M_cCom. Put_InBufferSize (1024);//receive buffer
M_cCom. Put_OutBufferSize (1024);//send buffer
M_cCom. Put_InputLen (0);//set the current receiving area data length is 0, said all read
M_cCom. Put_InputMode (1);//read and write data in a binary way
M_cCom. Put_RThreshold (1);//receive buffer when there are more than 1 and 1 character, will trigger a receiving data OnComm events
M_cCom. Put_Settings (_T (" 9600, n, 8, 1 "));//9600 baud rate test, 8 data bits, one stop bit

if (! M_cCom. Get_PortOpen ())//if the serial port does not open open
{
M_cCom. Put_PortOpen (TRUE);//open the serial port
AfxMessageBox (_T (" serial port open success "));

int i;
Int n=floor ((B - A)/C) + 1;

Int * xmatrix;
Int * ymatrix;
Int * zmatrix;
Xmatrix=(int *) malloc (sizeof (int) * n);
For (I=0; I & lt; n; I++)
{
Xmatrix [I]=0;
}

Ymatrix=(int *) malloc (sizeof (int) * n);
For (I=0; I & lt; n; I++)
{
Ymatrix [I]=0;
}

Zmatrix=(int *) malloc (sizeof (int) * n);
For (I=0; I & lt; n; I++)
{
Zmatrix [I]=0;
}

Int i1, matrix;
For (i1=1; I1 & lt; n; I1 + +)
{
Matrix=A + i1 * C - C;
Xmatrix [i1]=floor (matrix/100);
Ymatrix [i1]=floor ((matrix - 100 * xmatrix/i1)/10);
Zmatrix [i1]=floor ((matrix - 100 * xmatrix (i1) - 10 * ymatrix/i1)/1);

}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related