串口读取c ++

时间:2014-03-21 14:38:32

标签: c++

我正在尝试用c ++编写代码来编写和读取串口我写了下面的代码,我现在试图通过在USB到串口电缆上回路来测试这个,当我写任何东西,除了我读了它成功但是当写入零时,程序停止读取并且在零之后不读取任何东西并且在显示读取缓冲区时给我零(在该程序中它显示读取缓冲区的零但是如果我写任何东西,如写入循环写入“单词= 127-i“阅读器缓冲区将显示我写的所有内容,直到达到零为止”,快速提示任何帮助

#include    <windows.h>
#include    <stdlib.h>
#include    <stdio.h>
#include    <string.h>
#include    <commdlg.h>
#include    <iostream>
//#include  <windef.h>
using namespace std;
int nread,nwrite;

void main()
{
    HANDLE hSerial;
    COMMTIMEOUTS timeouts;
    COMMCONFIG dcbSerialParams;
    char words[128], *buffWrite;
    DWORD dwBytesWritten, dwBytesRead;

    hSerial = CreateFile(TEXT("COM1"),GENERIC_READ  |GNERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);      

    if(hSerial == INVALID_HANDLE_VALUE) 
    {
        if (GetLastError() == ERROR_FILE_NOT_FOUND)
        {
            printf(" serial port does not exist \n");
        }
        printf(" some other error occured. Inform user.\n");
    }

    // DCB    dcbSerialParams ;
    //GetCommState( hSerial, &dcbSerialParams.dcb);
    if (!GetCommState(hSerial, &dcbSerialParams.dcb)) 
    {
        printf("error getting state \n");
    }

    dcbSerialParams.dcb.DCBlength = sizeof(dcbSerialParams.dcb);
    dcbSerialParams.dcb.BaudRate = CBR_2400;
    dcbSerialParams.dcb.ByteSize = 8;
    dcbSerialParams.dcb.StopBits = ONESTOPBIT;
    dcbSerialParams.dcb.Parity = NOPARITY;

    dcbSerialParams.dcb.fBinary = TRUE;
    dcbSerialParams.dcb.fDtrControl = DTR_CONTROL_ENABLE;
    dcbSerialParams.dcb.fRtsControl = RTS_CONTROL_ENABLE;
    dcbSerialParams.dcb.fOutxCtsFlow = TRUE;
    dcbSerialParams.dcb.fOutxDsrFlow = TRUE;
    dcbSerialParams.dcb.fDsrSensitivity= FALSE;
    dcbSerialParams.dcb.fAbortOnError = TRUE;

    if(!SetCommState(hSerial, &dcbSerialParams.dcb)) 
    {
        printf(" error setting serial port state \n");
    }
    SetCommMask( hSerial,EV_RXCHAR);

    GetCommTimeouts(hSerial,&timeouts);
    //COMMTIMEOUTS timeouts = {0};

    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier= 10;

    if(!SetCommTimeouts(hSerial, &timeouts)) 
    {
        printf("error setting port state \n");
    }

    // printf("This is a string to be written to serial port COM1 \n\n\n");

    //****************Write Operation*********************//
    //words = "This is a string to be written to serial port COM1";
    //dwBytesWritten=1;
    cout<<"Data written to write buffer is \n";
    for(int i=0;i<128;i++)
    {
        words[i]=i;
        cout<<(int)words[i]<<",";
    }
    cout<<endl;
    nwrite = strlen(words);

    buffWrite = words;
    dwBytesWritten = 0;

    if (!WriteFile(hSerial, buffWrite, nwrite, &dwBytesWritten, NULL)) 
    { 
        printf("error writing to output buffer \n");
    }


    //***************Read Operation******************//
    char buffsRead[128]="0",*buffRead;  
    dwBytesRead = 0;
    nread = strlen(buffsRead);
    buffRead = buffsRead;
    //ReadFile(hSerial, buffRead, nread, &dwBytesRead, NULL);
    if (!ReadFile(hSerial, buffRead, nread, &dwBytesRead, NULL)) 
    {
        printf("error reading from input buffer \n");
    }
    // printf("Data read from read buffer is \n %s \n",(int)buffsRead);
    cout<<"Data read from read buffer is \n ";
    for(int j=0;j<128;j++)
    {
        cout<<(int)buffsRead[j]<<",";
    }
    cout<<endl;

    CloseHandle(hSerial);
    // system("PAUSE");
}  

1 个答案:

答案 0 :(得分:0)

问题在于计算要写入的字节数。

nwrite = strlen(words);

这对二进制数据不起作用,因为strlen在第一个'\0'(NUL字符)处停止。

只需在循环中调整nwrite,以便最终添加字符数。取代

words[i]=i;

words[nwrite++] = i;

并摆脱nwrite = strlen(words);,事情会更好。