C++ RS-485通訊示例


RS-485是一種半雙工的通信協議,經常用於工業控制模塊間的通信,因其傳輸距離遠,不容易出錯的特點,應用廣泛。

此為windows下示例,linux需做相應修改。

#pragma once
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

class Net485
{
public:
    Net485(long baud_rate, wchar_t* port_name);
    bool send(BYTE data[], int length);
protected:
    void set_up_serial_port(long baud);

private:

    HANDLE serial_port;

};

#include "Net485.h"
#include <iostream>

Net485::Net485(long baud_rate, wchar_t* port_name)
{
    const wchar_t name[8] = L"COM4";
    serial_port = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
    if (serial_port == INVALID_HANDLE_VALUE)
    {
        fprintf(stderr, "Error opening portn");
        CloseHandle(serial_port);
    }
    else 
    {
        set_up_serial_port(baud_rate);
    }
}

bool Net485::send(BYTE data[],int length)
{
    if (serial_port == INVALID_HANDLE_VALUE)
    {
        printf("發送失敗::INVALID_HANDLE_VALUE");
        return false;
    }

    DWORD dwTx = 0;
    BOOL ret = FALSE;
    DWORD dwLength = length;

    Sleep(10);

    if (dwLength > 0)
    {
        ret = WriteFile(serial_port, data, dwLength, &dwTx, NULL);
        if (ret == FALSE)
        {
            printf("發送失敗");
            return false;
        }
    }

    return true;
}

void Net485::set_up_serial_port(long baud)
{
    DCB properties;

    // 設置讀寫緩沖區
    GetCommState(serial_port, &properties);

    switch (baud)
    {
    case 1200:
        properties.BaudRate = CBR_1200;
        break;
    case 2400:
        properties.BaudRate = CBR_2400;
        break;
    case 4800:
        properties.BaudRate = CBR_4800;
        break;
    case 9600:
        properties.BaudRate = CBR_9600;
        break;
    case 14400:
        properties.BaudRate = CBR_14400;
        break;
    case 19200:
        properties.BaudRate = CBR_19200;
        break;
    case 38400:
        properties.BaudRate = CBR_38400;
        break;
    default:
        fprintf(stderr, "Invalid baud rate: %ldn", baud);
        exit(0);
        break;
    }

    properties.Parity = NOPARITY;
    properties.ByteSize = 8;
    properties.StopBits = ONESTOPBIT;

    SetCommState(serial_port, &properties);

    //在讀寫串口前,用 PurgeComm 函數清空緩沖區
    PurgeComm(serial_port, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_TXABORT | PURGE_TXABORT);

    return;
}

使用示例

/// 485PLC通信
Net485* net = new Net485(9600,L"COM4");

BYTE data[2];
data[0] = 0x01;
if (info.th == 1) {

    data[1] = 0x11;
}
else {

    data[1] = 0x12;
}
net->send(data,sizeof(data));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM