CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  硬件/嵌入开发 >  硬件设计

怎样在WINDOWS下用C语言编写串口接收数据程序?

楼主sunly15(热血与灰心)2003-12-01 10:46:00 在 硬件/嵌入开发 / 硬件设计 提问

我系在校学生,最近学习一些单片机的知识!  
  请问:  
  怎样在WINDOWS下用C语言编写串口接收数据程序?  
  又怎样去测试程序是否正确? 问题点数:80、回复次数:12Top

1 楼azmao(azmao)回复于 2003-12-01 13:49:25 得分 0

调用WindowsAPI的一些函数ReadFile、WriteFile等等。  
  验证:1、将pc串口的2-3脚短接,实现PC的自发自收,验证pc机程序的正确性!  
              2、将单片机的串口经电平转换(adm202)接PC的串口。试验通讯。Top

2 楼lastest(弯弓射大雕)回复于 2003-12-01 14:44:33 得分 0

可行Top

3 楼selevall(selevall)回复于 2003-12-04 14:29:35 得分 10

dos下的tc   中有个函数bioscom(),可以用c做;  
  我想windows中可试一下。Top

4 楼happyandy(安迪)回复于 2003-12-04 20:02:38 得分 0

可以用函数ReadFile、WriteFile  
  还可以用inputb和outputb  
  方法是很多的  
  你可以将串口的2-3脚短接,实现PC的自发自收,然后判断正确性!Top

5 楼sunly15(热血与灰心)回复于 2003-12-10 15:37:46 得分 0

我也想到用BIOSCOM,你们能提供实例吗?Top

6 楼jpyc(九品-沉默)回复于 2003-12-11 07:55:09 得分 0

用DELPHI最简单,有例程,用   的是最专业的通讯控件tubropower,需要发消息。Top

7 楼yangyilove1999(CSP)回复于 2003-12-18 22:03:16 得分 0

用com1     c的话     inport()   函数Top

8 楼erx(erx)回复于 2003-12-20 20:01:01 得分 70

portr.c     读串口  
   
  /*---------------------------------------------------------------------------  
    *    
    *   Synopsis  
    *   --------  
    *   Reads   characters   from   an   IO   port.  
    *  
    *   Usage  
    *   -----  
    *   ./portr.exe   <IO   port>   <count>  
    *  
    *   Author/Copyright  
    *   ----------------  
    *   Raymond   McKendall  
    *   2001  
    *  
    *--------------------------------------------------------------------------*/  
   
  #include   <windows.h>  
  #include   <stdio.h>  
  #include   <stdlib.h>  
   
  #define   MAX   1024  
   
  int   main(int   argc,   char**   argv)   {  
   
      char   *me   =   "portr";       /*   Who   I   am.                                                 */  
      char   *port;                       /*   IO   port   to   use:   COM1,   LPT1,   etc.   */  
      char   msg[MAX+1];             /*   Message   to   write   to   port.                 */  
      int   n_msg;                         /*   Counts   bytes   to   read.                         */  
      int   n_in;                           /*   Counts   bytes   read.                               */  
      int   ok;                               /*   Return   code   from   function   calls.   */  
      HANDLE   hPort;                   /*   Handle   to   IO   port   object.                 */  
   
      /*---   Get   port   ---*/  
      argc   --;  
      if   (argc   ==   2)   {  
          port     =   *++argv;  
          n_msg   =   atoi(*++argv);  
      }  
      else   {  
          printf   ("%s:   Usage:   %s   <IO   port>   <count>\n",   me,   me);  
          return   1;  
      }  
       
      /*---   Open   IO   port.   ---*/  
      hPort   =   CreateFile(port,   GENERIC_READ,   0,   NULL,   OPEN_EXISTING,   0,   NULL);  
      if   (hPort   ==   INVALID_HANDLE_VALUE)   {  
          printf("%s:   Cannot   open   IO   port   %s   (error   %d)\n",   me,   port,   GetLastError());  
          return   1;  
      }  
   
      /*---   Read   message   from   port.   ---*/  
      n_msg   =   (n_msg   <=   MAX   ?   n_msg   :   MAX);  
      ok   =   ReadFile(hPort,   msg,   n_msg,   &n_in,   NULL);  
      if   (!ok)   {  
          printf("%s:   Cannot   read   message   (error   %d)\n",   me,   GetLastError());  
          return   1;  
      }  
      msg[n_msg]   =   '\0';  
      printf   ("%s:   %s   (%d   of   %d   bytes)\n",   me,   msg,   n_msg,   n_in);  
   
      /*---   Cleanup.   ---*/  
      ok   =   CloseHandle(hPort);  
      if   (!ok)   {  
          printf("%s:   Cannot   close   port   handle   (error   %d)\n",   me,   GetLastError());  
      }  
       
      return   0;  
  }  
   
   
   
  portw.c   写串口  
  /*---------------------------------------------------------------------------  
    *    
    *   Synopsis  
    *   --------  
    *   Writes   message   to   an   IO   port.  
    *  
    *   Usage  
    *   -----  
    *   ./portw.exe   <IO   port>   <message>  
    *  
    *   Author/Copyright  
    *   ----------------  
    *   Raymond   McKendall  
    *   2001  
    *  
    *--------------------------------------------------------------------------*/  
   
  #include   <windows.h>  
  #include   <stdio.h>  
  #include   <string.h>  
   
  int   main   (int   argc,   char**   argv)   {  
   
      char   *me   =   "portw";       /*   Who   I   am.                                                 */  
      char   *port;                       /*   IO   port   to   use:   COM1,   LPT1,   etc.   */  
      char   *msg;                         /*   Message   to   write   to   port.                 */  
      int   n_msg;                         /*   Counts   bytes   in   message.                   */  
      int   n_out;                         /*   Counts   bytes   written.                         */  
      int   ok;                               /*   Return   code   from   function   calls.   */  
      HANDLE   hPort;                   /*   Handle   to   IO   port   object.                 */  
       
      /*---   Get   port   ---*/  
      argc   --;  
      if   (argc   ==   2)   {  
          port   =   *++argv;  
          msg     =   *++argv;  
      }  
      else   {  
          printf   ("%s:   Usage:   %s   <IO   port>   <message>\n",   me,   me);  
          return   1;  
      }  
       
      /*---   Open   IO   port.   ---*/  
      hPort   =   CreateFile(port,   GENERIC_WRITE,   0,   NULL,   OPEN_EXISTING,   0,   NULL);  
      if   (hPort   ==   INVALID_HANDLE_VALUE)   {  
          printf("%s:   Cannot   open   IO   port   %s   (error   %d)\n",   me,   port,   GetLastError());  
          return   1;  
      }  
   
      /*---   Write   message   to   port.   ---*/  
      n_msg   =   strlen(msg)+1;  
      ok   =   WriteFile(hPort,   msg,   n_msg,   &n_out,   NULL);  
      if   (!ok)   {  
          printf("%s:   Cannot   write   message   (error   %d)\n",   me,   GetLastError());  
          return   1;  
      }  
      printf   ("%s:   %s   (%d   of   %d   bytes)\n",   me,   msg,   n_msg,   n_out);  
   
      /*---   Cleanup.   ---*/  
      ok   =   CloseHandle(hPort);  
      if   (!ok)   {  
          printf("%s:   Cannot   close   port   handle   (error   %d)\n",   me,   GetLastError());  
      }  
       
      return   0;  
  }Top

9 楼erx(erx)回复于 2003-12-20 20:01:51 得分 0

真不知你该怎么谢我Top

10 楼Elpout(我欲开心颜!)回复于 2003-12-22 20:44:32 得分 0

outputb/inputb在9x下还行,NT内核系统行不通。Top

11 楼devzhao(wincer)回复于 2003-12-25 17:47:36 得分 0

1、windows   api:  
        createfile()  
        readfile()  
        writefile()  
        getcommstatus()  
        setcommstatus()  
  2、microsoft提供了控件MSCOMM,可很方便地在VC,VB   中使用Top

12 楼sunly15(热血与灰心)回复于 2004-01-06 11:56:14 得分 0

"windows.h"在哪?Top

相关问题

  • 用纯C语言编写串口通讯的程序
  • C中 串口程序中波特率程序怎么编写
  • 如何用C语言通过串口发送和接收float数组
  • 请教:vc下编写的串口程序是否可以用硬中断的方式接收数据?
  • 请问如何用Keil C编写串口与89S52通信的程序啊?
  • 在线急求:C#编写的发邮件程序为什么接收不到?
  • msdos6.22平臺中用Turbo C 寫串口通訊程式,發送字符串不能完整接收的問題.
  • 是用C#编写串口程序,还是使用VC++写成的Dll供C#使用?头疼???????
  • 是用C#编写串口程序,还是使用VC++写成的Dll供C#使用?头疼???????
  • C#串口通信

关键词

  • 函数
  • me
  • pc
  • 串口
  • hport
  • port
  • portr
  • msg
  • 程序
  • error%d

得分解答快速导航

  • 帖主:sunly15
  • selevall
  • erx

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo