附录A
下位机程序:
#include
#include
#include
#define FOSC 16000000
#define BAUD 9600
#define uint unsigned int
#define uchar unsigned char
#define TCNT 60536 //2.5ms中断一次
uchar RX_data[5]={'\0', '\0', '\0', '\0', '\0'}; //每次串口接收到的
数据
uchar RX_counter = 0; //串口接收到的字节数计数器
volatile int Rec_data1; // Rec_data1为串口接收到的数据,可
能会被随机改变
uint count,cunt,td,timer,temp;
uchar servotap; //用来标识舵机号
uint PwmWeigth; //用来标识脉冲宽度
uint pwm1, pwm2, pwm3, pwm4, pwm5, pwm6, pwm7, pwm8, pwm9, pwm10, pwm11, pwm12, pwm13, pwm14, pwm15, pwm16, pwm17, pwm18, pwm19, pwm20, pwm21,pwm22,pwm23,pwm24; //用来临时存储各舵机脉冲
宽度
uint pwm[24]={63536,62536,62536,62536,62536,62536,62536,
62536,62536,62536,62536,62536,62536,62536,62536,62536,
62536,62536,62536,62536,62536,62536,62536,62536}; //用来存储用户给各舵机的目标脉冲宽度
uchar StepLength[24];
/***************************************************************************************************************
** 端口初始化函数 **
********************************************************
*******************************************************/
void port_init()
{
PORTA = 0x00; //所有端口均设置为输出,且初始化为低电平 DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00;
DDRC = 0xFF;
PORTD = 0x00;
DDRD = 0xFF;
}
/*******************************************************
********************************************************
** 定时器1初始化函数 **
********************************************************
*******************************************************/
void timer1_init(void )
{
TCCR1B = 0x00; //给定时器赋初值时先关闭时钟以正正确计数
TCNT1 = TCNT; //2.5毫秒中断
TCCR1A = 0x00;
TCCR1B = 0x02; //8分频时钟
}
/*******************************************************
********************************************************
** 串口初始化函数**
***************************************************************************************************************/
void USART_Init()
{
UBRRH = (FOSC/BAUD/16-1)/256; //时钟16Mhz ,波特率
9600
UBRRL=(FOSC/BAUD/16-1)%256; //不对UCSRC 进行设置。
默认8位数据位,一位停
止位,无校验位
UCSRB=(1
}
/******************************************************
*******************************************************
**系统初始化函数 **
*******************************************************
******************************************************/
void init_devices(void )
{
CLI();
port_init();
timer1_init();
USART_Init();
TIMSK = 0x04; //使能定时器1中断
SEI(); //开总中断
}
/***************************************************************************************************************
**输出低电平函数**
********************************************************
*******************************************************/
void output_low()
{
if (td==1) //在第一个时间片中,当检测到检测计数器中的
值大于用
户给定的各组舵机对应的pwm[]数组中的值
时,将相应端置低
{
if (TCNT1>pwm[0])
{
PORTA &= ~(1
}
if (TCNT1>pwm[1])
{
PORTA &= ~(1
}
if (TCNT1>pwm[2])
{
PORTA &= ~(1
}
}
if (td==2)
{
if (TCNT1>pwm[3])
{
PORTA &= ~(1
}
if (TCNT1>pwm[4])
{
PORTA &= ~(1
}
if (TCNT1>pwm[5])
{
PORTA &= ~(1
}
}
if (td==3)
{
if (TCNT1>pwm[6])
{
} if (TCNT1>pwm[7]) { PORTA &= ~(1pwm[8]) { PORTB &= ~(1pwm[9]) { PORTB &= ~(1pwm[10]) { PORTB &= ~(1pwm[11]) { PORTB &= ~(1pwm[12]) { PORTB &= ~(1pwm[13]) { PORTB &= ~(1pwm[14]) { PORTB &= ~(1pwm[15])
}
PORTB &= ~(1pwm[16]) { PORTD &= ~(1pwm[17]) { PORTD &= ~(1pwm[18]) { PORTB &= ~(1pwm[19]) { PORTD &= ~(1pwm[20]) { PORTD &= ~(1pwm[21]) { PORTB &= ~(1pwm[22]) { PORTD &= ~(1pwm[23]) { PORTD &= ~(1
/**********************************************
* *定时器1中断服务函数* *
**********************************************/
#pragma interrupt_handler Timer1_ISR:iv_TIMER1_OVF
void Timer1_ISR()
{
unsigned char i , j;
TCNT1 = TCNT; //赋初始
td++; //时间片计数加1
output_high(); //输出高电平
if (td>=9) //在每个周期结束,执行一次调速函数
{
for (i=0;i
{
for (j=0;j
}
td=0;
}
}
void main()
{
init_devices();
while (1) //进入无限循环体,检测检测到检测计数
器中的值且与用户给定的各组舵机对应的pwm[]数组中的值比较
{
output_low();
}
}
附录B
上位机程序:
//部分控件函数的编写。初始化及VC++系统文件均未给出
void CMyDlg::OnCheck1() //选中check box1时,//使能滑竿1与滑竿上方的编辑框1
{
// TODO: Add your control notification handler code here
//m_slider1.SetEnabled();
if( m_CHECK1==0)
{
CSliderCtrl *pCHECK1 = (CSliderCtrl *)GetDlgItem(IDC_SLIDER1); //获得滑竿1按钮指针
pCHECK1->EnableWindow(TRUE); //使能滑竿1
CEdit *pCHECK1_edit1 = (CEdit *)GetDlgItem(IDC_EDIT1);
//获得滑竿上方的编辑框1按钮指针
pCHECK1_edit1->EnableWindow(TRUE); //使能滑竿上方的编辑框1 }
if( m_CHECK1==1) // check box2处于非选中时, //禁止滑竿1与滑竿上方的编辑框1
{
CSliderCtrl *pCHECK1_1 = (CSliderCtrl *)GetDlgItem(IDC_SLIDER1); //获得滑竿1按钮指针
pCHECK1_1->EnableWindow(FALSE); //禁止滑竿1
CEdit *pCHECK1_edit1_1 = (CEdit *)GetDlgItem(IDC_EDIT1); //获得滑竿1按钮指针
pCHECK1_edit1_1->EnableWindow(FALSE); //禁止滑竿1与滑竿上方的编辑框1
m_slider1.SetPos(1500);
}
}
//其它类似控件操作类似
void CMyDlg::OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) {
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_edit1=m_slider1.GetPos(); // 滑竿1与编辑框1数值关联,其它的类似
((CSliderCtrl*)GetDlgItem(IDC_SLIDER1))->SetPos(m_edit1);
UpdateData(FALSE);
*pResult = 0;
}
v
void CMyDlg::OnOnCommMscomm1() //MSComm控件函数
{
// TODO: Add your control notification handler code here
m_ctrlComm.SetOutBufferSize(1024);//发送缓冲区
UpdateData(FALSE); //更新编辑框内容*/
}
void CMyDlg::OnButtonSet() //设置参数按扭函数
{
// TODO: Add your control notification handler code here
int index=((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel();
m_nPort= index+1;
UpdateData(TRUE);
setDlg.m_nPort=m_nPort;
setDlg.m_strSettings=m_strSettings;
UpdateData(FALSE);
if(setDlg.DoModal()==IDOK)
{
m_nPort = setDlg.m_nPort;
m_strSettings=setDlg.m_strSettings;
switch (m_nPort) //将主窗口与副窗口组合框值关联 {
case 1:
((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM1");
//在组合框中写入字符串
UpdateData(TRUE);
}
OpenPort(); //以当前串口参数重新打开串口
}
void CMyDlg::OpenPort() //打开串口函数
{
if(m_ctrlComm.GetPortOpen()) //若串口已经打开,则先关闭
{
m_ctrlComm.SetPortOpen(FALSE);
}
//UpdateData(FALSE);
m_ctrlComm.SetCommPort(m_nPort); //根据当前串口号选择所要打开的串口
if(!m_ctrlComm.GetPortOpen()) //如果串口没有打开,打开串口
m_ctrlComm.SetPortOpen(TRUE);
}
else
AfxMessageBox ("端口未打开或已被占用");
}
void CMyDlg::OnSend() //将数据发送给单片机
{
UpdateData(TRUE);
CString str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14, str15,str16,str17,str18,str19,str20,str21,str22,str23,str24;
if(m_CHECK1)
{
str1.Format("A%04d",m_edit1);
m_ctrlComm.SetOutput(COleVariant(str1)); }
void CMyDlg::OnButtonConnect()
{
//TODO: Add your control notification handler code here
int index;
index=((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel();
m_nPort = index+1;
OpenPort();
bSend=!bSend;
if(bSend)
{
if(m_ctrlComm.GetPortOpen())
{
m_ctrlConnect.SetWindowText(_T("断开"));
SetTimer(1,m_editTimer,NULL); //启动定时器
UpdateData(FALSE);
CButton *pBut1 = (CButton *)GetDlgItem(IDC_BUTTON1); pBut1->EnableWindow(FALSE);
}
}
else
{
m_ctrlConnect.SetWindowText(_T("连接"));
KillTimer(1); 关闭定时器
CButton *pBut1 = (CButton *)GetDlgItem(IDC_BUTTON1); //获得按钮指针
pBut1->EnableWindow(TRUE);
UpdateData(FALSE);
}
void CMyDlg::OnTimer(UINT nIDEvent)
{
//TODO: Add your message handler code here and/or call default
OnSend(); //发送
}
void CMyDlg::OnEditSrialPortCombo1() //设置主窗口显示串口号组合框的值 {
// TODO: Add your control notification handler code here
((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM1"); ((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM2"); ((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM3"); ((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM4");
((CComboBox*)GetDlgItem(IDC_COMBO1))->SetCurSel(3);
}
void CMyDlg::OnChangeEdit1() //滑竿上方编辑框与滑竿关联
{
// TODO: If this is a RICHEDIT control, the control will 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
UpdateData();
((CSliderCtrl*)GetDlgItem(IDC_SLIDER1))->SetPos(m_edit1);
UpdateData(FALSE);
}
void CMyDlg::OnChangeEditSpeed1() //在速度编辑框中写入数值范围提示,其它类似
{
// TODO: If this is a RICHEDIT control, the control will 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
UpdateData(TRUE);
if(m_edit_speed1>8000)
{
MessageBox("请输入一个小于8000的整数");
}
UpdateData(FALSE);
}
void CMyDlg::OnHScroll(UINT nSBCode,UINT nPos, CScrollBar*pScrollBar) //设置时间间隔滚动条
{
// TODO: Add your message handler code here and/or call default
UpdateData();
int nCurPos=pScrollBar->GetScrollPos();
switch(nSBCode)
{
case SB_LINEDOWN:
nCurPos+=1;
break;
case SB_LINEUP:
nCurPos-=1;
break;
case SB_THUMBTRACK:
nCurPos=nPos;
break;
default:
break;
}
pScrollBar->SetScrollPos(nCurPos);
m_editTimer=m_scrollbar2.GetScrollPos();//与编辑框关联
m_editTimer=nCurPos;
UpdateData(FALSE);
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
//设计编辑框文本颜色及背景色
// TODO: Change any attributes of the DC here
//UpdateData(TRUE);
if (pWnd->GetDlgCtrlID() == IDC_SCROLLBAR2)
{
// Set the text color to red
UpdateData(TRUE);
pDC->SetTextColor(RGB(0, 255, 0));
pDC->SetBkMode(TRANSPARENT);
nCtlColor=CTLCOLOR_SCROLLBAR ;
//UpdateData(FALSE);
}
if (pWnd->GetDlgCtrlID() == IDC_EDIT_SPEED1)
{
// Set the text color to red
UpdateData(TRUE);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255, 0, 0));
//pDC->SetBkColor(RGB(255,0,0));
nCtlColor=CTLCOLOR_EDIT ;//CTLCOLOR_STATIC
HBRUSH b=CreateSolidBrush(RGB (121,121,255));
return b;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
//删除编辑框内容
void CMyDlg::OndeleteButton5()
{
// TODO: Add your control notification handler code here
//UpdateData(TRUE);
CEdit *pEditSend = (CEdit*)GetDlgItem(IDC_EDIT_RX);
pEditSend->Clear();
pEditSend->RedrawWindow();
UpdateData(FALSE);
}
void CMyEdit::OnLButtonDown(UINT nFlags, CPoint point) //显示窗口中光标选中整行的设置
{
// TODO: Add your message handler code here and/or call default
CEdit::OnLButtonDown(nFlags, point);
int nChar=CharFromPos(point);
int nLine=HIWORD(nChar);
int start=LineIndex(nLine);
int nLen=LineLength(start);
int end=start+nLen;
SetSel(start,end);
}
副窗口函数编写
void CSettingDlg::OnEditchangeCombo1Set() //设置串口数
{
// TODO: Add your control notification handler code here
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM1"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM2"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM3"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM4"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(3); //在组合框的列表框当中选择一个字符串
}
void CSettingDlg::OnOk()
{
// TODO: Add your control notification handler code here
int index;
index=((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->GetCurSel(); //获取当前选择条目的索引值
//((CComboBox*)GetDlgItem(IDC_COMBO1))->GetLBText(index,m_strSalary); //将索引值存在m_strSalary中
m_nPort = index+1; //点击确定后,与主窗口关联
UpdateData(TRUE);
CDialog::OnOK();
}
void CSettingDlg::OnButtonCancel()
{
UpdateData(TRUE);
CDialog::OnCancel();
}
BOOL CSettingDlg::OnInitDialog()
{
CDialog::OnInitDialog();
OnEditchangeCombo1Set() ;
OnEditchangeCombo2Baund();
OnEditchangeCombo3Databit();
OnEditchangeCombo4Stop() ;
OnEditchangeCombo5Check();
OnEditchangeCombo6Control();
UpdateData(TRUE);
UpdateData(FALSE);
switch (m_nPort) //副窗口初始化时将主窗口中对串口号的设置读取 {
case 1:
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))>AddString("COM1"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(0);
break;
case 2:
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM2"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(1); break;
case 3:
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM3"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(2); break;
case 4
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM4"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(3); break;
UpdateData(FALSE);
}
return TRUE;
}
附录A
下位机程序:
#include
#include
#include
#define FOSC 16000000
#define BAUD 9600
#define uint unsigned int
#define uchar unsigned char
#define TCNT 60536 //2.5ms中断一次
uchar RX_data[5]={'\0', '\0', '\0', '\0', '\0'}; //每次串口接收到的
数据
uchar RX_counter = 0; //串口接收到的字节数计数器
volatile int Rec_data1; // Rec_data1为串口接收到的数据,可
能会被随机改变
uint count,cunt,td,timer,temp;
uchar servotap; //用来标识舵机号
uint PwmWeigth; //用来标识脉冲宽度
uint pwm1, pwm2, pwm3, pwm4, pwm5, pwm6, pwm7, pwm8, pwm9, pwm10, pwm11, pwm12, pwm13, pwm14, pwm15, pwm16, pwm17, pwm18, pwm19, pwm20, pwm21,pwm22,pwm23,pwm24; //用来临时存储各舵机脉冲
宽度
uint pwm[24]={63536,62536,62536,62536,62536,62536,62536,
62536,62536,62536,62536,62536,62536,62536,62536,62536,
62536,62536,62536,62536,62536,62536,62536,62536}; //用来存储用户给各舵机的目标脉冲宽度
uchar StepLength[24];
/***************************************************************************************************************
** 端口初始化函数 **
********************************************************
*******************************************************/
void port_init()
{
PORTA = 0x00; //所有端口均设置为输出,且初始化为低电平 DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00;
DDRC = 0xFF;
PORTD = 0x00;
DDRD = 0xFF;
}
/*******************************************************
********************************************************
** 定时器1初始化函数 **
********************************************************
*******************************************************/
void timer1_init(void )
{
TCCR1B = 0x00; //给定时器赋初值时先关闭时钟以正正确计数
TCNT1 = TCNT; //2.5毫秒中断
TCCR1A = 0x00;
TCCR1B = 0x02; //8分频时钟
}
/*******************************************************
********************************************************
** 串口初始化函数**
***************************************************************************************************************/
void USART_Init()
{
UBRRH = (FOSC/BAUD/16-1)/256; //时钟16Mhz ,波特率
9600
UBRRL=(FOSC/BAUD/16-1)%256; //不对UCSRC 进行设置。
默认8位数据位,一位停
止位,无校验位
UCSRB=(1
}
/******************************************************
*******************************************************
**系统初始化函数 **
*******************************************************
******************************************************/
void init_devices(void )
{
CLI();
port_init();
timer1_init();
USART_Init();
TIMSK = 0x04; //使能定时器1中断
SEI(); //开总中断
}
/***************************************************************************************************************
**输出低电平函数**
********************************************************
*******************************************************/
void output_low()
{
if (td==1) //在第一个时间片中,当检测到检测计数器中的
值大于用
户给定的各组舵机对应的pwm[]数组中的值
时,将相应端置低
{
if (TCNT1>pwm[0])
{
PORTA &= ~(1
}
if (TCNT1>pwm[1])
{
PORTA &= ~(1
}
if (TCNT1>pwm[2])
{
PORTA &= ~(1
}
}
if (td==2)
{
if (TCNT1>pwm[3])
{
PORTA &= ~(1
}
if (TCNT1>pwm[4])
{
PORTA &= ~(1
}
if (TCNT1>pwm[5])
{
PORTA &= ~(1
}
}
if (td==3)
{
if (TCNT1>pwm[6])
{
} if (TCNT1>pwm[7]) { PORTA &= ~(1pwm[8]) { PORTB &= ~(1pwm[9]) { PORTB &= ~(1pwm[10]) { PORTB &= ~(1pwm[11]) { PORTB &= ~(1pwm[12]) { PORTB &= ~(1pwm[13]) { PORTB &= ~(1pwm[14]) { PORTB &= ~(1pwm[15])
}
PORTB &= ~(1pwm[16]) { PORTD &= ~(1pwm[17]) { PORTD &= ~(1pwm[18]) { PORTB &= ~(1pwm[19]) { PORTD &= ~(1pwm[20]) { PORTD &= ~(1pwm[21]) { PORTB &= ~(1pwm[22]) { PORTD &= ~(1pwm[23]) { PORTD &= ~(1
/**********************************************
* *定时器1中断服务函数* *
**********************************************/
#pragma interrupt_handler Timer1_ISR:iv_TIMER1_OVF
void Timer1_ISR()
{
unsigned char i , j;
TCNT1 = TCNT; //赋初始
td++; //时间片计数加1
output_high(); //输出高电平
if (td>=9) //在每个周期结束,执行一次调速函数
{
for (i=0;i
{
for (j=0;j
}
td=0;
}
}
void main()
{
init_devices();
while (1) //进入无限循环体,检测检测到检测计数
器中的值且与用户给定的各组舵机对应的pwm[]数组中的值比较
{
output_low();
}
}
附录B
上位机程序:
//部分控件函数的编写。初始化及VC++系统文件均未给出
void CMyDlg::OnCheck1() //选中check box1时,//使能滑竿1与滑竿上方的编辑框1
{
// TODO: Add your control notification handler code here
//m_slider1.SetEnabled();
if( m_CHECK1==0)
{
CSliderCtrl *pCHECK1 = (CSliderCtrl *)GetDlgItem(IDC_SLIDER1); //获得滑竿1按钮指针
pCHECK1->EnableWindow(TRUE); //使能滑竿1
CEdit *pCHECK1_edit1 = (CEdit *)GetDlgItem(IDC_EDIT1);
//获得滑竿上方的编辑框1按钮指针
pCHECK1_edit1->EnableWindow(TRUE); //使能滑竿上方的编辑框1 }
if( m_CHECK1==1) // check box2处于非选中时, //禁止滑竿1与滑竿上方的编辑框1
{
CSliderCtrl *pCHECK1_1 = (CSliderCtrl *)GetDlgItem(IDC_SLIDER1); //获得滑竿1按钮指针
pCHECK1_1->EnableWindow(FALSE); //禁止滑竿1
CEdit *pCHECK1_edit1_1 = (CEdit *)GetDlgItem(IDC_EDIT1); //获得滑竿1按钮指针
pCHECK1_edit1_1->EnableWindow(FALSE); //禁止滑竿1与滑竿上方的编辑框1
m_slider1.SetPos(1500);
}
}
//其它类似控件操作类似
void CMyDlg::OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) {
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_edit1=m_slider1.GetPos(); // 滑竿1与编辑框1数值关联,其它的类似
((CSliderCtrl*)GetDlgItem(IDC_SLIDER1))->SetPos(m_edit1);
UpdateData(FALSE);
*pResult = 0;
}
v
void CMyDlg::OnOnCommMscomm1() //MSComm控件函数
{
// TODO: Add your control notification handler code here
m_ctrlComm.SetOutBufferSize(1024);//发送缓冲区
UpdateData(FALSE); //更新编辑框内容*/
}
void CMyDlg::OnButtonSet() //设置参数按扭函数
{
// TODO: Add your control notification handler code here
int index=((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel();
m_nPort= index+1;
UpdateData(TRUE);
setDlg.m_nPort=m_nPort;
setDlg.m_strSettings=m_strSettings;
UpdateData(FALSE);
if(setDlg.DoModal()==IDOK)
{
m_nPort = setDlg.m_nPort;
m_strSettings=setDlg.m_strSettings;
switch (m_nPort) //将主窗口与副窗口组合框值关联 {
case 1:
((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM1");
//在组合框中写入字符串
UpdateData(TRUE);
}
OpenPort(); //以当前串口参数重新打开串口
}
void CMyDlg::OpenPort() //打开串口函数
{
if(m_ctrlComm.GetPortOpen()) //若串口已经打开,则先关闭
{
m_ctrlComm.SetPortOpen(FALSE);
}
//UpdateData(FALSE);
m_ctrlComm.SetCommPort(m_nPort); //根据当前串口号选择所要打开的串口
if(!m_ctrlComm.GetPortOpen()) //如果串口没有打开,打开串口
m_ctrlComm.SetPortOpen(TRUE);
}
else
AfxMessageBox ("端口未打开或已被占用");
}
void CMyDlg::OnSend() //将数据发送给单片机
{
UpdateData(TRUE);
CString str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14, str15,str16,str17,str18,str19,str20,str21,str22,str23,str24;
if(m_CHECK1)
{
str1.Format("A%04d",m_edit1);
m_ctrlComm.SetOutput(COleVariant(str1)); }
void CMyDlg::OnButtonConnect()
{
//TODO: Add your control notification handler code here
int index;
index=((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel();
m_nPort = index+1;
OpenPort();
bSend=!bSend;
if(bSend)
{
if(m_ctrlComm.GetPortOpen())
{
m_ctrlConnect.SetWindowText(_T("断开"));
SetTimer(1,m_editTimer,NULL); //启动定时器
UpdateData(FALSE);
CButton *pBut1 = (CButton *)GetDlgItem(IDC_BUTTON1); pBut1->EnableWindow(FALSE);
}
}
else
{
m_ctrlConnect.SetWindowText(_T("连接"));
KillTimer(1); 关闭定时器
CButton *pBut1 = (CButton *)GetDlgItem(IDC_BUTTON1); //获得按钮指针
pBut1->EnableWindow(TRUE);
UpdateData(FALSE);
}
void CMyDlg::OnTimer(UINT nIDEvent)
{
//TODO: Add your message handler code here and/or call default
OnSend(); //发送
}
void CMyDlg::OnEditSrialPortCombo1() //设置主窗口显示串口号组合框的值 {
// TODO: Add your control notification handler code here
((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM1"); ((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM2"); ((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM3"); ((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("COM4");
((CComboBox*)GetDlgItem(IDC_COMBO1))->SetCurSel(3);
}
void CMyDlg::OnChangeEdit1() //滑竿上方编辑框与滑竿关联
{
// TODO: If this is a RICHEDIT control, the control will 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
UpdateData();
((CSliderCtrl*)GetDlgItem(IDC_SLIDER1))->SetPos(m_edit1);
UpdateData(FALSE);
}
void CMyDlg::OnChangeEditSpeed1() //在速度编辑框中写入数值范围提示,其它类似
{
// TODO: If this is a RICHEDIT control, the control will 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
UpdateData(TRUE);
if(m_edit_speed1>8000)
{
MessageBox("请输入一个小于8000的整数");
}
UpdateData(FALSE);
}
void CMyDlg::OnHScroll(UINT nSBCode,UINT nPos, CScrollBar*pScrollBar) //设置时间间隔滚动条
{
// TODO: Add your message handler code here and/or call default
UpdateData();
int nCurPos=pScrollBar->GetScrollPos();
switch(nSBCode)
{
case SB_LINEDOWN:
nCurPos+=1;
break;
case SB_LINEUP:
nCurPos-=1;
break;
case SB_THUMBTRACK:
nCurPos=nPos;
break;
default:
break;
}
pScrollBar->SetScrollPos(nCurPos);
m_editTimer=m_scrollbar2.GetScrollPos();//与编辑框关联
m_editTimer=nCurPos;
UpdateData(FALSE);
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
//设计编辑框文本颜色及背景色
// TODO: Change any attributes of the DC here
//UpdateData(TRUE);
if (pWnd->GetDlgCtrlID() == IDC_SCROLLBAR2)
{
// Set the text color to red
UpdateData(TRUE);
pDC->SetTextColor(RGB(0, 255, 0));
pDC->SetBkMode(TRANSPARENT);
nCtlColor=CTLCOLOR_SCROLLBAR ;
//UpdateData(FALSE);
}
if (pWnd->GetDlgCtrlID() == IDC_EDIT_SPEED1)
{
// Set the text color to red
UpdateData(TRUE);
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255, 0, 0));
//pDC->SetBkColor(RGB(255,0,0));
nCtlColor=CTLCOLOR_EDIT ;//CTLCOLOR_STATIC
HBRUSH b=CreateSolidBrush(RGB (121,121,255));
return b;
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
//删除编辑框内容
void CMyDlg::OndeleteButton5()
{
// TODO: Add your control notification handler code here
//UpdateData(TRUE);
CEdit *pEditSend = (CEdit*)GetDlgItem(IDC_EDIT_RX);
pEditSend->Clear();
pEditSend->RedrawWindow();
UpdateData(FALSE);
}
void CMyEdit::OnLButtonDown(UINT nFlags, CPoint point) //显示窗口中光标选中整行的设置
{
// TODO: Add your message handler code here and/or call default
CEdit::OnLButtonDown(nFlags, point);
int nChar=CharFromPos(point);
int nLine=HIWORD(nChar);
int start=LineIndex(nLine);
int nLen=LineLength(start);
int end=start+nLen;
SetSel(start,end);
}
副窗口函数编写
void CSettingDlg::OnEditchangeCombo1Set() //设置串口数
{
// TODO: Add your control notification handler code here
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM1"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM2"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM3"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM4"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(3); //在组合框的列表框当中选择一个字符串
}
void CSettingDlg::OnOk()
{
// TODO: Add your control notification handler code here
int index;
index=((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->GetCurSel(); //获取当前选择条目的索引值
//((CComboBox*)GetDlgItem(IDC_COMBO1))->GetLBText(index,m_strSalary); //将索引值存在m_strSalary中
m_nPort = index+1; //点击确定后,与主窗口关联
UpdateData(TRUE);
CDialog::OnOK();
}
void CSettingDlg::OnButtonCancel()
{
UpdateData(TRUE);
CDialog::OnCancel();
}
BOOL CSettingDlg::OnInitDialog()
{
CDialog::OnInitDialog();
OnEditchangeCombo1Set() ;
OnEditchangeCombo2Baund();
OnEditchangeCombo3Databit();
OnEditchangeCombo4Stop() ;
OnEditchangeCombo5Check();
OnEditchangeCombo6Control();
UpdateData(TRUE);
UpdateData(FALSE);
switch (m_nPort) //副窗口初始化时将主窗口中对串口号的设置读取 {
case 1:
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))>AddString("COM1"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(0);
break;
case 2:
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM2"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(1); break;
case 3:
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM3"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(2); break;
case 4
((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->AddString("COM4"); ((CComboBox*)GetDlgItem(IDC_COMBO1_SET))->SetCurSel(3); break;
UpdateData(FALSE);
}
return TRUE;
}