Not reading data from the serial port continuously

79 Views Asked by At
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

int main()
{
    const char *device = "/dev/ttyUSB0"; // Replace with your USB TTL device name
    int serialport_fd,output_fd,serial_rbytes=0,serial_wbytes=0, python_status;
    char serial_buffer[100];
    output_fd = open("/home/inbe1e-dl3882av/navin/systecall/output.txt", O_RDWR | O_CREAT, S_IRUSR |S_IWUSR);
    if(output_fd< 0)
    {
        printf("Output File is not opened\n");
        return 1;
    }
    serialport_fd = open(device, O_RDWR | O_NOCTTY);
    if(serialport_fd < 0)
    {
        printf("Serial port not opened\n");
        return 1;
    }
    struct termios options;
    tcgetattr(serialport_fd, &options);
    cfsetispeed(&options, B115200); // Set baud rate 
    cfsetospeed(&options, B115200);
    options.c_cflag |= (CLOCAL | CREAD); // Enable receiver and set local mode
    options.c_cflag &= ~PARENB; // No parity
    options.c_cflag &= ~CSTOPB; // 1 stop bit
    options.c_cflag &= ~CSIZE; // Mask the character size bits
    options.c_cflag |= CS8; // Set 8 data bits
    tcsetattr(serialport_fd, TCSANOW, &options);
    while(1)
    {
        read_again:
            serial_rbytes = read(serialport_fd,serial_buffer,sizeof(serial_buffer));
            if(serial_rbytes > 0)
            {
                write_again:
                printf("serial Read Bytes %d and serial buffer is %s\n",serial_rbytes,serial_buffer);
                serial_wbytes = write(output_fd,serial_buffer,serial_rbytes);
                if(serial_wbytes > 0)
                {
                    printf("successfully Written into the output.txt file\n");
                    python_status = system("python3 read.py");
                }
            }
            else if(serial_rbytes <= 0)
            {
                printf("Failed to Read\n\n");
                goto read_again;
            }
    }
    close(serialport_fd);
    close(output_fd);
}

python code

import sys
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit

class ConsoleWindow(QMainWindow):
    def __init__(self, file_path, display_duration_seconds):
        super().__init__()
        self.file_path = file_path
        self.display_duration_seconds = display_duration_seconds
        self.initUI()

    def initUI(self):
        self.text_edit = QTextEdit(self)
        self.setCentralWidget(self.text_edit)
        self.setWindowTitle('Console Window')
        self.setGeometry(100, 100, 800, 600)

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.closeWindow)
        self.timer.start(self.display_duration_seconds * 1000)

        self.readFile()
        self.show()

    def readFile(self):
        try:
            with open(self.file_path, 'r') as file:
                data = file.read()
                self.text_edit.setPlainText(data)
        except FileNotFoundError:
            self.text_edit.setPlainText("File not found: " + self.file_path)

    def closeWindow(self):
        self.timer.stop()
        self.close()

def main(file_path, display_duration_seconds):
    app = QApplication(sys.argv)
    window = ConsoleWindow(file_path, display_duration_seconds)
    sys.exit(app.exec_())

if __name__ == '__main__':
    file_path = '/home/inbe1e-dl3882av/navin/systecall/output.txt'  # Replace with the path to your input file
    display_duration_seconds = 5  # Adjust the display duration as needed
    main(file_path, display_duration_seconds)

I am running all these codes in the Ubuntu 22.04 and I have looped the Rx and Tx of the serialport. The C program is working correctly but sometimes it's not reading the data from the serialport and displaying it on the PythonQt Framework ,but mostof the times the excepted output(i.e reading data from the serial port and writting it to the output.txt and displaying it on the pythonQt frame work ) Can you please help me with this issue , I'm giving the input to the serial using the echo "hello" >> /dev/ttyUSB0 ,and I had observed that whenever the data is not reading from the serial port the data is getting displayed on the **minicom ** Thanks

0

There are 0 best solutions below