波特率串行窗口

时间:2011-07-04 07:58:12

标签: c windows serial-port

为了探测Windows串口,我编写了这个程序。我将串口波特率设置为115200 bps。当我运行该程序时,经过的时间是1250毫秒,因此,波特率仅达到102400 bps。我也用类似的程序检查接收波特率,波特率是相同的。

以下是该计划:

char* message = 
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

int numBytes = 144;

c0 = clock()

for (;;)

{

sendSerial(&hCom, message, numBytes );
tx +=numBytes;

//14400 bytes * 8 = 115200 bps

    if (tx >= 14400)
    {
        c1 = clock();
        runtime_diff_ms = (c1 - c0) * 1000. / CLOCKS_PER_SEC;
        printf("Tx frames %d Time ms %f", tx, runtime_diff_ms);
        system ("pause");
        return -1;
    }
}

bool sendSerial(HANDLE *hCom, char *WriteBuffer, DWORD dwBytesToWrite)
{
    DWORD dwBytesWritten = 0;
    BOOL bErrorFlag = FALSE;

     bErrorFlag = WriteFile( 
                    *hCom,           // open file handle
                    WriteBuffer,      // start of data to write
                    dwBytesToWrite,  // number of bytes to write
                    &dwBytesWritten, // number of bytes that were written
                    NULL);    
...
}

这些是我的串口规格:

DCB dcbSerialParams;
COMMTIMEOUTS timeouts;  
dcbSerialParams.BaudRate=CBR_115200;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;

timeouts.ReadIntervalTimeout=MAXDWORD; 
timeouts.ReadTotalTimeoutMultiplier=MAXDWORD; 
timeouts.ReadTotalTimeoutConstant=5000; // 5sec
timeouts.WriteTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=100;

任何人都知道如何解决此问题达到115200 bps?

1 个答案:

答案 0 :(得分:5)

每个字符有10位 - 数据为8位加上起始位和停止位。

如果计算每个字符10位的14400个字符应该以115200 bps的速度运行多长时间,那么你得到1250毫秒:

(14400 characters * 10 bits/character) / (115200 bits/second) =  1.250 seconds