两个函数串行发送不同波特率数据的问题? [英] problems with two functions for sending data at different baudrate serially?

查看:84
本文介绍了两个函数串行发送不同波特率数据的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//Serial Port communication in Windows  for test purpose!!//
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>


int main()
{
    int _kbhit();
    int _getch();


    // Defining hexadecimal bytes to be sent a 200 bps//
    unsigned int bytes_to_send[1];
    bytes_to_send[0] = 0xF0; /* 11110000b */

     // Defining hexadecimal bytes to be sent a 10400 bps//
    unsigned int databytes_send[5];
    databytes_send[0] = 0x81;
    databytes_send[1] = 0x11;
    databytes_send[2] = 0xF1;
    databytes_send[3] = 0x11;
    databytes_send[4] = 0x04;

    int GetPressedKey()
    {
        int ret = 0;
        if (_kbhit())
        {
            ret = _getch();
        }
        return ret;
    }

    // Declare variables and structures//
    HANDLE hSerial;
    DCB dcbSerialParams = {0};
    COMMTIMEOUTS timeouts = {0};

    // Open the available serial port number//
    fprintf(stderr, "Opening serial port...");
    hSerial = CreateFile(
                "COM4", GENERIC_READ|GENERIC_WRITE, 0, NULL,
                OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
    if (hSerial == INVALID_HANDLE_VALUE)
    {
        fprintf(stderr, "Error\n");
        return 1;
    }
    else
        fprintf(stderr, "OK\n");

    // Set device parameters (200 baud, 1 start bit,
    // 1 stop bit, no parity)
    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    if (GetCommState(hSerial, &dcbSerialParams) == 0)
    {
        fprintf(stderr, "Error getting device state\n");
        CloseHandle(hSerial);
        return 1;
    }

    while(1)
    {

    // Set baudrate to send bytes at 200 bps//
    dcbSerialParams.BaudRate = 200;
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;
    if(SetCommState(hSerial, &dcbSerialParams) == 0)
    {
        fprintf(stderr, "Error setting device parameters\n");
        CloseHandle(hSerial);
        return 1;
    }

    // Set COM port timeout settings//
    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 10;
    if(SetCommTimeouts(hSerial, &timeouts) == 0)
    {
        fprintf(stderr, "Error setting timeouts\n");
        CloseHandle(hSerial);
        return 1;
    }

    // Send specified text (remaining command line arguments)
    DWORD bytes_written = 0;
    fprintf(stderr, "sending bytes...\n");

        //Transmit slow initialization data
        if (!WriteFile(hSerial, bytes_to_send, 1, &bytes_written, NULL))

            break;

        fprintf(stderr, "%d bytes written\n", bytes_written);

        //check for key press event
        if (GetPressedKey())
            break;

        // Set baudrate to send bytes at 10400 bps//
        dcbSerialParams.BaudRate = 10400;
        dcbSerialParams.ByteSize = 8;
        dcbSerialParams.StopBits = ONESTOPBIT;
        dcbSerialParams.Parity = NOPARITY;
        if(SetCommState(hSerial, &dcbSerialParams) == 0)
        {
            fprintf(stderr, "Error setting device parameters\n");
            CloseHandle(hSerial);
            return 1;
        }

        // Set COM port timeout settings//
        timeouts.ReadIntervalTimeout = 50;
        timeouts.ReadTotalTimeoutConstant = 50;
        timeouts.ReadTotalTimeoutMultiplier = 10;
        timeouts.WriteTotalTimeoutConstant = 50;
        timeouts.WriteTotalTimeoutMultiplier = 10;
        if(SetCommTimeouts(hSerial, &timeouts) == 0)
        {
            fprintf(stderr, "Error setting timeouts\n");
            CloseHandle(hSerial);
            return 1;
        }

        //Transmit fast initialization data
        if (!WriteFile(hSerial, databytes_send, 5, &bytes_written, NULL))

            break;

        fprintf(stderr, "%d bytes written\n", bytes_written);

        //Check for key press event
        if (GetPressedKey())
            break;
    }

    DWORD bytes_written;

    CloseHandle(hSerial);

    if(!WriteFile(hSerial, bytes_written,5, &bytes_written, NULL))
    {
        fprintf(stderr, "End of transmission\n");
        CloseHandle(hSerial);

        return 1;
    }
    fprintf(stderr, "%d bytes written\n", bytes_written);



    // Close serial port
    fprintf(stderr, "Closing serial port...");
    if (CloseHandle(hSerial) == 0)
    {
        fprintf(stderr, "Error\n");
        return 1;
    }
    fprintf(stderr, "OK\n");

    // exit normally
    return 0;
}

推荐答案

vinod bv写道:
vinod b v wrote:

在200波特发送字节0xF0时我应该得到25ms低和25ms高方波,接下来我的5个数据字节应该在10400发送,但我无法在示波器中看到确切的波形。代码中有什么问题??

I am supposed to get 25ms low and 25ms high square wave upon sending byte 0xF0 at 200 baud, and next my 5 databytes should be sent at 10400, but i am not able to see the exact waveform in my oscilloscope. is there something wrong in the code??







你对这个范围有什么看法?

限制程序第一部分的讨论(200波特),应该有一个脉冲,如:




What do you see on the scope?
Limiting the discussion on the first part of your program (200 baud), there should be a single pulse, like:

______      _______________________________________________
      |_____|



是高 - >低转换,25毫秒低(起始位+4个零位)然后低 - >高转换( 4个一位,停止位,最后线路保持高电平,空闲)。

参见:异步串行沟通 [ ^ ]。


这篇关于两个函数串行发送不同波特率数据的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆