Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot have ModbusRTU (serial) working in linux #75

Open
arkypita opened this issue Dec 6, 2024 · 2 comments
Open

cannot have ModbusRTU (serial) working in linux #75

arkypita opened this issue Dec 6, 2024 · 2 comments

Comments

@arkypita
Copy link

arkypita commented Dec 6, 2024

Hi, I tried the example with ModbusTCP and it works perfectly
I also need to use serial port, but I am not able to get the library to work properly in ModbusRTU mode with Linux (timeout, sometime read sometime not, read 1 address ok, read 2 addresses fail get timeout etc)

I can't find an example for the serial in Linux, do you have one?

@arkypita
Copy link
Author

arkypita commented Dec 6, 2024

Actually my best implementation look like this.
It works well if I read ONE register but fails when I read two or more register (return NMBS_ERROR_TIMEOUT).

void * connect_rtu(char * port, int speed, char parity, int data_bits, int stop_bits)
{
    printf("Opening serial port... ");

    int fd = open(port, O_RDWR | O_NOCTTY | O_SYNC);

    if (fd < 0) {
        printf("Error!\n");
        return 1;
    }

    struct termios tty;
    if (tcgetattr(fd, &tty) != 0) {
        printf("Error getting terminal attributes!\n");
        close(fd);
        return 1;
    }

    // Configure port
    cfsetospeed(&tty, speed); // Set output baud rate
    cfsetispeed(&tty, speed); // Set input baud rate

    tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8 data bits
    tty.c_cflag &= ~PARENB;                    // No parity
    tty.c_cflag &= ~CSTOPB;                    // 1 stop bit
    tty.c_cflag |= (CLOCAL | CREAD);           // Local line | Enable receiver

    tty.c_iflag &= ~IGNBRK; // Disable break processing
    tty.c_lflag = 0;        // No signaling chars, no echo, no canonical processing
    tty.c_oflag = 0;        // No remapping, no delays
    tty.c_cc[VMIN] = 1;     // Read at least 1 character
    tty.c_cc[VTIME] = 1;    // Wait for up to 0.1s

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        printf("Error setting terminal attributes!\n");
        close(fd);
        return NULL;
    }

    printf("Success!\n");

    client_connection = fd;
    return &client_connection;
}
int32_t read_fd_linux(uint8_t* buf, uint16_t count, int32_t timeout_ms, void* arg) 
{

    if (!arg) return -1;
    int fd = *(int*) arg;

    struct pollfd pfd = {
        .fd = fd,
        .events = POLLIN // Wait for data to read
    };

    int poll_result = poll(&pfd, 1, timeout_ms);
    if (poll_result < 0) {
        return -1; // Error during poll
    } else if (poll_result == 0) {
        return 0; // Timeout occurred
    }

    if (pfd.revents & POLLIN) {
        ssize_t bytes_read = read(fd, buf, count);
        if (bytes_read < 0) {
            return -1; // Error during read
        }
        return (int32_t)bytes_read;
    }

    return -1; // Unexpected condition

}
int32_t write_fd_linux(const uint8_t* buf, uint16_t count, int32_t timeout_ms, void* arg) 
{

    if (!arg) return -1;
    int fd = *(int*) arg;

#if (TRANSPORT_MODE == MODE_RTU)

    struct pollfd pfd = {
        .fd = fd,
        .events = POLLOUT // Wait for the ability to write
    };

    int poll_result = poll(&pfd, 1, timeout_ms);
    if (poll_result < 0) {
        return -1; // Error during poll
    } else if (poll_result == 0) {
        return 0; // Timeout occurred
    }

    if (pfd.revents & POLLOUT) {
        ssize_t bytes_written = write(fd, buf, count);
        if (bytes_written < 0) {
            return -1; // Error during write
        }
        return (int32_t)bytes_written;
    }

    return -1; // Unexpected condition
}

@arkypita
Copy link
Author

arkypita commented Dec 6, 2024

What I also try is to use the same read/write implementation as in client-tcp, but i have the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant