這次上課   老師說要做一個 RS232 的傳輸

想說如果大家都用 BCB  C#  (BCB  手上有書了,C# 有元件了)

作起來好像有點簡單

所以這次就直接用 Dev - C++  用 Win32 所用的 API下去寫

可傳可接   不過   overlapped 的地方好像有點問題   ~"~

要期中了   改天想到再改把  XD

寫了四、五天 Good

對 VC++ 越來越有興趣  Good

 

 

 

 

 

#include <cstring>
#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

void ReadFun();
//Create File RS232
HANDLE    hComm;
DCB        Commdcb;
//Event
long EvWait;
// Overlap
OVERLAPPED OverLap;

int main()
{
    OverLap.hEvent = CreateEvent(NULL, true, false, NULL);
    //Read Use Thread
    HANDLE    hThread;
    DWORD    dwThread;
    
    hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
    
    if (hComm == INVALID_HANDLE_VALUE)
    {
        cout << "Create File Error" << endl;
        return 0;
    }
    GetCommState(hComm, &Commdcb);
    Commdcb.BaudRate = CBR_115200;
    Commdcb.ByteSize = 8;
    Commdcb.Parity = NOPARITY;
    Commdcb.StopBits = ONESTOPBIT;

    
    
    if (!SetCommState(hComm, &Commdcb))
    {
        cout << "Set Comm Error" << endl;
        CloseHandle(hComm);
        return 0;
    }
    
//    EvWait = EV_CTS | EV_DSR | EV_RING | EV_RLSD | EV_BREAK | EV_RXCHAR | EV_RXFLAG | EV_TXEMPTY | EV_ERR;
    EvWait = EV_RXCHAR | EV_TXEMPTY;
    if (!SetCommMask(hComm,EvWait))
    {
        cout << "Event Set Error" << endl;
        return 0;
    }
    // setup device buffer
    SetupComm(hComm, 10240, 10240);
    // purge any information in buffer
    PurgeComm(hComm, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
    
    COMMTIMEOUTS CommTimeOuts;
    CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
    CommTimeOuts.ReadTotalTimeoutMultiplier = 0 ;
    CommTimeOuts.ReadTotalTimeoutConstant = 0;
    CommTimeOuts.WriteTotalTimeoutMultiplier = 1;
    CommTimeOuts.WriteTotalTimeoutConstant = 0 ;
    SetCommTimeouts(hComm, &CommTimeOuts);

    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadFun, NULL, 0, &dwThread);
    if (OverLap.hEvent == NULL)
    {
        cout << "Error Create OverLap" << endl;
        return 0;
    }

    while (1)
    {
        char output;
        DWORD dwWrite;
        dwWrite = 1;
        fflush(stdin);
        output = getch();
        if (!WriteFile(hComm, &output, 1, &dwWrite, &OverLap))
        {
            //cout << "Write Error" << endl;
            //break;
        }
    }
    CloseHandle(hThread);
    CloseHandle(hComm);
    system("pause");
    return 0;
}

void ReadFun()
{
    DWORD    dwEv;
    DWORD    dwRead;
    char    input;
    dwRead = 1;
    while (1)
    {
        WaitCommEvent(hComm, &dwEv, NULL);
        if (dwEv == EV_RXCHAR)
        {
            if (!ReadFile(hComm, &input, dwRead, &dwRead, &OverLap))
            {
                //cout << "Read Error" << endl;
                //break;
            }
            printf("%c",input);
        }
    }
}


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 ffyy99 的頭像
    ffyy99

    喜歡亂搞的世界

    ffyy99 發表在 痞客邦 留言(7) 人氣()