本文介绍了pc和Arduino通过USB串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 C++ 代码,需要用于 PC 和 Arduino 之间的串行通信.文件Serial.cpp"包含一个名为stdafx.h"的文件,该文件不存在于项目中或我计算机上的任何位置,这显然会导致错误.除此之外,我还收到其他错误,例如 C2065 'CSerial': undeclared identifier.

I have some C++ code that I need to use for serial communication between PC and Arduino. The file "Serial.cpp" is including a file called "stdafx.h" that doesn't exist in the project or anywhere on my computer, which obviously causes an error.Other than that, I also get other errors, such as C2065 'CSerial': undeclared identifier.

这是项目中的三个文件:

Here are the three files that are in the project:

Serial.h

#ifndef __SERIAL_H__
#define __SERIAL_H__
#pragma once

#include <Windows.h>
#include <memory.h>

#define FC_DTRDSR       0x01
#define FC_RTSCTS       0x02
#define FC_XONXOFF      0x04
#define ASCII_BEL       0x07
#define ASCII_BS        0x08
#define ASCII_LF        0x0A
#define ASCII_CR        0x0D
#define ASCII_XON       0x11
#define ASCII_XOFF      0x13

class CSerial
{

public:
    CSerial();
    ~CSerial();

    BOOL Open( int nPort = 2, int nBaud = 9600 );
    BOOL Close( void );

    int ReadData( void *, int );
    int SendData( const char *, int );
    int ReadDataWaiting( void );

    BOOL IsOpened( void ){ return( m_bOpened ); }

protected:
    BOOL WriteCommByte( unsigned char );

    HANDLE m_hIDComDev;
    OVERLAPPED m_OverlappedRead, m_OverlappedWrite;
    bool m_bOpened;

};

#endif

Serial.cpp

// Serial.cpp

#include "stdafx.h"
#include "Serial.h"


CSerial::CSerial()
{

    memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
    memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
    m_hIDComDev = NULL;
    m_bOpened = false;

}

CSerial::~CSerial()
{

    Close();

}

BOOL CSerial::Open( int nPort, int nBaud )
{

    if( m_bOpened ) return( TRUE );

    wchar_t szPort[15];
    wchar_t szComParams[50];
    DCB dcb;

    wsprintf( szPort, L"COM%d", nPort );
    m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
    if( m_hIDComDev == NULL ) return( FALSE );

    memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
    memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

    COMMTIMEOUTS CommTimeOuts;
    CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
    CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
    CommTimeOuts.ReadTotalTimeoutConstant = 0;
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
    CommTimeOuts.WriteTotalTimeoutConstant = 5000;
    SetCommTimeouts( m_hIDComDev, &CommTimeOuts );

    wsprintf( szComParams, L"COM%d:%d,n,8,1", nPort, nBaud );

    m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
    m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

    dcb.DCBlength = sizeof( DCB );
    GetCommState( m_hIDComDev, &dcb );
    dcb.BaudRate = nBaud;
    dcb.ByteSize = 8;
    unsigned char ucSet;
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
    ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
    if( !SetCommState( m_hIDComDev, &dcb ) ||
        !SetupComm( m_hIDComDev, 10000, 10000 ) ||
        m_OverlappedRead.hEvent == NULL ||
        m_OverlappedWrite.hEvent == NULL ){
        DWORD dwError = GetLastError();
        if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
        if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
        CloseHandle( m_hIDComDev );
        return( FALSE );
        }

    m_bOpened = TRUE;

    return( m_bOpened );

}

BOOL CSerial::Close( void )
{

    if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE );

    if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
    if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
    CloseHandle( m_hIDComDev );
    m_bOpened = FALSE;
    m_hIDComDev = NULL;

    return( TRUE );

}

BOOL CSerial::WriteCommByte( unsigned char ucByte )
{
    BOOL bWriteStat;
    DWORD dwBytesWritten;

    bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten, &m_OverlappedWrite );
    if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
        if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) ) dwBytesWritten = 0;
        else{
            GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite, &dwBytesWritten, FALSE );
            m_OverlappedWrite.Offset += dwBytesWritten;
            }
        }

    return( TRUE );

}

int CSerial::SendData( const char *buffer, int size )
{

    if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

    DWORD dwBytesWritten = 0;
    int i;
    for( i=0; i<size; i++ ){
        WriteCommByte( buffer[i] );
        dwBytesWritten++;
        }

    return( (int) dwBytesWritten );

}

int CSerial::ReadDataWaiting( void )
{

    if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

    DWORD dwErrorFlags;
    COMSTAT ComStat;

    ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );

    return( (int) ComStat.cbInQue );

}

int CSerial::ReadData( void *buffer, int limit )
{

    if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

    BOOL bReadStatus;
    DWORD dwBytesRead, dwErrorFlags;
    COMSTAT ComStat;

    ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
    if( !ComStat.cbInQue ) return( 0 );

    dwBytesRead = (DWORD) ComStat.cbInQue;
    if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;

    bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );
    if( !bReadStatus ){
        if( GetLastError() == ERROR_IO_PENDING ){
            WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
            return( (int) dwBytesRead );
            }
        return( 0 );
        }

    return( (int) dwBytesRead );

}


SerialExample.cpp

bool sendExample(int port, int baudRate)
{
    char data[4];

    CSerial* s = new CSerial();

    if(!s->Open(port, baudRate))
    {
        std_out << _T("Could not open COM") << port << endl;
        return false;
    }


    // Sending a string of 4 characters
    data[0] = 0x31;
    data[1] = 0x32;
    data[2] = 0x33;
    data[3] = 0x0D; // ASCII CR
    s->SendData(data, 4);

    s->Close();

    delete s;

}

有人知道我必须做什么吗?

Does anyone know what I have to do?

推荐答案

stdafx.h 是 Visual Studio 的预编译头文件.因此,如果您不在 Visual Studio 中工作,则可以将其删除,它应该可以正常工作.

stdafx.h is a precompiled header for Visual studio. so if you are not working in visual studio, you can just remove it and it should work just fine.

关于编译器无法识别 CSerial 类 -我没有看到您在 SerialExample.cpp 中包含CSerial.h"的位置(即:#includeCSerial.h"),但是如果您这样做了,这也可能是 CSerial.cpp 未编译的症状(虽然不太可能))...

In regard to the compiler not recognizing the CSerial class -I don't see where you include "CSerial.h" in SerialExample.cpp (i.e: #include "CSerial.h"), but if you do, this may also be a symptom of CSerial.cpp not compiling (less likely though)...

希望这有帮助,利奥

这篇关于pc和Arduino通过USB串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 15:48