390 likes | 631 Views
Embedded System Laboratory Serial Port. Suthit Rattathanapad suthitr@gmail.com. Basic of Serial Communications. 1. Configuring the Serial Port. 2. Advanced Serial Programming. Appendix B, ASCII Codes. 3. 5. Appendix A, RS-232 Pinouts. 4. Contents. What are Serial Communications?.
E N D
Embedded System LaboratorySerial Port SuthitRattathanapadsuthitr@gmail.com
Basic of Serial Communications 1 Configuring the Serial Port 2 Advanced Serial Programming Appendix B, ASCII Codes 3 5 Appendix A, RS-232 Pinouts 4 Contents Embedded System Laboratory
What are Serial Communications? • Computers transfer information (data) one or more bits at a time. Serial refers to the transfer of data one bit at a time. Serial communications include most network devices, keyboards, mice, MODEMs, and terminals. • When doing serial communications each word (i.e. byte or character) of data you send or receive is sent one bit at a time. Each bit is either on or off. The terms you'll hear sometimes are mark for the on state and space for the off state. • The speed of the serial data is most often expressed as bits-per-second ("bps") or baudot rate ("baud"). This just represents the number of ones and zeroes that can be sent in one second. Embedded System Laboratory
What is RS-232? • RS-232 is a standard electrical interface for serial communications defined by the Electronic Industries Association ("EIA"). RS-232 actually comes in 3 different flavors (A, B, and C) with each one defining a different voltage range for the on and off levels. The most commonly used variety is RS-232C, which defines a mark (on) bit as a voltage between -3V and -12V and a space (off) bit as a voltage between +3V and +12V. The RS-232C specification says these signals can go about 25 feet (8m) before they become unusable. You can usually send signals a bit farther than this as long as the baud is low enough. Embedded System Laboratory
What is RS-232? (Cont.) Embedded System Laboratory
Signal Definitions • GND • The common signal ground connection. All the other signals refer to this reference. This must be connected at both connectors. • TXD • The data signal from the PC. • RXD • The data signal to the PC. • DCD • A control signal to the PC. The modem is receiving a valid carrier signal from the remote modem. Embedded System Laboratory
Signal Definitions (Cont.) • CTS • A control signal to the PC. The remote host is ready to receive your data. • RTS • A control signal from to the PC. The PC is ready to receive data. • DSR • A control signal to the PC. The modem is ready to answer or originate a call – as opposed to being in a test mode. • DTR • A control signal from the PC. The PC uses this to enable the modem. The modem is allowed to go on-line or answer a call. Embedded System Laboratory
Asynchronous Communications Embedded System Laboratory
Accessing Serial Ports • Routines: • Usage: • The stty utility shall set or report on terminal I/O characteristics for the device that is its standard input. stty [ -a| -g] Embedded System Laboratory
Example Access Serial Port • Download: Virtual Serial Ports Emulator << Click • Download: Serial Terminal << Click • Install Virtual Serial Ports Emulator • Install Serial Terminal Embedded System Laboratory
Example Access Serial Port (Cont.) • Click Create new device… • Select Device type: pair • Click Next • Select your virtual serial port 1 and 2 • Click Finish Embedded System Laboratory
Example Access Serial Port (Cont.) • Open Vmware • Click Edit virtual machine setting • Add Serial Port • Connection: Use physical serial port • Select your virtual serial port Embedded System Laboratory
Example Access Serial Port (Cont.) • Practice: A list of Serial Port name • Practice: Change baud rate to 38400 • Practice: Enable parity bit • Practice: Print only one line containing string $ root@slax:# dmesg | greptty root@slax:# stty –F /dev/ttyS0 38400 root@slax:# stty –F /dev/ttyS0 parenb root@slax:# cat /dev/ttyS0 | grep $ -m 1 Embedded System Laboratory
Basic of Serial Communications 1 Configuring the Serial Port 2 Advanced Serial Programming Appendix B, ASCII Codes 3 5 Appendix A, RS-232 Pinouts 4 Contents Embedded System Laboratory
Opening and Closing terminal • Routines: • Usage: • The open() function establishes the connection between a file and a file descriptor. It creates an open file description that refers to a file and a file descriptor that refers to that open file description. The file descriptor is used by other I/O functions to refer to that file. The path argument points to a pathname naming the file. • The close() function will deallocate the file descriptor indicated by fildes. To deallocate means to make the file descriptor available for return by subsequent calls to open() or other functions that allocate file descriptors. All outstanding record locks owned by the process on the file associated with the file descriptor will be removed (that is, unlocked). open (*path, oflag, ... ) close (fildes) Embedded System Laboratory
Getting and Setting terminal • Routines: • Usage: • Most systems support the POSIX terminal (serial) interface for changing parameters such as baud rate, character size, and so on. The first thing you need to do is include the file <termios.h>; this defines the terminal control structure as well as the POSIX control functions. • These get and set terminal attributes, respectively; you provide a pointer to a termios structure that contains all of the serial options available: tcgetattr (fildes, *termios_p) tcsetattr (fildes, optional_actions, *termios_p); cfgetispeed (*termios_p) cfsetispeed (*termios_p, speed_t) cfgetospeed (*termios_p) cfsetospeed (*termios_p, speed_t) Embedded System Laboratory
Reading and Writing terminal • Routines: • Usage: • The read() function attempts to readnbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf. • If nbyte is 0, read() will return 0 and have no other results. • The write() function attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes. • If nbyteis 0, write() will return 0 and have no other results if the file is a regular file; otherwise, the results are unspecified. read (fildes, *buf, nbyte) write (fildes, *buf, nbyte) Embedded System Laboratory
Example Configuring the Serial Port • Practice: vi serial.c #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <errno.h> #include <termios.h> int main() { int port; int count = 0; char buf[] = "\r\nPlease press any key... in 10 sec\r\n"; structtermiostty_attributes; Embedded System Laboratory
Example Configuring the Serial Port • Practice: (Cont.) port = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK); tcgetattr(port, &tty_attributes); tty_attributes.c_cflag = CS8 | CLOCAL | CREAD; tty_attributes.c_iflag = IGNPAR | IGNBRK; tty_attributes.c_oflag = 0; cfsetospeed(&tty_attributes,B9600); // Set the baud rate cfsetispeed(&tty_attributes,B9600); tcflush(port, TCIOFLUSH); tcsetattr(port, TCSANOW, &tty_attributes); sleep(1); write(port, buf, sizeof(buf)); sleep(10); Embedded System Laboratory
Example Configuring the Serial Port • Practice: (Cont.) while (read(port, &buf[count], 1) > 0) { count++; } buf[count++] = 0; printf("Your pressed key: %s\n", buf); write(port, buf, sizeof(char) * count); close(port); exit(0); } Embedded System Laboratory
Basic of Serial Communications 1 Configuring the Serial Port 2 Advanced Serial Programming Appendix B, ASCII Codes 3 5 Appendix A, RS-232 Pinouts 4 Contents Embedded System Laboratory
Advanced Serial Programming • Routines: • Usage: • The ioctl() function performs a variety of control functions on STREAMS devices. For non-STREAMS devices, the functions performed by this call are unspecified. The request argument and an optional third argument (with varying type) are passed to and interpreted by the appropriate part of the STREAM associated with fildes. • The fcntl() function provides for control over open files. The fildes argument is a file descriptor. Ioctl (fildes, request, ...) fcntl (fildes, cmd, ...) Embedded System Laboratory
Advanced Serial Programming Embedded System Laboratory
Example Advance Serial Programming • Practice: vi interface.c #include <termios.h> #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/signal.h> #include <sys/types.h> #define BAUDRATE B38400 #define FALSE 0 #define TRUE 1 volatile int STOP=FALSE; void signal_handler_IO (int status); intwait_flag=TRUE; FILE *input; FILE *output; int status; Embedded System Laboratory
Example Advance Serial Programming • Practice: (Cont.) main() { char Param_strings[7][80]; char message[90]; intfd, tty, c, res, i, error; char In1, Key; structtermiosoldtio, newtio; structtermiosoldkey, newkey; structsigactionsaio; char buf[255]; input = fopen("/dev/tty", "r"); output = fopen("/dev/tty", "w"); Embedded System Laboratory
Example Advance Serial Programming • Practice: (Cont.) tty= open("/dev/tty", O_RDWR | O_NOCTTY | O_NONBLOCK); tcgetattr(tty,&oldkey); newkey.c_cflag= BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; newkey.c_iflag= IGNPAR; newkey.c_oflag= 0; newkey.c_lflag= 0; //ICANON; newkey.c_cc[VMIN] = 1; newkey.c_cc[VTIME] = 0; tcflush(tty, TCIFLUSH); tcsetattr(tty,TCSANOW,&newkey); fd= open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK); Embedded System Laboratory
Example Advance Serial Programming • Practice: (Cont.) //install the serial handler before making the device asynchronous saio.sa_handler= signal_handler_IO; sigemptyset(&saio.sa_mask); //saio.sa_mask = 0; saio.sa_flags= 0; saio.sa_restorer= NULL; sigaction(SIGIO,&saio,NULL); // allow the process to receive SIGIO fcntl(fd, F_SETOWN, getpid()); // Make the file descriptor asynchronous (the manual page says only // O_APPEND and O_NONBLOCK, will work with F_SETFL...) fcntl(fd, F_SETFL, FASYNC); tcgetattr(fd, &oldtio); // save current port settings // set new port settings for canonical input processing Embedded System Laboratory
Example Advance Serial Programming • Practice: (Cont.) newtio.c_cflag = B9600 | CRTSCTS | CS8 | 0 | 0 | 0 | CLOCAL | CREAD; newtio.c_iflag= IGNPAR; newtio.c_oflag= 0; newtio.c_lflag= 0; //ICANON; newtio.c_cc[VMIN]=1; newtio.c_cc[VTIME]=0; tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); while (STOP==FALSE) { status = fread(&Key,1,1,input); if (status==1) //if a key was hit { Embedded System Laboratory
Example Advance Serial Programming • Practice: (Cont.) switch (Key) { case 0x1b: /* Esc */ STOP=TRUE; break; default: fputc((int) Key,output); write(fd,&Key,1); break; } } if (wait_flag==FALSE) { res = read(fd,buf,255); Embedded System Laboratory
Example Advance Serial Programming • Practice: (Cont.) if (res>0) { printf("%s\n", buf); } wait_flag = TRUE; } } tcsetattr(fd,TCSANOW,&oldtio); tcsetattr(tty,TCSANOW,&oldkey); close(tty); close(fd); Embedded System Laboratory
Example Advance Serial Programming • Practice: (Cont.) fclose(input); fclose(output); } void signal_handler_IO (int status) { printf("received SIGIO signal.\r\n"); wait_flag= FALSE; } Embedded System Laboratory
Basic of Serial Communications 1 Configuring the Serial Port 2 Advanced Serial Programming Appendix B, ASCII Codes 3 5 Appendix A, RS-232 Pinouts 4 Contents Embedded System Laboratory
Appendix A, RS-232 Pinouts • RS-232 Pinouts • RS-232 uses a 9-pin D-Sub connector Embedded System Laboratory
Appendix A, RS-232 Pinouts • RS-232 Pinouts • RS-232 comes in three flavors (A, B, C) and uses a 25-pin D-Sub connector: Embedded System Laboratory
Appendix A, RS-232 Pinouts • RS-422 Pinouts • RS-422 also uses a 25-pin D-Sub connector, but with differential signals: Embedded System Laboratory
Basic of Serial Communications 1 Configuring the Serial Port 2 Advanced Serial Programming Appendix B, ASCII Codes 3 5 Appendix A, RS-232 Pinouts 4 Contents Embedded System Laboratory
Appendix B, ASCII Codes Embedded System Laboratory
Appendix B, ASCII Codes (Cont.) Embedded System Laboratory