0% found this document useful (0 votes)
20 views6 pages

Experiment 02 Ramesh RB Transmitter (Final)

Uploaded by

rameshsv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Experiment 02 Ramesh RB Transmitter (Final)

Uploaded by

rameshsv06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT NUMBER: 02

DATE :

Question:
Write a user space program to communicate with the UART in Rugged Board
A5D2x to transmit the data

Algorithm:

1. Initialization:

Include necessary header files for serial communication, input/output, and terminal
control.
Define functions:
set_interface_attribs: Configures the serial port attributes like baud rate, data bits, etc.
uart_transmit: Transmits data over the serial port.
Declare variables:
portname: Stores the serial port device name (/dev/ttyS3 in this case).
fd: File descriptor for the serial port.
input: Character array to store user input (size 100).

2. Open Serial Port:

Open the serial port using open function with flags:


O_RDWR: Allows both reading and writing.
O_NOCTTY: Doesn't become the controlling terminal of the process.
O_SYNC: Enables synchronous mode (optional for some devices).
Check for errors during opening. If error occurs, print error message and exit.

3. Set Serial Port Attributes:

Call set_interface_attribs function to configure the serial port with desired settings:
speed: Baud rate (e.g., B9600 for 9600 baud).
Check for errors during configuration. If error occurs, print error message, close the port,
and exit.

4. Main Loop (Continuous communication):

Enter an infinite loop (while(1)) for continuous communication.


Inside the loop:
Prompt the user to enter a message using printf.
Use fgets to read user input from standard input (keyboard) and store it in input.
Use strcspn to remove the newline character (\n) from the end of the input string if
present.
Call uart_transmit function to transmit the user-entered message (input) over the serial
port.
Use usleep function to introduce a short delay (100 milliseconds in this case) before the
next iteration, allowing time for potential responses from the connected device.

5. Close Serial Port:

After exiting the loop (Ctrl+C or program termination), close the serial port using close
function.

6. uart_transmit Function:

This function takes three arguments:


fd: File descriptor for the serial port.
data: Pointer to the character array containing data to transmit.
size: Size of the data to transmit.
Use write function to write the data (data) of size (size) to the serial port.
Check the return value of write. If the entire data wasn't written, print an error message.

7. set_interface_attribs Function:

This function takes two arguments:


fd: File descriptor for the serial port.
speed: Baud rate for the serial configuration.
Use tcgetattr function to get the current attributes of the serial port and store them in a
termios structure (tty).
Set baud rate for input and output using cfsetispeed and cfsetospeed functions.
Configure other serial port attributes like:
CLOCAL | CREAD: Ignore modem control lines and enable receiver.
Character size (8 bits), no parity bits, one stop bit (standard serial settings).
Ignore parity errors, no output/input processing.
Set minimum number of characters to be read for read not to block (0 in this case).
Set timeout for read (0.5 seconds in this case).
Use tcsetattr function to apply the modified attributes to the serial port.
Check for errors during configuration. If error occurs, print error message and return -1.

This detailed algorithm explains how the program establishes serial communication, reads
user input, transmits data, and handles potential errors.

Program:

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

// Function prototypes
int set_interface_attribs(int fd, int speed);
void uart_transmit(int fd, const char* data, int size);

int main() {
char *portname = "/dev/ttyS3";
int fd;
char input[100]; // Buffer for user input

// Open serial port


fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
printf("Error opening %s: %s\n", portname, strerror(errno));
return -1;
}

// Set serial port attributes


if (set_interface_attribs(fd, B9600) < 0) {
printf("Error setting serial port attributes\n");
close(fd);
return -1;
}

while (1) {
// Get input from user
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // Remove newline character

// Transmit data
uart_transmit(fd, input, strlen(input));

// Sleep for a short duration before next iteration


usleep(100000); // 100 ms
}

// Close serial port


close(fd);
return 0;
}

// Function to set serial port attributes


int set_interface_attribs(int fd, int speed) {
struct termios tty;

if (tcgetattr(fd, &tty) < 0) {


printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}

cfsetispeed(&tty, (speed_t)speed);
cfsetospeed(&tty, (speed_t)speed);

tty.c_cflag |= (CLOCAL | CREAD); // ignore modem


controls, enable receiver
tty.c_cflag &= ~CSIZE; // clear character size mask
tty.c_cflag |= CS8; // 8-bit characters
tty.c_cflag &= ~PARENB; // no parity bit
tty.c_cflag &= ~CSTOPB; // one stop bit
tty.c_cflag &= ~CRTSCTS; // no hardware flow control

tty.c_iflag = IGNPAR; // ignore parity errors


tty.c_oflag = 0; // no output processing
tty.c_lflag = 0; // no input processing

tty.c_cc[VMIN] = 0; // read doesn't block


tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}

return 0;
}

// Function to transmit data over UART


void uart_transmit(int fd, const char* data, int size) {
int wlen = write(fd, data, size);
if (wlen != size) {
printf("Error writing to serial port\n");
}
}

Output:
Result:
The above program is run and executed Successfully

You might also like