搜索
bottom↓
回复: 14

关于vc上位机 的cserialport类的问题(已解决)

[复制链接]

出0入0汤圆

发表于 2014-6-6 13:43:07 | 显示全部楼层 |阅读模式
本帖最后由 FSL_TICS_ZP 于 2014-7-2 21:57 编辑
  1. // practise4Dlg.cpp : implementation file
  2. //

  3. #include "stdafx.h"
  4. #include "practise4.h"
  5. #include "practise4Dlg.h"

  6. //



  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif

  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About

  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17.         CAboutDlg();

  18. // Dialog Data
  19.         //{{AFX_DATA(CAboutDlg)
  20.         enum { IDD = IDD_ABOUTBOX };
  21.         //}}AFX_DATA

  22.         // ClassWizard generated virtual function overrides
  23.         //{{AFX_VIRTUAL(CAboutDlg)
  24.         protected:
  25.         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26.         //}}AFX_VIRTUAL

  27. // Implementation
  28. protected:
  29.         //{{AFX_MSG(CAboutDlg)
  30.         //}}AFX_MSG
  31.         DECLARE_MESSAGE_MAP()
  32. };

  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35.         //{{AFX_DATA_INIT(CAboutDlg)
  36.         //}}AFX_DATA_INIT
  37. }

  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40.         CDialog::DoDataExchange(pDX);
  41.         //{{AFX_DATA_MAP(CAboutDlg)
  42.         //}}AFX_DATA_MAP
  43. }

  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45.         //{{AFX_MSG_MAP(CAboutDlg)
  46.                 // No message handlers
  47.         //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()

  49. /////////////////////////////////////////////////////////////////////////////
  50. // CPractise4Dlg dialog

  51. CPractise4Dlg::CPractise4Dlg(CWnd* pParent /*=NULL*/)
  52.         : CDialog(CPractise4Dlg::IDD, pParent)
  53. {
  54.         //{{AFX_DATA_INIT(CPractise4Dlg)
  55.                 // NOTE: the ClassWizard will add member initialization here
  56.         //}}AFX_DATA_INIT
  57.         // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58.         m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }

  60. void CPractise4Dlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62.         CDialog::DoDataExchange(pDX);
  63.         //{{AFX_DATA_MAP(CPractise4Dlg)
  64.         DDX_Control(pDX, IDC_COMBO1, m_combo1);
  65.         //}}AFX_DATA_MAP
  66. }

  67. BEGIN_MESSAGE_MAP(CPractise4Dlg, CDialog)
  68.         //{{AFX_MSG_MAP(CPractise4Dlg)
  69.         ON_WM_SYSCOMMAND()
  70.         ON_WM_PAINT()
  71.         ON_WM_QUERYDRAGICON()
  72.         ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  73.         ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  74.         ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  75.         ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  76.         ON_BN_CLICKED(IDC_exit, Onexit)
  77.         //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()

  79. /////////////////////////////////////////////////////////////////////////////
  80. // CPractise4Dlg message handlers

  81. BOOL CPractise4Dlg::OnInitDialog()
  82. {
  83.         CDialog::OnInitDialog();

  84.         // Add "About..." menu item to system menu.

  85.         // IDM_ABOUTBOX must be in the system command range.
  86.         ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  87.         ASSERT(IDM_ABOUTBOX < 0xF000);

  88.         CMenu* pSysMenu = GetSystemMenu(FALSE);
  89.         if (pSysMenu != NULL)
  90.         {
  91.                 CString strAboutMenu;
  92.                 strAboutMenu.LoadString(IDS_ABOUTBOX);
  93.                 if (!strAboutMenu.IsEmpty())
  94.                 {
  95.                         pSysMenu->AppendMenu(MF_SEPARATOR);
  96.                         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  97.                 }
  98.         }

  99.         // Set the icon for this dialog.  The framework does this automatically
  100.         //  when the application's main window is not a dialog
  101.         SetIcon(m_hIcon, TRUE);                        // Set big icon
  102.         SetIcon(m_hIcon, FALSE);                // Set small icon
  103.        
  104.         // TODO: Add extra initialization here

  105.         //自己添加代码
  106.         m_combo1.SetCurSel(1);                //选择第二个串口
  107.         GetDlgItem(IDC_BUTTON3)->EnableWindow(m_openflag);
  108.         GetDlgItem(IDC_BUTTON4)->EnableWindow(m_openflag);

  109. //        port1.InitPort()
  110.        
  111.         return TRUE;  // return TRUE  unless you set the focus to a control
  112. }

  113. void CPractise4Dlg::OnSysCommand(UINT nID, LPARAM lParam)
  114. {
  115.         if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  116.         {
  117.                 CAboutDlg dlgAbout;
  118.                 dlgAbout.DoModal();
  119.         }
  120.         else
  121.         {
  122.                 CDialog::OnSysCommand(nID, lParam);
  123.         }
  124. }

  125. // If you add a minimize button to your dialog, you will need the code below
  126. //  to draw the icon.  For MFC applications using the document/view model,
  127. //  this is automatically done for you by the framework.

  128. void CPractise4Dlg::OnPaint()
  129. {
  130.         if (IsIconic())
  131.         {
  132.                 CPaintDC dc(this); // device context for painting

  133.                 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  134.                 // Center icon in client rectangle
  135.                 int cxIcon = GetSystemMetrics(SM_CXICON);
  136.                 int cyIcon = GetSystemMetrics(SM_CYICON);
  137.                 CRect rect;
  138.                 GetClientRect(&rect);
  139.                 int x = (rect.Width() - cxIcon + 1) / 2;
  140.                 int y = (rect.Height() - cyIcon + 1) / 2;

  141.                 // Draw the icon
  142.                 dc.DrawIcon(x, y, m_hIcon);
  143.         }
  144.         else
  145.         {
  146.                 CDialog::OnPaint();
  147.         }
  148. }

  149. // The system calls this to obtain the cursor to display while the user drags
  150. //  the minimized window.
  151. HCURSOR CPractise4Dlg::OnQueryDragIcon()
  152. {
  153.         return (HCURSOR) m_hIcon;
  154. }

  155. void CPractise4Dlg::OnButton1()               
  156. {

  157.         char buf[1]={'a'};
  158.                 if(!m_bSerialPortOpened) //检查串口是否打开
  159.         {
  160.                 AfxMessageBox("串口没有打开");
  161.                 return;
  162.         }
  163.         // TODO: Add your control notification handler code here
  164.         port1.WriteToPort(buf);
  165. }



  166. void CPractise4Dlg::OnButton2()
  167. {
  168.         // TODO: Add your control notification handler code here
  169.                 char buf1[1]={'b'};
  170.                 if(!m_bSerialPortOpened) //检查串口是否打开
  171.         {
  172.                 AfxMessageBox("串口没有打开");
  173.                 return;
  174.         }
  175.         // TODO: Add your control notification handler code here
  176.                 port1.WriteToPort(buf1);
  177. }

  178. void CPractise4Dlg::OnButton3()
  179. {
  180.         // TODO: Add your control notification handler code here
  181.         int nPort=m_combo1.GetCurSel()+1; //得到串口号,想想为什么要加1
  182.         if(port1.InitPort(this, nPort, 9600,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
  183.         {
  184.                 port1.StartMonitoring();
  185.                 m_bSerialPortOpened=TRUE;
  186.                 m_unPort=nPort;
  187.         }
  188.         else
  189.         {
  190.                 AfxMessageBox("没有发现此串口或被占用");
  191.                 m_bSerialPortOpened=FALSE;
  192.         }
  193.         GetDlgItem(IDC_BUTTON3)->EnableWindow(!m_bSerialPortOpened);
  194.         GetDlgItem(IDC_BUTTON4)->EnableWindow(m_bSerialPortOpened);
  195. }


  196. void CPractise4Dlg::OnButton4()
  197. {
  198.         // TODO: Add your control notification handler code here
  199.         port1.ClosePort();//关闭串口
  200.     m_bSerialPortOpened=FALSE;
  201.         GetDlgItem(IDC_BUTTON3)->EnableWindow(!m_bSerialPortOpened);
  202.         GetDlgItem(IDC_BUTTON4)->EnableWindow(m_bSerialPortOpened);
  203. }

  204. void CPractise4Dlg::Onexit()       
  205. {
  206.         // TODO: Add your control notification handler code here
  207.         CDialog::OnCancel();
  208. }
复制代码

出0入0汤圆

发表于 2014-6-6 13:44:33 | 显示全部楼层
不能先说问题再贴代码吗?

出0入0汤圆

 楼主| 发表于 2014-6-6 13:45:31 | 显示全部楼层
目前我想实现  上位机去控制我的LED灯,我按钮1点亮   按钮2熄灭
现在出现的问题是我打开串口后,然后点亮led是可以的,但是再点熄灭就不行了,
必须关闭串口以后再重新打开串口点按钮2才可以熄灭,请问是我的上位机的问题还是我下位机的问题,还是cserialport这个类的问题,请教各位了!

出0入0汤圆

 楼主| 发表于 2014-6-6 13:46:22 | 显示全部楼层
下位机我是用stm8ss208的单片机,

下位机我贴下我的代码

出0入0汤圆

 楼主| 发表于 2014-6-6 13:47:03 | 显示全部楼层
  1. #include <iostm8s208rb.h>
  2. #include "main.h"

  3. uint buf[10],m;
  4. _Bool flag_led1,flag_led2;

  5. #define adc_enable()                        ADC_CR1|=0x01
  6. #define adc_disable()                        ADC_CR1&=~0x01

  7. void  adc_init(void)       //adc初始化
  8. {
  9.     adc_disable();         
  10.     ADC_CSR=0x00;
  11.     ADC_CR2=0x18;
  12.     ADC_CR1=0x03;
  13.     asm("nop");        asm("nop");asm("nop");        asm("nop");        asm("nop");        asm("nop");                                         
  14.     adc_enable();       
  15. }

  16. unsigned long  adc_get(uchar mux)                //得到adc转换后的
  17. {
  18.     uchar  dataH, dataL;
  19.     uint  value = 0;   
  20.    // adc_init();
  21.     ADC_CSR|=(mux&0x0f);//选择通道               
  22.     while(!((ADC_CSR&0x80)==0x80));                //等待转换完成
  23.     ADC_CSR&=~0x80;                                //软件清零
  24.     asm("nop");        asm("nop");asm("nop");        asm("nop");        asm("nop");        asm("nop");       
  25.     dataL = ADC_DRL;            //读取寄存器数据
  26.     dataH = ADC_DRH;            //右对齐数据     
  27.     value = dataH*256+dataL;          
  28.     return  value;
  29. }



  30. ulong  Sort_detection(uchar mux)            //中值滤波
  31. {
  32.     uchar   i,j,k;
  33.     ulong   tmp,adc_buf[5];   
  34.     for(i=0;i<5;i++)
  35.     {
  36.         adc_buf[i] = adc_get(mux);
  37.         asm("nop");        asm("nop");asm("nop");        asm("nop");        asm("nop");        asm("nop");       
  38.     }
  39.     for(j=4;j>0;j--)            //冒泡排序
  40.     {
  41.         for(k=0;k<j;k++)
  42.         {
  43.             if(adc_buf[k]>adc_buf[k+1])
  44.             {
  45.                 tmp = adc_buf[k+1];
  46.                 adc_buf[k+1] = adc_buf[k];
  47.                 adc_buf[k] = tmp;
  48.             }
  49.         }
  50.     }
  51.     return  adc_buf[2];           //函数返回值为数组中间值
  52. }


  53. void Key_init()
  54. {
  55.     PF_DDR_DDR3=0;
  56.     PD_DDR_DDR0=0;
  57.     PD_DDR_DDR2=0;
  58.     PD_DDR_DDR3=0;
  59.    
  60.     PF_CR1_C13=1;
  61.     PD_CR1_C10=1;
  62.     PD_CR1_C12=1;
  63.     PD_CR1_C13=1;
  64. }

  65. void Led_init()
  66. {
  67.     PC_DDR_DDR1=1;
  68.     PF_DDR_DDR6=1;
  69.     PF_DDR_DDR5=1;
  70.     PF_DDR_DDR4=1;
  71.    
  72.     PC_CR1_C11=1;
  73.     PF_CR1_C16=1;
  74.     PF_CR1_C15=1;
  75.     PF_CR1_C14=1;
  76. }

  77. void Uart_init()
  78. {
  79.     UART1_BRR2 = 0x00; //9600bps @2M
  80.     UART1_BRR1 = 0x0D; //9600bps @2M
  81.     UART1_CR3 = 0x00;  //1位停止位,不开启同步模式
  82.     UART1_CR2 = 0x2C;  // 使能发送和接收功能,并开启接收中断
  83.     UART1_CR1 = 0x00;  //8位数据,不使用奇偶效验,并且使能UART1
  84. }

  85. void send_char(char buf)
  86. {
  87.     UART1_DR = buf;//发送TxBuffer1数组的字符
  88.     while((UART1_SR & 0x80) == 0x00);//查询发送缓冲区的字节是否已经发送出去
  89. }
  90. uchar Get_char()
  91. {
  92.     uchar b;
  93.     b=UART1_DR;
  94.     while((UART1_SR & 0x80) == 0x00);//查询发送缓冲区的字节是否已经发送出去
  95.     return b;
  96. }

  97. void send_string(char *buf)
  98. {
  99.     while(*buf)
  100.     {
  101.         send_char(*(buf++));
  102.     }
  103. }

  104. void Timer4_init()
  105. {
  106.     TIM4_ARR=255;//计数到255后产生溢出
  107.     TIM4_PSCR=0x07;//128分频,即2M/128=15.625KHz(相当于计数器周期为64uS)
  108.     TIM4_IER=0X01;//禁止中断
  109.     TIM4_EGR=0x01;//允许产生更新事件
  110.     TIM4_CR1=0X01;//启动定时器
  111. }

  112. void Device_inti()
  113. {
  114.     Key_init();  
  115.     adc_init();
  116.     Led_init();
  117.     Uart_init();
  118.     Timer4_init();
  119.     LED1=1;
  120.     LED2=1;
  121.     LED3=1;
  122.     LED4=1;
  123.     asm("rim");  // 开全局中断
  124. }

  125. void Ad_handle()
  126. {
  127.       if(m>=50)
  128.       {
  129.           m=0;
  130.           send_char(buf[0]%10000/1000+48);
  131.           send_char(buf[0]%1000/100+48);
  132.           send_char(buf[0]%100/10+48);
  133.           send_char(buf[0]%10/1+48);
  134.           send_char(0x0a);
  135.       }  
  136. }


  137. void main()
  138. {
  139.   Device_inti();
  140.   
  141.     while(1)
  142.     {
  143.             
  144.       
  145.        buf[0]=adc_get(9)*3.22;   
  146.        // Ad_handle();     
  147.        if(KEY1==0)
  148.        {
  149.            if(m>=10)
  150.            {
  151.               m=0;
  152.               send_string("hello STM8");
  153.            }
  154.             while(KEY1==0);
  155.        }
  156.       
  157.     }
  158. }

  159. #pragma vector=20
  160. __interrupt void Uart1_IRQHandler(void)
  161. {
  162.     uchar  uartbuf[10];
  163.     uartbuf[0]=Get_char();
  164.     send_string("iget it \n");
  165.    
  166.     switch(uartbuf[0])
  167.     {
  168.           case 'a':
  169.            //  flag_led1=1;
  170.              LED1=0;
  171.             break;
  172.             
  173.           case 'b':
  174.         //    flag_led2=1;
  175.             LED1=1;
  176.             break;
  177.           default :break;
  178.     }  
  179.    
  180.    
  181.     return;
  182. }

  183. #pragma vector=25
  184. __interrupt void Timer1_IRQHandler(void)
  185. {
  186.   
  187.   TIM4_SR = 0x00; // 清除更新标志
  188.   m++;
  189.   return;
  190. }

复制代码

出0入0汤圆

发表于 2014-6-6 13:50:44 | 显示全部楼层
没有人能帮你编程的,别人只能帮你理一下思路,根本不会有人去看你的那么长的代码!

出130入129汤圆

发表于 2014-6-6 14:06:25 | 显示全部楼层
发送的2个按钮函数,将 char类型改为 static char看看,
因为是异步传输,很可能WriteToPort的时候,数组生命周期已经结束。

出0入0汤圆

 楼主| 发表于 2014-6-6 15:30:25 | 显示全部楼层
wangpengcheng 发表于 2014-6-6 13:50
没有人能帮你编程的,别人只能帮你理一下思路,根本不会有人去看你的那么长的代码! ...

这话说的,我只是不知道问题在哪?

这代码是我昨天就敲完,今天验证的时候出现这个问题了!

出0入0汤圆

发表于 2014-6-6 15:45:47 | 显示全部楼层
dhw5qq 发表于 2014-6-6 15:30
这话说的,我只是不知道问题在哪?

这代码是我昨天就敲完,今天验证的时候出现这个问题了! ...

单步调试,设断点,printf等好多种调试方法,先把问题点确认!

出0入0汤圆

 楼主| 发表于 2014-6-6 20:05:19 | 显示全部楼层
68336016 发表于 2014-6-6 14:06
发送的2个按钮函数,将 char类型改为 static char看看,
因为是异步传输,很可能WriteToPort的时候,数组生 ...

  查了vc的书,好像一个控件在处理消息以后需要返回,可能是没有返回,导致程序无法响应按钮2

出130入129汤圆

发表于 2014-6-6 20:18:53 | 显示全部楼层
dhw5qq 发表于 2014-6-6 20:05
查了vc的书,好像一个控件在处理消息以后需要返回,可能是没有返回,导致程序无法响应按钮2 ...

别管书上怎么说,有没有按我说的试试,只需要敲 12个字母而已。

出0入0汤圆

 楼主| 发表于 2014-6-7 20:37:13 | 显示全部楼层
试过了,还是不行,我问了我们老师,他说你需要清空缓冲区数据!

出0入0汤圆

发表于 2014-6-8 01:14:24 | 显示全部楼层
在上位机程序中设置一个数组变量,如char m_cSendbuf[1], 然后每次发送都是先 m_cSendbuf[0] = 'a'或'b',然后WriteToPort(m_cSendbuf,1),试试看。还不行就把按钮消息中只设置m_cSendbuf,然后设一个BOOL值作为标志,将WriteToPort的执行放到一个定时器中,多执行WriteToPort几次,不信还收不到!

出130入129汤圆

发表于 2014-6-8 02:31:01 | 显示全部楼层
这个类我使用很久了,特地又试一次,毫无问题。

出0入0汤圆

 楼主| 发表于 2014-6-9 13:30:09 | 显示全部楼层
68336016 发表于 2014-6-8 02:31
这个类我使用很久了,特地又试一次,毫无问题。

我知道跟这个串口类没有关系,可能我的思路出问题了,我再试试吧!按照楼上的
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-29 22:16

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表