Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write programs using the serial port to carry out a dialogue between two compute

ID: 3638577 • Letter: W

Question

Write programs using the serial port to carry out a dialogue between two
computers ( C Programming in Ubuntu)
1. the computers alternate in sending data
2. exit with ctrlC

Explanation / Answer

1)for sending #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. */ 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 /* 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. */ 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); } 2)exit with ctrlC #!/bin/sh cleanup() { echo "Caught Signal ... Terminated." exit 1 } ... while [ 1 ] do # enter the optional parameter if we need to set -m #invoke perl script in background ${HOME}/${PROCESS}.pl ${OPTIONAL} 2>&1 > $LOGFILENAME & #check exit status status=$? if [ $status -ne 0 ] then echo ${PROCESS}.pl ${OPTIONAL} Job fails with status $status exit $status fi # write the PID into files echo $! > ${PID_DIR}/${PROCESS}.perl chmod 755 ${PID_DIR}/${PROCESS}.perl # then bring the job to foreground fg %+ 2>/dev/null trap 'cleanup' 2 9 15 17 19 20 sleep ${SLEEP}; done exit 0
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote