MSP430F149和PC机串口通信问题

来源:互联网 发布:淘宝店铺审核在哪看 编辑:程序博客网 时间:2024/04/29 02:10
Code:
  1. //******************************************************************************   
  2.   
  3. #include "msp430x14x.h"                     // Standard Equations   
  4. typedef unsigned char uchar ;   
  5. typedef unsigned int uint ;   
  6. /*******************************************   
  7. functional module    
  8. *******************************************/   
  9. void delay_l(int q);   
  10. void ClockInit() ;                          //时钟初始化   
  11. void UartInit() ;                           //串口通信初始化函数   
  12. void SendDataToPC(uchar *value) ;           //发送数据函数   
  13. void SendDataToPC1(uchar value) ;   
  14. void ADDataTodecimal(uchar *p,uint value) ; //十六进制转换十进制函数   
  15.   
  16. uchar value[5] ;                            //用来存放把一个十六进制熟转换为十   
  17.   
  18. uint temp = 0 ;                             //Function as exchange data   
  19. uchar i,j ;     
  20. /********************************************  
  21. main function   
  22. ********************************************/  
  23. void main(void)   
  24. {    
  25.   WDTCTL = WDTPW+WDTHOLD;                 // Stop watchdog timer   
  26.   value[4] = '/0' ;                       // ADD A END SIGN TO          
  27.   ClockInit();   
  28.      
  29.   UartInit();                             //Initial series communication     
  30.      
  31.   P6SEL |= 0x10 ;                         // Enable A/D channel A3   
  32.   ADC12CTL0 = ADC12ON |REFON |REF2_5V ;                   // Turn on ADC12, set sampling time   
  33.   ADC12CTL1 = SHS_1 + SHP + CONSEQ_2 ;    // Use sampling timer, set mode   
  34.   ADC12MCTL0 |= INCH_4 ;                  //Select input signal from A0 channel   
  35.   delay_l(6) ;   
  36.   ADC12CTL0 |= ENC ;                      // Enable conversions   
  37.      
  38.   CCTL0 = CCIE ;                        //Enable timerA Interrupt   
  39.   TACCR0 = 4000;                         //Sample frequence about 1000Hz   
  40.   TACCR1 = 2000 ;   
  41.   TACCTL1 = OUTMOD_3 ;                    //PWM set/reset   
  42.   TACTL = TASSEL_2 + MC_1 + TACLR ;       //Use increase count mode    
  43.   //TACTL = TASSEL_2 + MC_1 ;    
  44.   _BIS_SR(LPM0_bits + GIE) ;              // Enter LPM0 LOW-POWER0 MODE     
  45.                 
  46. }   
  47. /********************************************  
  48. //Intialized the SMCLK Clock  
  49. *********************************************/  
  50. void ClockInit()   
  51. {   
  52.   BCSCTL1 &=~ XT2OFF ;   
  53.   do  
  54.   {   
  55.     IFG1 &=~ OFIFG ;   
  56.     for(uint i=0xff;i>0;i--) ;   
  57.   }   
  58.   while((IFG1 & OFIFG));   
  59.             
  60.   BCSCTL1 |= SELS | DIVS1 | DIVS0 ;      
  61. }   
  62. /*******************************************  
  63. USART初始化,选择波特率为9600,8位,无校验位  
  64. *******************************************/  
  65. void UartInit()   
  66. {   
  67.   P3SEL |= 0x30 ;              // P3.4,5 = USART0 TXD/RXD   
  68.   ME1 |= URXE0 + UTXE0 ;       // Enable USART0 T/RXD   
  69.   UCTL0 |= CHAR ;              // 8-bit character   
  70.   UTCTL0 |= SSEL0 ;            // UCLK = ACLK   
  71.   //**Common used baud rates********************/   
  72.   // UCLK---------Clock----------32k---------ACLK    
  73.   // UxBR1=0x0,UxBR0=0x1B,UxMCTL=0x03---1200bit/s   
  74.   UBR10 = 0x00 ; //SET BAUD    // UxBR1=0x0,UxBR0=0x0D,UxMCTL=0x6B---2400bit/s   
  75.   UBR00 = 0x06 ;               // UxBR1=0x0,UxBR0=0x06,UxMCTL=0x6F---4800bit/s   
  76.   UMCTL0 = 0x6f ;//Modulation  // UxBR1=0x0,UxBR0=x03,UxMCTL=0x4A---9600bit/s   
  77.   //************BAUD=2400Bit/s******************/       
  78.   UCTL0 &= ~SWRST ;            // Initialize USART state machine   
  79.   IE1 |= URXIE0 ;            //Open Transmit interrupt   
  80. }   
  81. /************************************************  
  82. 定时器A中断服务程序把AD采集的数据放到  
  83. results[Num_of_Results]  
  84. ************************************************/  
  85. #pragma vector=TIMERA0_VECTOR             //TIMERA0_VECTOR INTERRUPT SERVICE    
  86. __interrupt void TIMERAISR (void)         //ROUTINE    
  87. {   
  88.   _BIC_SR(LPM0_bits) ;    
  89.   temp = ADC12MEM0 ;                    //SAVE THE CONVERSATIN RESULT   
  90.   ADDataTodecimal(value,temp);         //Conversation HEX into DECIMAL   
  91.   for(j = 0;j <= 3;j++)   
  92.   {    
  93.     value[j] += 0x30 ;               //Conversation the DECIMAL into     
  94.   }    
  95.   SendDataToPC(value);                //Transmit a serial of characters    
  96.   //for(uint k=0x7f;k>0;k--);   
  97. }                                           //per time    
  98. #pragma vector = UART0RX_VECTOR   
  99. __interrupt void receive_ISR(void)   
  100. {   
  101.   _BIC_SR_IRQ(LPM3_bits);               // Clear LPM3 bits from 0(SR)   
  102.   if ( RXBUF0 == 0x31 )   
  103.   {   
  104.     UCTL0 |= SWRST;   
  105.     ME1 &= ~UTXE0;   
  106.     UCTL0 &= ~SWRST;   
  107.   }   
  108.   if ( RXBUF0 == 0x32 )   
  109.   {   
  110.     UCTL0 |= SWRST;   
  111.     ME1 |= URXE0 + UTXE0 ;       // Enable USART0 T/RXD   
  112.     UCTL0 &= ~SWRST;   
  113.   }    
  114. }   
  115. /***********************************  
  116. 功能:发送数据给PC机  
  117. 参数:*P指向被发送的数据的缓冲区  
  118. 还回值:无  
  119. 发送3位数据-----参考下面发送4位程序  
  120. ***********************************/  
  121. void SendDataToPC(uchar *value)   
  122. {   
  123.   while(*value != '/0')            //JUDGE A CHARACTER IS END SIGN OR NOT   
  124.   {   
  125.     while(!(IFG1 & UTXIFG0)) ;//TRANSMIT COMPLETELY OR NOT   
  126.     TXBUF0 = *value ;   
  127.     value++ ;    
  128.   }   
  129.   while(!(IFG1 & UTXIFG0)) ;   
  130.   TXBUF0 = '/n' ;   
  131. }   
  132. /*****************************/  
  133. void SendDataToPC1(uchar value)   
  134. {   
  135.      
  136.   while(!(IFG1 & UTXIFG0)) ;   
  137.   TXBUF0 = value ;   
  138.   while(!(IFG1 & UTXIFG0)) ;   
  139.   TXBUF0 = '/n' ;   
  140. }   
  141. /********************************************  
  142. 函数名称:ADDataTodecimal  
  143. 功    能:将16进制ADC转换数据变换成十进制  
  144.           表示形式  
  145. 参    数:value--16进制数据    
  146.           p--指向存放转换结果的指针  
  147. 返回值  :无  
  148. ********************************************/  
  149. void ADDataTodecimal(uchar *p,uint value)   
  150. {   
  151.     p[0] = value / 1000 ;                          //THE THOUSAND BIT   
  152.     p[1] = (value - p[0]*1000)/100 ;               //THE HUNDRED BIT   
  153.     p[2] = (value - p[0]*1000 - p[1]*100)/10 ;     //THE DECIMAL BIT   
  154.     p[3] = (value - p[0]*1000 - p[1]*100 - p[2]*10);// THE INDIVIDUAL BIT   
  155. }      
  156.   
  157. /*******************************  
  158. 功能:延时
  159. ************************** 
  160. void delay_l(int q)   
  161. {   
  162.  int  a,b;   
  163.  for(a=0; a<q; a++ )   
  164.    for(b=0;b<30000;b++);    
  165. }   
  166.   
  167. 上面的程序是MSP430F149的采用定时器定时触发A/D转换,并把转换好的数据发送到PC机,   
  168.   

 

Code:
  1. // FormComDlg.cpp : implementation file   
  2. //   
  3.   
  4. #include "stdafx.h"   
  5. #include "FormCom.h"   
  6. #include "FormComDlg.h"   
  7. #include "time.h"   
  8.   
  9. #ifdef _DEBUG   
  10. #define new DEBUG_NEW   
  11. #undef THIS_FILE   
  12. static char THIS_FILE[] = __FILE__;   
  13. #endif   
  14.   
  15. /////////////////////////////////////////////////////////////////////////////   
  16. // CAboutDlg dialog used for App About   
  17. //int data[4096];   
  18. //#define Num 1000    
  19. class CAboutDlg : public CDialog   
  20. {   
  21. public:   
  22.  CAboutDlg();   
  23.   
  24. // Dialog Data   
  25.  //{{AFX_DATA(CAboutDlg)   
  26.  enum { IDD = IDD_ABOUTBOX };   
  27.  //}}AFX_DATA   
  28.   
  29.  // ClassWizard generated virtual function overrides   
  30.  //{{AFX_VIRTUAL(CAboutDlg)   
  31.  protected:   
  32.  virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support   
  33.  //}}AFX_VIRTUAL   
  34.   
  35. // Implementation   
  36. protected:   
  37.  //{{AFX_MSG(CAboutDlg)   
  38.  //}}AFX_MSG   
  39.  DECLARE_MESSAGE_MAP()   
  40. };   
  41.   
  42. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)   
  43. {   
  44.  //{{AFX_DATA_INIT(CAboutDlg)   
  45.  //}}AFX_DATA_INIT   
  46. }   
  47.   
  48. void CAboutDlg::DoDataExchange(CDataExchange* pDX)   
  49. {   
  50.  CDialog::DoDataExchange(pDX);   
  51.  //{{AFX_DATA_MAP(CAboutDlg)   
  52.  //}}AFX_DATA_MAP   
  53. }   
  54.   
  55. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)   
  56.  //{{AFX_MSG_MAP(CAboutDlg)   
  57.   // No message handlers   
  58.  //}}AFX_MSG_MAP   
  59. END_MESSAGE_MAP()   
  60.   
  61. /////////////////////////////////////////////////////////////////////////////   
  62. // CFormComDlg dialog   
  63.   
  64. CFormComDlg::CFormComDlg(CWnd* pParent /*=NULL*/)   
  65.  : CDialog(CFormComDlg::IDD, pParent)   
  66. {   
  67.  //{{AFX_DATA_INIT(CFormComDlg)   
  68.  m_strReceiveData = _T("");   
  69.  m_selcom = _T("");   
  70.  m_selbaud = _T("");   
  71.  //}}AFX_DATA_INIT   
  72.  // Note that LoadIcon does not require a subsequent DestroyIcon in Win32   
  73.  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);   
  74. }   
  75.   
  76. void CFormComDlg::DoDataExchange(CDataExchange* pDX)   
  77. {   
  78.  CDialog::DoDataExchange(pDX);   
  79.  //{{AFX_DATA_MAP(CFormComDlg)   
  80.  DDX_Control(pDX, IDC_COMBO3, m_sendControlInfo);   
  81.  DDX_Control(pDX, IDC_COMBO2, m_selbuadd);   
  82.  DDX_Control(pDX, IDC_COMBO1, m_selcomm);   
  83.  DDX_Control(pDX, IDC_EDIT2, m_ctrReceiveData);   
  84.  DDX_Control(pDX, IDC_MSCOMM1, m_ctrComm);   
  85.  DDX_Text(pDX, IDC_EDIT2, m_strReceiveData);   
  86.  DDX_CBString(pDX, IDC_COMBO1, m_selcom);   
  87.  DDX_CBString(pDX, IDC_COMBO2, m_selbaud);   
  88.  //}}AFX_DATA_MAP   
  89. }   
  90.   
  91. BEGIN_MESSAGE_MAP(CFormComDlg, CDialog)   
  92.  //{{AFX_MSG_MAP(CFormComDlg)   
  93.  ON_WM_SYSCOMMAND()   
  94.  ON_WM_PAINT()   
  95.  ON_WM_QUERYDRAGICON()   
  96.  ON_BN_CLICKED(IDC_BUTTON1_CLEARRECE, OnClearReceiveData)   
  97.  ON_WM_TIMER()   
  98.  ON_BN_CLICKED(IDC_SendData, OnSendData)   
  99.  ON_BN_CLICKED(IDC_BUTTON1, On_CloseSerial)   
  100.  //}}AFX_MSG_MAP   
  101. END_MESSAGE_MAP()   
  102.   
  103. /////////////////////////////////////////////////////////////////////////////   
  104. // CFormComDlg message handlers   
  105.   
  106. BOOL CFormComDlg::OnInitDialog()   
  107. {   
  108.  CDialog::OnInitDialog();   
  109.   
  110.  // Add "About..." menu item to system menu.   
  111.   
  112.  // IDM_ABOUTBOX must be in the system command range.   
  113.  ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);   
  114.  ASSERT(IDM_ABOUTBOX < 0xF000);   
  115.   
  116.  CMenu* pSysMenu = GetSystemMenu(FALSE);   
  117.  if (pSysMenu != NULL)   
  118.  {   
  119.   CString strAboutMenu;   
  120.   strAboutMenu.LoadString(IDS_ABOUTBOX);   
  121.   if (!strAboutMenu.IsEmpty())   
  122.   {   
  123.    pSysMenu->AppendMenu(MF_SEPARATOR);   
  124.    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);   
  125.   }   
  126.  }   
  127.   
  128.  // Set the icon for this dialog.  The framework does this automatically   
  129.  //  when the application's main window is not a dialog   
  130.  SetIcon(m_hIcon, TRUE);   // Set big icon   
  131.  SetIcon(m_hIcon, FALSE);  // Set small icon   
  132.     
  133.  // TODO: Add extra initialization here   
  134.  for(int i=0;i<ECGNum;i++)ECGdata[i]=0;   
  135.  ecgnum=0;   
  136. // if (m_selcomm.GetCurSel()<0)   
  137. //  i = 2;   
  138. // else   
  139. //  i = m_selcomm.GetCurSel()+1;   
  140.  m_ctrComm.SetCommPort(2);//选择端口COM2   
  141. // if (m_selbuadd.GetCurSel()<0)   
  142. //  i = 2;   
  143. // else   
  144. ///  i = m_selbuadd.GetCurSel();   
  145.  //int n=m_selbuadd.GetLBTextLen(i);   
  146. // CString str;   
  147. // m_selbuadd.GetLBText(i,str);   
  148. // str += ",n,8,1";   
  149.  m_ctrComm.SetInputMode(1);//输入方式为二进制   
  150.  m_ctrComm.SetInBufferSize(4096);//设置输入缓冲区大小   
  151.  m_ctrComm.SetOutBufferSize(512);//设置输出缓冲区大小   
  152.  m_ctrComm.SetSettings("4800,n,8,1");//设置串口4800,N,8,1   
  153.  if(!m_ctrComm.GetPortOpen())//检测串口是否打开   
  154.   m_ctrComm.SetPortOpen(true);//打开串口   
  155.  m_ctrComm.SetRThreshold(1);//参数1表示当串口接收缓冲区中有多于1或者等于1个   
  156.                             //字符将引发一个接收数据的OnCom事件   
  157.  m_ctrComm.SetInputLen(0);//设置当前接收数据的长度为0   
  158.  m_ctrComm.GetInput();//先预留缓冲区以清除残留数据   
  159.   
  160.     
  161.  return TRUE;  // return TRUE  unless you set the focus to a control   
  162. }   
  163.   
  164. void CFormComDlg::OnSysCommand(UINT nID, LPARAM lParam)   
  165. {   
  166.  if ((nID & 0xFFF0) == IDM_ABOUTBOX)   
  167.  {   
  168.   CAboutDlg dlgAbout;   
  169.   dlgAbout.DoModal();   
  170.  }   
  171.  else  
  172.  {   
  173.   CDialog::OnSysCommand(nID, lParam);   
  174.  }   
  175. }   
  176.   
  177. // If you add a minimize button to your dialog, you will need the code below   
  178. //  to draw the icon.  For MFC applications using the document/view model,   
  179. //  this is automatically done for you by the framework.   
  180.   
  181. void CFormComDlg::OnPaint()    
  182. {   
  183.  if (IsIconic())   
  184.  {   
  185.   CPaintDC dc(this); // device context for painting   
  186.   
  187.   SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);   
  188.   
  189.   // Center icon in client rectangle   
  190.   int cxIcon = GetSystemMetrics(SM_CXICON);   
  191.   int cyIcon = GetSystemMetrics(SM_CYICON);   
  192.   CRect rect;   
  193.   GetClientRect(&rect);   
  194.   int x = (rect.Width() - cxIcon + 1) / 2;   
  195.   int y = (rect.Height() - cyIcon + 1) / 2;   
  196.   
  197.   // Draw the icon   
  198.   dc.DrawIcon(x, y, m_hIcon);   
  199.  }   
  200.  else  
  201.  {   
  202.   CDialog::OnPaint();   
  203.  }   
  204. }   
  205.   
  206. // The system calls this to obtain the cursor to display while the user drags   
  207. //  the minimized window.   
  208. HCURSOR CFormComDlg::OnQueryDragIcon()   
  209. {   
  210.  return (HCURSOR) m_hIcon;   
  211. }   
  212.   
  213. BEGIN_EVENTSINK_MAP(CFormComDlg, CDialog)   
  214.     //{{AFX_EVENTSINK_MAP(CFormComDlg)   
  215.  ON_EVENT(CFormComDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)   
  216.  //}}AFX_EVENTSINK_MAP   
  217. END_EVENTSINK_MAP()   
  218.   
  219. /************************************************************************/  
  220. void CFormComDlg::OnComm()    
  221. {   
  222.  //SetTimer(1,100,NULL);   
  223.  LONG len=0,k=0;//定义从串口发送过来是数据长度   
  224.  BYTE rxdata[1200];//定义一个存放从串口发送数据的BYTE类型的数组   
  225.  CString strtemp=_T("0");//定义CString类型的变量实现BYTE类型转换为CString类型   
  226.  // TODO: Add your control notification handler code here   
  227.  COleVariant variant_inp;   
  228.  COleSafeArray safearray_inp;//定义一个COleSafeArray的类型的变量实现串口数据类型与   
  229.  if(m_ctrComm.GetCommEvent()==2)//判断串口接收缓冲区有没接收到数据   
  230.  {    
  231.   variant_inp.Attach(m_ctrComm.GetInput());//把串口缓冲区的数据读到variant_inp结构体中   
  232.   safearray_inp=variant_inp;   
  233.   len=safearray_inp.GetOneDimSize();   
  234.   BOOL flag;   
  235.   for(k=0;k<len;k++)   
  236.    safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE数组   
  237.   for(k=0;k<len;k++)//把BYTE的数组转换为CString    
  238.   {   
  239.    BYTE bt=*(char*)(rxdata+k);   
  240.    strtemp.Format("%c",bt);   
  241.    m_strReceiveData+=strtemp;//m_strReceiveData用来存储串口接收的所有数据   
  242.    flag =true;   
  243.    while(flag)//判断从串口接收的数据是否大于等于5个字节,如果满足这个条件   
  244.    {    
  245.     if(m_strReceiveData.GetLength()<5)   
  246.      flag = false;   
  247.     else  
  248.     {   
  249.      strtemp=m_strReceiveData.Left(4);//取conv字符串中的最左边四个字符,因为我发送过来的数据是5个字节,   
  250.             //其中最后一个字节是一个‘/n   
  251.     data[ecgnum]=_ttoi(strtemp);//两种数据的转换   
  252.      num=(ecgnum+1)%ECGNum;//数组长度加1   
  253.      m_strReceiveData.Delete(1,5);//删除已经转换的字符串   
  254.      OnTimer(1);   
  255.     }   
  256.    }    
  257.    strtemp=_T("0");//重新给这个变量进行初始化     
  258.   }   
  259.  }   
  260.  UpdateData(false);   
  261. }   
  262.   
  263. void CFormComDlg::OnClearReceiveData()    
  264. {   
  265.  // TODO: Add your control notification handler code here   
  266.  m_strReceiveData.Empty();//CLEAR THE CONTENT OF m_strReceiveData   
  267.  for (int i = 0;i<=num-1;i++);   
  268.   data[i] = 0;   
  269.  num = 0;   
  270.  UpdateData(false);//UPDATE THE DRAWING AREA,In principle the aim of this item    
  271.     
  272. }   
  273. void CFormComDlg::DrawECG(CDC* pDC)   
  274. {   
  275.  int i=0;   
  276.  CRect rec;   
  277.   
  278.  CWnd* pWnd=GetDlgItem(IDC_STATIC12);//DIRECT TO DRAWING CONTROL    
  279.  pWnd->GetClientRect(&rec);//DIRECT TO THE CLIENT PICTURE AREA   
  280.  pDC->Rectangle(&rec);//DIRECTED THE PICTURE AREA    
  281.   
  282.   
  283.  CPen* pPenRed = new CPen;//RED PEN   
  284.  pPenRed->CreatePen(PS_SOLID,1,RGB(255,0, 0));   
  285.  CPen* pOldPen=pDC->SelectObject(pPenRed);   
  286.  int temp = 0;   
  287.  for(i=0;i<num-1;i++)//pay attention to the consistent with coordinate between two point   
  288.  {           
  289.   
  290.   pDC->MoveTo(rec.left+temp,data[i]/10-30);                         
  291.   pDC->LineTo(rec.left+1+temp,data[i+1]/10-30);                                                             
  292.   if(rec.left+1+temp>rec.Width()-10)   
  293.   {   
  294.    temp = 0;   
  295.   }   
  296.   else  
  297.    temp = temp + 1;   
  298.  }   
  299.  pDC->SelectObject(pOldPen);   
  300.   
  301.  pWnd->ReleaseDC(pDC);   
  302.  delete pPenRed;   
  303. }   
  304. /**********************************************************  
  305. /**********************************************************/  
  306. void CFormComDlg::OnTimer(UINT nIDEvent)    
  307. {   
  308.  // TODO: Add your message handler code here and/or call default   
  309.  CRect rect;   
  310.     
  311.  CWnd* pWnd = GetDlgItem(IDC_STATIC12);   
  312.     
  313.  pWnd->GetClientRect(&rect);   
  314.     
  315.  CDC* pDC = pWnd->GetDC();    
  316.  pWnd->Invalidate();   
  317.  pWnd->UpdateWindow();   
  318.   
  319.  CBitmap memBitmap;   
  320.  CBitmap* pOldBmp = NULL;   
  321.  memDC.CreateCompatibleDC(pDC);   
  322.  memBitmap.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);   
  323.  pOldBmp = memDC.SelectObject(&memBitmap);   
  324.  memDC.BitBlt(rect.left,rect.top,rect.right,rect.bottom,pDC,0,0,SRCCOPY);   
  325.  DrawECG(&memDC);   
  326.  pDC->BitBlt(rect.left,rect.top,rect.right,rect.bottom,&memDC,0,0,SRCCOPY);   
  327.   
  328.  memDC.SelectObject(pOldBmp);   
  329.  memDC.DeleteDC();   
  330.  memBitmap.DeleteObject();   
  331.  pWnd->ReleaseDC(pDC);   
  332.  CDialog::OnTimer(nIDEvent);   
  333. }   
  334. void CFormComDlg::OnSendData()    
  335. {   
  336.  // TODO: Add your control notification handler code here   
  337.  CString s;   
  338.  int i;   
  339.  if (m_sendControlInfo.GetCurSel()<0)   
  340.   i=m_sendControlInfo.SetCurSel(1);   
  341.  else  
  342.   i = m_sendControlInfo.GetCurSel();   
  343.  m_sendControlInfo.GetLBText(i,s);   
  344.   
  345. m_ctrComm.SetOutput(COleVariant(s));   
  346.   
  347. }   
  348.   
  349. void CFormComDlg::On_CloseSerial()    
  350. {   
  351.  // TODO: Add your control notification handler code here   
  352.  if (m_ctrComm.GetPortOpen())   
  353.  {   
  354.   m_ctrComm.SetPortOpen(false);   
  355.  }   
  356.  else  
  357.  {   
  358.   m_ctrComm.SetPortOpen(true);   
  359.  }   
  360. }   
  361.   
  362.   
  363. 上面是MFC中的一部分程序   
  364.   

PC机可以正常的接收来自MSP430F149发送过来的数据,问题是,当我在关闭上位机的串口,然后重新打开串口接收时,就会出现乱码,有时是重新打开又可以正常接收;不过大多数还是显示乱码;

另外 PC机在正常接收时,当我点击COM选择组合框时,组合框的内容不停的闪烁,点击buadrate组合框时,也会不停的闪烁;不知道是什么问题?

本打算在上位机上发送 1 和 2来通知 MSP430F149关闭和打开串口结果是 发送1让MSP430F149停止发送可以实现,

1: PC机发送1给MSP430  通知MSP430关闭串口;

2:PC机发送2 给MSP430 通知MSP430打开串口,进行通信;

当发送2时,让MSP430F149重新打开串口,进行通信时就不能实现,请问各路高手 这到底是什么问题?

PC机程序如下

Code:
  1. void CFormComDlg::OnSendData()    
  2. {   
  3.     // TODO: Add your control notification handler code here   
  4.   
  5.     CString s;   
  6.     int i;   
  7.   
  8.     i = m_sendControlInfo.GetCurSel();   
  9.     m_sendControlInfo.GetLBText(i,s);   
  10.   
  11.     m_ctrComm.SetOutput(COleVariant(s));   
  12.   
  13. }  

MSP430F149的对应的程序如下

Code:
  1. #pragma vector = UART0RX_VECTOR   
  2. __interrupt void receive_ISR(void)   
  3. {   
  4.   _BIC_SR_IRQ(LPM3_bits);                  
  5.   if ( RXBUF0 == 0x31 )   
  6.   {   
  7.     UCTL0 |= SWRST;   
  8.     ME1 &= ~UTXE0;                   //关闭串口发送功能   
  9.     UCTL0 &= ~SWRST;   
  10.   }   
  11.   if ( RXBUF0 == 0x32 )   
  12.   {   
  13.     UCTL0 |= SWRST;   
  14.     ME1 |= URXE0 + UTXE0 ;       // Enable USART0 T/RXD   
  15.     UCTL0 &= ~SWRST;   
  16.   
  17.   }    
  18. }  

发现采用上面的办法 不能实现串口的关和闭,又采用用MS COMM控件来实现串口的关和闭 ,在正常情况下MSP430发送数据给PC机,PC机可以正常接收,当按下CloseSerial来关闭串口时,然后过会儿重新按下CloseSerial按钮 进行重新接收MSP430发送过来的数据时,就会出现乱码,请问高手用什么办法可以解决这个问题?PC机的程序如下

Code:
  1.   
  2. void CFormComDlg::On_CloseSerial()    
  3. {   
  4.     // TODO: Add your control notification handler code here   
  5.     if (m_ctrComm.GetPortOpen())   
  6.     {   
  7.         m_ctrComm.SetPortOpen(false);   
  8.     }   
  9.     else  
  10.     {   
  11.         m_ctrComm.SetPortOpen(true);   
  12.     }   
  13. }  

 

我的联系QQ 258130629

小弟 在此先谢谢各位高手啦!

原创粉丝点击