⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q_test.c

📁 rs485串口的通信源代码,希望大家多多下载啊,指点指点
💻 C
字号:
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Q_TEST.C
//
// Author:  Anthony A. Kempka
//          Device Drivers International, LLC
//
// Description:
// ------------
// This program will attempt to open a channel to the driver RS485NT and
// make sure iterative ReadFile() and WriteFile() calls.
//
// *Note* 
// This is a WIN32 console application.
//

//---------------------------------------------------------------------------
//
// Basic includes
//

#include "windows.h"
#include "winioctl.h"
#include "stdio.h"
#include "conio.h"

#include "RS485IOC.H"        // IOCTL codes


//---------------------------------------------------------------------------
//
// Main 
//
// Main processing loop.
// 
int main (void)
{
    ULONG   i;
    UCHAR   DummyBuffer[100];
    UCHAR   DriverName[] = "\\\\.\\RS485NT";
    HANDLE  DriverHandle;
    DWORD   BytesWritten, BytesRead;
    BOOL    status;

    //
    // Open a channel to the driver
    //

    printf ("Attempting to open device: %s\n", DriverName);
    DriverHandle = CreateFile (DriverName, GENERIC_READ | GENERIC_WRITE,
                               0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

    //
    // Do we have a valid handle? (If not, the driver probably isn't loaded)
    //

    if (DriverHandle == INVALID_HANDLE_VALUE) {
        printf ("Error: CreateFile failed!\n");
        return (1);
    } else {
        printf ("CreateFile success!\n\n");
    }

    //
    // Simple WriteFile, ReadFile 
    //

    memcpy (DummyBuffer, "This is a test!", 15);

    status = WriteFile (DriverHandle, DummyBuffer, 15, &BytesWritten, 0);
    if (status) {
        printf ("WriteFile: BytesWritten=%li\n", BytesWritten);
    } else {
        printf ("WriteFile: Error!\n");
    }

    //
    // Wait for some receive response time
    //

    Sleep (1000);

    status = ReadFile (DriverHandle, DummyBuffer, 50, &BytesRead, 0);
    if (status) {
        printf ("ReadFile: BytesRead=%li\n", BytesRead);
    } else {
        printf ("ReadFile: Error!\n");
    }

    for (i=0; i<BytesRead; i++) {
        printf ("%02X ", DummyBuffer[i]);
    }

    printf ("\n");

    printf ("\nWrite and Read sequence complete!\n");
    printf ("Closing driver handle\n");

    //
    // Close the channel to the driver
    //

    CloseHandle (DriverHandle);

    //
    // Exit
    //
    return (0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -