我是C++初学者,最近在研究C++串口通信,哪位大侠有单片机发送数据,在上位机上画出波形的代码啊~~~~~
请发送到[email protected]   
或者给个思路~~
不胜感激 

解决方案 »

  1.   

    #pragma once#ifndef __SERIALPORT_H__
    #define __SERIALPORT_H__
    enum Parity
    {    
    EvenParity,
    MarkParity,
    NoParity,
    OddParity,
    SpaceParity
    };enum StopBits
    {
    OneStopBit,
    OnePointFiveStopBits,
    TwoStopBits
    };
    class CSerialPort
    {
    protected:
    HANDLE m_hComm;       //Handle to the comms port
    HANDLE m_hEvent;      //A event handle we need for internal synchronisation
        BOOL   m_bOverlapped; //Is the port open in overlapped IO
    public:
    enum FlowControl
    {
    NoFlowControl,
    CtsRtsFlowControl,
    CtsDtrFlowControl,
    DsrRtsFlowControl,
    DsrDtrFlowControl,
    XonXoffFlowControl
    }; CSerialPort(void);
    ~CSerialPort(void);    BOOL IsOpen() const { return m_hComm != INVALID_HANDLE_VALUE; };
    void Open(LPCTSTR szPort, DWORD dwBaud = 9600, Parity parity = NoParity, BYTE DataBits = 8, StopBits stopbits = OneStopBit, FlowControl fc = NoFlowControl, BOOL bOverlapped = FALSE);
    void Close(); void GetState(DCB& dcb);
    void SetState(DCB& dcb);
        void SetTimeouts(DWORD readTimeoutConstant = 0, DWORD readTimeoutMultiplier = 0, DWORD writeTimeoutConstant = 0, DWORD writeTimeoutMultiplier = 0);
        void Setup(DWORD dwInQueue = 1024, DWORD dwOutQueue = 1024);
        void Purge(DWORD dwFlags);    void ClearError(DWORD dwErrors);
    void ClearWriteBuffer();
    void ClearReadBuffer();
    DWORD Read(void* lpBuf, DWORD dwCount);
    BOOL  Read(void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesRead=NULL);
    DWORD Write(const void* lpBuf, DWORD dwCount);
    BOOL  Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesWritten=NULL);
    };
    #endif
      

  2.   

    #include "StdAfx.h"
    #include ".\serialport.h"CSerialPort::CSerialPort(void)
    {
    m_hComm = INVALID_HANDLE_VALUE;
    m_hEvent = NULL;
        m_bOverlapped = FALSE;
    }CSerialPort::~CSerialPort(void)
    {
    Close();
    }void CSerialPort::Open(LPCTSTR szPort, DWORD dwBaud, Parity parity, BYTE DataBits, StopBits stopbits, FlowControl fc, BOOL bOverlapped)
    {
    CString m_szPort(szPort);
    CString m_szMsg;
    int m_iPortID; m_szPort.MakeUpper();
    m_szPort.Trim();
    if (m_szPort.GetLength() <=3)
    {
    m_szMsg.Format(_T("串口[%s]设置错误!"),m_szPort);
    MessageBox(NULL,m_szMsg,_T("通信错误"),MB_ICONINFORMATION | MB_OK);
    return ;
    }
    m_szPort = m_szPort.Mid(3);
    m_iPortID = (int)atoi((LPCTSTR)m_szPort);
    if ((m_iPortID <1) || (m_iPortID)>255)
    {
    m_szMsg.Format(_T("你打开的串口[%s],不正确!"),szPort);
    ::MessageBox(NULL,m_szMsg,_T("通信错误"),MB_ICONEXCLAMATION | MB_OK);
    return ;
    } Close(); //In case we are already open //Call CreateFile to open up the comms port
    CString sPort;
    sPort.Format(_T("\\\\.\\%s"), szPort);
    m_hComm = CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, bOverlapped ? FILE_FLAG_OVERLAPPED : 0, NULL);
    if (m_hComm == INVALID_HANDLE_VALUE)
    {
    sPort.Format("打开%s口失败", szPort);
    MessageBox(NULL, sPort, _T("端口错误"), MB_OK);
    } //Create the event we need for later synchronisation use
    m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (m_hEvent == NULL)
    {
    Close();
    TRACE(_T("Failed in call to CreateEvent in Open\n"));
    }
      
    m_bOverlapped = bOverlapped; //Get the current state prior to changing it
    DCB dcb;
    dcb.DCBlength = sizeof(DCB);
    GetState(dcb); //Setup the baud rate
    dcb.BaudRate = dwBaud;  //Setup the Parity
    switch (parity)
    {
    case EvenParity:  dcb.Parity = EVENPARITY;  break;
    case MarkParity:  dcb.Parity = MARKPARITY;  break;
    case NoParity:    dcb.Parity = NOPARITY;    break;
    case OddParity:   dcb.Parity = ODDPARITY;   break;
    case SpaceParity: dcb.Parity = SPACEPARITY; break;
    default:          ASSERT(FALSE);            break;
    } //Setup the data bits
    dcb.ByteSize = DataBits; //Setup the stop bits
    switch (stopbits)
    {
    case OneStopBit:           dcb.StopBits = ONESTOPBIT;   break;
    case OnePointFiveStopBits: dcb.StopBits = ONE5STOPBITS; break;
    case TwoStopBits:          dcb.StopBits = TWOSTOPBITS;  break;
    default:                   ASSERT(FALSE);               break;
    } //Setup the flow control 
    dcb.fDsrSensitivity = FALSE;
    switch (fc)
    {
    case NoFlowControl:
    {
    dcb.fOutxCtsFlow = FALSE;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fOutX = FALSE;
    dcb.fInX = FALSE;
    break;
    }
    case CtsRtsFlowControl:
    {
    dcb.fOutxCtsFlow = TRUE;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
    dcb.fOutX = FALSE;
    dcb.fInX = FALSE;
    break;
    }
    case CtsDtrFlowControl:
    {
    dcb.fOutxCtsFlow = TRUE;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
    dcb.fOutX = FALSE;
    dcb.fInX = FALSE;
    break;
    }
    case DsrRtsFlowControl:
    {
    dcb.fOutxCtsFlow = FALSE;
    dcb.fOutxDsrFlow = TRUE;
    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
    dcb.fOutX = FALSE;
    dcb.fInX = FALSE;
    break;
    }
    case DsrDtrFlowControl:
    {
    dcb.fOutxCtsFlow = FALSE;
    dcb.fOutxDsrFlow = TRUE;
    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
    dcb.fOutX = FALSE;
    dcb.fInX = FALSE;
    break;
    }
    case XonXoffFlowControl:
    {
    dcb.fOutxCtsFlow = FALSE;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fOutX = TRUE;
    dcb.fInX = TRUE;
    dcb.XonChar = 0x11;
    dcb.XoffChar = 0x13;
    dcb.XoffLim = 100;
    dcb.XonLim = 100;
    break;
    }
    default:
    {
    ASSERT(FALSE);
    break;
    }
    }
      
    //Now that we have all the settings in place, make the changes
    SetState(dcb);
    }void CSerialPort::Close()
    {
    if (IsOpen())
    {
    //Close down the comms port
    BOOL bSuccess = CloseHandle(m_hComm);
    m_hComm = INVALID_HANDLE_VALUE;
    if (!bSuccess)
    TRACE(_T("Failed to close up the comms port, GetLastError:%d\n"), GetLastError());
    m_bOverlapped = FALSE; //Free up the event object we are using
    CloseHandle(m_hEvent);
    m_hEvent = NULL;
    }
    }void CSerialPort::GetState(DCB& dcb)
    {
    ASSERT(IsOpen()); if (!GetCommState(m_hComm, &dcb))
    TRACE(_T("Failed in call to GetCommState\n"));
    }void CSerialPort::SetState(DCB& dcb)
    {
    ASSERT(IsOpen()); if (!SetCommState(m_hComm, &dcb))
    TRACE(_T("Failed in call to SetCommState\n"));
    }void CSerialPort::SetTimeouts(DWORD readTimeoutConstant, DWORD readTimeoutMultiplier, DWORD writeTimeoutConstant, DWORD writeTimeoutMultiplier)
    {
    ASSERT(IsOpen());
    COMMTIMEOUTS timeouts;
    timeouts.ReadIntervalTimeout = MAXDWORD;
    timeouts.ReadTotalTimeoutConstant = readTimeoutConstant;
    timeouts.ReadTotalTimeoutMultiplier = readTimeoutMultiplier;
    timeouts.WriteTotalTimeoutConstant = writeTimeoutConstant;
    timeouts.WriteTotalTimeoutMultiplier = writeTimeoutMultiplier; if (!SetCommTimeouts(m_hComm, &timeouts))
    TRACE(_T("Failed in call to SetCommTimeouts\n"));
    }void CSerialPort::Setup(DWORD dwInQueue, DWORD dwOutQueue)
    {
    ASSERT(IsOpen()); if (!SetupComm(m_hComm, dwInQueue, dwOutQueue))
    TRACE(_T("Failed in call to SetupComm\n"));
    }void CSerialPort::Purge(DWORD dwFlags)
    {
    ASSERT(IsOpen()); if (!PurgeComm(m_hComm, dwFlags))
    TRACE(_T("Failed in call to PurgeComm\n"));
    }void CSerialPort::ClearError(DWORD dwErrors)
    {
    ASSERT(IsOpen()); if (!ClearCommError(m_hComm, &dwErrors, NULL))
    TRACE(_T("Failed in call to ClearCommError\n"));
    }void CSerialPort::ClearWriteBuffer()
    {
    Purge(PURGE_TXCLEAR);
    }void CSerialPort::ClearReadBuffer()
    {
    Purge(PURGE_RXCLEAR);
    }DWORD CSerialPort::Read(void* lpBuf, DWORD dwCount)
    {
    ASSERT(IsOpen());
    ASSERT(!m_bOverlapped); DWORD dwBytesRead = 0;
    if (!ReadFile(m_hComm, lpBuf, dwCount, &dwBytesRead, NULL))
    TRACE(_T("Failed in call to ReadFile\n")); return dwBytesRead;
    }BOOL CSerialPort::Read(void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesRead)
    {
    ASSERT(IsOpen());
    ASSERT(m_bOverlapped); DWORD dwBytesRead = 0;
    BOOL bSuccess = ReadFile(m_hComm, lpBuf, dwCount, &dwBytesRead, &overlapped);
    if (!bSuccess)
    {
    if (GetLastError() != ERROR_IO_PENDING)
    TRACE(_T("Failed in call to ReadFile\n"));
    }
    else
    {
    if (pBytesRead)
    *pBytesRead = dwBytesRead;
    }
    return bSuccess;
    }DWORD CSerialPort::Write(const void* lpBuf, DWORD dwCount)
    {
    ASSERT(IsOpen());
    ASSERT(!m_bOverlapped); DWORD dwBytesWritten = 0;
    if (!WriteFile(m_hComm, lpBuf, dwCount, &dwBytesWritten, NULL))
    TRACE(_T("Failed in call to WriteFile\n"));
    //Add by ZhengGezhi
    ::FlushFileBuffers(m_hComm);
    return dwBytesWritten;
    }BOOL CSerialPort::Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesWritten)
    {
    ASSERT(IsOpen());
    ASSERT(m_bOverlapped); DWORD dwBytesWritten = 0;
    BOOL bSuccess = WriteFile(m_hComm, lpBuf, dwCount, &dwBytesWritten, &overlapped);
    if (!bSuccess)
    {
    if (GetLastError() != ERROR_IO_PENDING)
    TRACE(_T("Failed in call to WriteFile\n"));
    }
    else
    {
    if (pBytesWritten)
    *pBytesWritten = dwBytesWritten;
    } return bSuccess;
    }
      

  3.   

    http://www.vckbase.com/document/viewdoc/?id=1734