zoukankan      html  css  js  c++  java
  • Win32 API UART编程

        下面是一个使用Win32 API进行UART编程的简单示例。

     1 #include <windows.h>
     2 #include <stdio.h>
     3 
     4 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
     5 {
     6     HANDLE hCom;
     7     DWORD dwError;
     8     char buf[1024] = "Hello World From COM3!
    ";
     9     DWORD nBytes;
    10     
    11     printf("%s
    ", lpCmdLine);
    12     
    13     hCom = CreateFile("COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    14     
    15     if (hCom == (HANDLE)-1) {
    16         dwError = GetLastError();
    17         printf("Error = %d
    ", dwError);
    18         return 0;
    19     }
    20     
    21     DCB dcb;
    22     GetCommState(hCom, &dcb);
    23     dcb.BaudRate = 9600;
    24     dcb.ByteSize = 8;
    25     dcb.Parity = NOPARITY;
    26     dcb.StopBits = 1;
    27     dcb.fBinary = TRUE;
    28     dcb.fParity = TRUE;
    29     SetCommState(hCom, &dcb);
    30     SetupComm(hCom, 1024, 1024);
    31     PurgeComm(hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
    32     
    33     COMMTIMEOUTS to;
    34     memset(&to, 0, sizeof(to));
    35     to.ReadIntervalTimeout = 10;
    36     SetCommTimeouts(hCom, &to);
    37 
    38     WriteFile(hCom, buf, sizeof(buf), &nBytes, NULL);
    39     printf("Data Sent! NBytes = %d
    ", nBytes);
    40     ReadFile(hCom, buf, sizeof(buf), &nBytes, NULL);
    41     printf("Data Received! NBytes = %d
    ", nBytes);
    42     
    43     printf("%s
    ", buf);
    44     
    45     CloseHandle(hCom);
    46     return 0;
    47 }
  • 相关阅读:
    前端设计网站收藏
    JAVA的StringBuffer类
    JDBC连接数据库
    JSP中request属性的用法
    jquery学习
    servlet学习(一)
    javascript 计算器
    xml学习(一)
    网站之单点登录简单思路
    关于ASP.NET中Menu控件在浏览器中不正常显示解决方法
  • 原文地址:https://www.cnblogs.com/lyuyangly/p/5746515.html
Copyright © 2011-2022 走看看