繁体   English   中英

无法在C ++中的串行端口上进行读写

[英]Unable to read write on serial port in C++

我正在Visual Studio 2010上用c ++编写串行通信代码。为此,我尝试使用本示例, 示例运行良好。 现在,我尝试通过短接硬件的Rx和Tx引脚来发送字符串并读回。 这是我发送字符串的代码:

char *buffer ="hello";
   DWORD bytesSend;
   DWORD errors;
   COMSTAT status;
   DWORD numWritten;
   WriteFile(hCom, buffer, strlen(buffer), &numWritten, NULL); 
   printf("send succesfully\n");

这是我的接收代码:

DWORD numRead;

  BOOL ret = ReadFile(hCom, buffer, 5, &numRead, NULL);

    if(!ret)
    {
        printf("Read Fail\n");
    }
    else
    {
        printf("%s\n",buffer);
    }

在运行我的代码时,接收失败。 所以,请告诉我问题出在哪里。 这是我完整的代码。

#include "stdafx.h"

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

void PrintCommState(DCB dcb)
{
    //  Print some of the DCB structure values
    _tprintf( TEXT("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n"), 
              dcb.BaudRate, 
              dcb.ByteSize, 
              dcb.Parity,
              dcb.StopBits );
}


int _tmain( )
{
   DCB dcb;
   HANDLE hCom;
   BOOL fSuccess;
   TCHAR *pcCommPort = TEXT("COM1"); //  Most systems have a COM1 port

   //  Open a handle to the specified com port.
   hCom = CreateFile( pcCommPort,
                      GENERIC_READ | GENERIC_WRITE,
                      0,      //  must be opened with exclusive-access
                      NULL,   //  default security attributes
                      OPEN_EXISTING, //  must use OPEN_EXISTING
                      0,      //  not overlapped I/O
                      NULL ); //  hTemplate must be NULL for comm devices

   if (hCom == INVALID_HANDLE_VALUE) 
   {
       //  Handle the error.
       printf ("CreateFile failed with error %d.\n", GetLastError());
       return (1);
   }

   //  Initialize the DCB structure.
   SecureZeroMemory(&dcb, sizeof(DCB));
   dcb.DCBlength = sizeof(DCB);

   //  Build on the current configuration by first retrieving all current
   //  settings.
   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      //  Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }

   PrintCommState(dcb);       //  Output to console

   //  Fill in some DCB values and set the com state: 
   //  57,600 bps, 8 data bits, no parity, and 1 stop bit.
   dcb.BaudRate = CBR_57600;     //  baud rate
   dcb.ByteSize = 8;             //  data size, xmit and rcv
   dcb.Parity   = NOPARITY;      //  parity bit
   dcb.StopBits = ONESTOPBIT;    //  stop bit

   fSuccess = SetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      //  Handle the error.
      printf ("SetCommState failed with error %d.\n", GetLastError());
      return (3);
   }

   //  Get the comm config again.
   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      //  Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }

   PrintCommState(dcb);       //  Output to console

   _tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);

   char *buffer ="hello";
   DWORD bytesSend;
   DWORD errors;
   COMSTAT status;
   DWORD numWritten;
    WriteFile(hCom, buffer, strlen(buffer), &numWritten, NULL); 
    printf("send succesfully\n");

  DWORD numRead;
  BOOL ret = ReadFile(hCom, buffer, 5, &numRead, NULL);

    if(!ret)
    {
        printf("Read Fail\n");
    }
    else
    {
        printf("%s",buffer);
    }



   return (0);
}

这是我的输出:- 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM