Write programs using the serial port to carry out a dialogue between two compute
ID: 3638986 • Letter: W
Question
Write programs using the serial port to carry out a dialogue between twocomputers
1. the computers alternate in sending data
2. exit with ctrlC
Please Edit these two files. One is for sending and the other is for receiving
1)for sending
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
main()
{
open_port();
}
int
open_port(void)
{
int fd; /* File descriptor for the port */
int n;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
n = write(fd, "ATZ ", 4);
if (n < 0)
fputs("write() of 4 bytes failed! ", stderr);
return (fd);
}
for receiving
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
main() {
open_port();
}
int
open_port(void)
{
int fd; /* File descriptor for the port */
int res;
char buf[255];
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
/*
fcntl(fd, F_SETFL, FNDELAY); */
res = read(fd,buf,255);
printf("res=%d ",res);
buf[res]=0; /* set end of string, so we can printf */
printf(":%s:%d ", buf, res);
return (fd);
}
Explanation / Answer
#include /* Standard input/output definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* POSIX terminal control definitions */ /* * 'open_port()' - Open serial port 1. * * Returns the file descriptor on success or -1 on error. */ int open_port(void) { int fd; /* File descriptor for the port */ fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { /* * Could not open the port. */ perror("open_port: Unable to open /dev/ttyS0 - "); } else fcntl(fd, F_SETFL, 0); return (fd); } Open Options You'll notice that when we opened the device file we used two other flags along with the read+write mode: fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); The O_NOCTTY flag tells UNIX that this program doesn't want to be the "controlling terminal" for that port. If you don't specify this then any input (such as keyboard abort signals and so forth) will affect your process. Programs like getty(1M/8) use this feature when starting the login process, but normally a user program does not want this behavior. The O_NDELAY flag tells UNIX that this program doesn't care what state the DCD signal line is in - whether the other end of the port is up and running. If you do not specify this flag, your process will be put to sleep until the DCD signal line is the space voltage. Writing Data to the Port Writing data to the port is easy - just use the write(2) system call to send data it: n = write(fd, "ATZ ", 4); if (n < 0) fputs("write() of 4 bytes failed! ", stderr); The write function returns the number of bytes sent or -1 if an error occurred. Usually the only error you'll run into is EIO when a MODEM or data link drops the Data Carrier Detect (DCD) line. This condition will persist until you close the port. Reading Data from the Port Reading data from a port is a little trickier. When you operate the port in raw data mode, each read(2) system call will return the number of characters that are actually available in the serial input buffers. If no characters are available, the call will block (wait) until characters come in, an interval timer expires, or an error occurs. The read function can be made to return immediately by doing the following: fcntl(fd, F_SETFL, FNDELAY); The FNDELAY option causes the read function to return 0 if no characters are available on the port. To restore normal (blocking) behavior, call fcntl() without the FNDELAY option: fcntl(fd, F_SETFL, 0); This is also used after opening a serial port with the O_NDELAY option. Closing a Serial Port To close the serial port, just use the close system call: close(fd);Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.