以下是我的源程序 结果出来的是只能从串口助手端输入2位数据,当输入的数据大于2位的时候就会出现上面图形中的结果,求大神搭救啊 在线等着啊
源程序如下:
#include "stdafx.h"
#include "SCommtest.h"
#include "SCommtestDlg.h"
void CSCommtestDlg::OnComm()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integer that is not signed.
CString strtemp;
if(m_ctrlComm.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
{
variant_inp=m_ctrlComm.GetInput(); //读缓冲区
safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
len=safearray_inp.GetOneDimSize(); //得到有效数据长度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
for(k=0;k<len;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k); //字符型
strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
m_strRXData+=strtemp; //加入接收编辑框对应字符串
}
}
UpdateData(FALSE); //更新编辑框内容
}
void CSCommtestDlg::OnButtonManualsend()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE); //读取编辑框内容
m_ctrlComm.SetOutput(COleVariant(m_strTXData));//发送数据
}
void CSCommtestDlg::OnWave()
{
// TODO: Add your control notification handler code here
CClientDC dc(GetDlgItem(IDC_STATICw));
dc.FillSolidRect ( CRect(0,0,550,450) , RGB(170,200,150) ) ; //绘制背景,浅绿色
CPen pen_BB,pen_B,*pOldPen; //定义画笔
pen_BB.CreatePen(PS_SOLID,2,RGB(0,80,255)); //创建蓝色画笔
pen_B.CreatePen(PS_SOLID,1,RGB(0,100,200)); //创建蓝色细画笔
pOldPen=dc.SelectObject(&pen_BB); //把蓝色画笔选入DC,保存原画笔
dc.MoveTo(50,10); //画边框
dc.LineTo(530,10);
dc.LineTo(530,410);
dc.LineTo(50,410);
dc.LineTo(50,10);
pOldPen=dc.SelectObject(&pen_B); //把蓝色细画笔选入DC,保存原画笔
for(int i=1;i<50;i++)
{
dc.MoveTo(50,10+i*8);
dc.LineTo(530,10+i*8);
}
for(i=1;i<60;i++)
{
dc.MoveTo(50+i*8,10);
dc.LineTo(50+i*8,410);
}
pOldPen=dc.SelectObject(&pen_BB); //把蓝色画笔选入DC,保存原画笔
for(i=1;i<10;i++)
{
dc.MoveTo(50,10+i*40);
dc.LineTo(530,10+i*40);
}
for(i=1;i<12;i++)
{
dc.MoveTo(50+i*40,10);
dc.LineTo(50+i*40,410);
}
LOGFONT lf; //字
memset(&lf,0,sizeof(LOGFONT));
lf.lfHeight=-13;
lf.lfCharSet=GB2312_CHARSET;
strcpy((LPSTR) &(lf.lfFaceName),"黑体");
CFont cf;
cf.CreateFontIndirect(&lf);
CFont *oldfont=dc.SelectObject(&cf);
//dc.SetTextColor(RGB(0,80,255)); //定义字体为蓝色
dc.TextOut(5,5,"数"); //纵轴
dc.TextOut(5,18,"据");
dc.TextOut(25,5,"100");
dc.TextOut(30,45,"90");
dc.TextOut(30,85,"80");
dc.TextOut(30,125,"70");
dc.TextOut(30,165,"60");
dc.TextOut(30,205,"50");
dc.TextOut(30,245,"40");
dc.TextOut(30,285,"30");
dc.TextOut(30,325,"20");
dc.TextOut(30,365,"10");
dc.TextOut(35,415,"0");
dc.TextOut(500,430,"时间/s"); //横轴
dc.TextOut(122,415,"10");
dc.TextOut(202,415,"20");
dc.TextOut(282,415,"30");
dc.TextOut(362,415,"40");
dc.TextOut(442,415,"50");
dc.TextOut(522,415,"60");
dc.SelectObject(oldfont);
cf.DeleteObject();
}
int j=0,k=0;
void CSCommtestDlg::OnRefesh()
{
// TODO: Add your control notification handler code here
CSCommtestDlg::OnWave();
j=1;
}
void CSCommtestDlg::OnTimer(UINT nIDEvent) //自动执行的代码
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
OnButtonManualsend();
else if(nIDEvent==2)
{
int t,count;
static int tt;
CClientDC dc(GetDlgItem(IDC_STATICw));
CPen pen_R,*pOldPen; //定义红色画笔
pen_R.CreatePen(PS_SOLID,2,RGB(255,0,0)); //创建红色画笔
pOldPen=dc.SelectObject(&pen_R); //把红色画笔选入DC,保存原画笔
//m_strRXData="27\n28\n29\n2";
count=m_strRXData.GetLength();
count=count-(count%3+3);
CString temp=m_strRXData.Mid(count,3);//从count位开始取三个字符
t=atoi(temp);//若是这样 t就是串口数据直接转换成整数的值,比如m_strRXData=11111,则t=11111
if(k==0)
{
dc.MoveTo(42,410-t*4);
k++;
j++;
tt=t;
}
else if(j<=60)
{
if(t<=100)
{
dc.MoveTo(50+8*(j-1),410-tt*4);//将画笔移动到这个位置
tt=t;
dc.LineTo(50+8*j++,410-t*4);//从moveto的位置为起点画线到lineto后面的点
}
else if(t>100&&t<1000)
{ int m=t/100;
dc.MoveTo(50+8*(j-1),410-tt*4);
tt=t;
dc.LineTo(50+8*j++,410-(t-m*100)*4);
}
}
else if(m_bAutoRefresh&&j>60)
CSCommtestDlg::OnRefesh();
}
CDialog::OnTimer(nIDEvent);
}
void CSCommtestDlg::OnCheckAutosend() //自动发送定时
{
// TODO: Add your control notification handler code here
m_bAutoSend=!m_bAutoSend;
if(m_bAutoSend)
{
SetTimer(1,10,NULL);//时间为1000毫秒
}
else
{
KillTimer(1); //取消定时
}
}
void CSCommtestDlg::OnAutoWave() //自动显示定时
{
// TODO: Add your control notification handler code here
m_bAutoWave=!m_bAutoWave;
if(m_bAutoWave)
{
SetTimer(2,1000,NULL);//时间为1000毫秒
}
else
{
KillTimer(2);//取消定时
}
}
void CSCommtestDlg::OnCHECKAutoRefresh()
{
// TODO: Add your control notification handler code here
m_bAutoRefresh=!m_bAutoRefresh;
}
void CSCommtestDlg::OnExit() //退出程序
{
// TODO: Add your control notification handler code here
PostQuitMessage(0); //Send WM_DESTROY Message
}
void CSCommtestDlg::OnPortOpen() //打开串口
{
// TODO: Add your control notification handler code here
if( !m_ctrlComm.GetPortOpen()) //打开串口
m_ctrlComm.SetPortOpen(TRUE);
else if(m_ctrlComm.GetPortOpen())
AfxMessageBox("串口已打开");
else
AfxMessageBox("无法打开串口");
}
void CSCommtestDlg::OnPortClose() //关闭串口
{
// TODO: Add your control notification handler code here
if(m_ctrlComm.GetPortOpen())
m_ctrlComm.SetPortOpen(FALSE);
}
void CSCommtestDlg::OnSelchangeCOMBORate() //改变波特率
{
// TODO: Add your control notification handler code here
CString rt;
int r;
m_Rate.GetLBText(m_Rate.GetCurSel(),rt);
r=atoi(rt);
switch(r)
{
case 600:
m_ctrlComm.SetSettings("600,n,8,1"); break;
case 1200:
m_ctrlComm.SetSettings("1200,n,8,1"); break;
case 4800:
m_ctrlComm.SetSettings("4800,n,8,1"); break;
case 9600:
m_ctrlComm.SetSettings("9600,n,8,1"); break;
case 14400:
m_ctrlComm.SetSettings("14400,n,8,1"); break;
default:
MessageBox("设置波特率出错");
}
}
void CSCommtestDlg::OnSelchangeCOMBOComNum() //改变串口
{
// TODO: Add your control notification handler code here
CString cn;
m_ComNum.GetLBText(m_ComNum.GetCurSel(),cn);
//int c=atoi(cn);
//m_ctrlComm.SetCommPort(c);
char c=cn.GetAt(3);
switch(c)
{
case '1':
m_ctrlComm.SetCommPort(1); break;
case '2':
m_ctrlComm.SetCommPort(2); break;
case '3':
m_ctrlComm.SetCommPort(3); break;
case '4':
m_ctrlComm.SetCommPort(4); break;
case '5':
m_ctrlComm.SetCommPort(5); break;
default:
MessageBox("设置端口出错");
}
}
void CSCommtestDlg::OnBUTTONClearRXD()
{
// TODO: Add your control notification handler code here
int count;
count=m_strRXData.GetLength();
count=count-count%3;
m_strRXData.Delete(0,count-3);
UpdateData(FALSE); //更新编辑框内容
}
void CSCommtestDlg::OnBUTTONClearTXD()
{
// TODO: Add your control notification handler code here
m_strTXData.Empty();
UpdateData(FALSE); //更新编辑框内容
}
void CSCommtestDlg::OnChangeEditTxdata()
{
// 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
}
void CSCommtestDlg::OnChangeEditRxdata()
{
// 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
}
void CSCommtestDlg::OnSTATICw()
{
// TODO: Add your control notification handler code here
}