Using the serial ports

FOX Board G20 has 7 asynchronous serial interfaces identified in Linux with the device name from /dev/ttyS0 to /dev/ttyS6.

The picture below illustrates where the transmit and receive lines are located:

Please note that the electrical levels are at 3.3 volt. This means that is not possible to wire these lines directly to any standard RS232 devices but just with asynchronous ports on chips and modules that works at 3.3 volt. In any other case is requested a signal adapter circuitry from 3.3 volt to the standard +-12 volt.

Serial ports description

/dev/ttyS0 (DBGU)

This port is reserved by default as Linux system console port and is available on the Debug Port pin strip (see DPI product description).

Port /dev/ttyS0

/dev/ttyS1 (USART0)

This is the only port where all the handshaking signals requested by a modem link are available.

Port /dev/ttyS1

/dev/ttyS2 (USART1)

Port /dev/ttyS2

/dev/ttyS3 (USART2)

The RXD and TXD signals of this port are available both on J7 and J16 terminal. J16 is the terminal to weld a connector compatible with (4D systems oLed displays).

Port /dev/ttyS3

/dev/ttyS4 (USART3)

Port /dev/ttyS4

/dev/ttyS5 (USART4)

Port /dev/ttyS5

/dev/ttyS6 (USART5)

Port /dev/ttyS6

Quick test

To quickly test the serial ports it's installed by default the terminal emulator utility Minicom. Run it from the shell promp typing:

# mincom -s

-s parm runs minicom in setup mode.

Select Serial port setup and type <enter>:

Now type <A> to change the serial port and <F> to disable the hardware flow control. Select Exit from the configuration menu. Now each character press on your keyboard is sent to the selected port and each character received from the serial port is print on the screen.

Type <CTRL-A> and <Z> for more help on Minicom.

Python example

This is a basic example on how to use a serial port in Pyhon using the pySerial module (http://pyserial.sourceforge.net/index.html):

import serial
 
ser = serial.Serial(
	port='/dev/ttyS1', 
	baudrate=9600, 
	timeout=1,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
	bytesize=serial.EIGHTBITS
)  
 
ser.write("A")		# Send a "A" char to the serial port
s = ser.read(1)         # Wait for a char
print s
ser.close()

C example

This is a basic example on how to use a serial port in C.

Refer to these links to understand how it works.

#include <stdio.h>     
#include <string.h>    
#include <unistd.h>    
#include <fcntl.h>     
#include <errno.h>     
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdarg.h>
#include <termios.h>
 
int main(void) {
	char txBuffer[10];
	char rxBuffer[10];
	int fd;
	struct termios tty_attributes;
 
	if ((fd = open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NONBLOCK))<0) {
		fprintf (stderr,"Open error on %s\n", strerror(errno));
		exit(EXIT_FAILURE);
	} else {
		tcgetattr(fd,&tty_attributes);
 
		// c_cflag
		// Enable receiver
		tty_attributes.c_cflag |= CREAD;		
 
		// 8 data bit
		tty_attributes.c_cflag |= CS8;			
 
		// c_iflag
		// Ignore framing errors and parity errors. 
		tty_attributes.c_iflag |= IGNPAR;		
 
		// c_lflag
		// DISABLE canonical mode.
		// Disables the special characters EOF, EOL, EOL2, 
		// ERASE, KILL, LNEXT, REPRINT, STATUS, and WERASE, and buffers by lines.
 
		// DISABLE this: Echo input characters.
		tty_attributes.c_lflag &= ~(ICANON); 	 
 
		tty_attributes.c_lflag &= ~(ECHO);		
 
		// DISABLE this: If ICANON is also set, the ERASE character erases the preceding input 	
		// character, and WERASE erases the preceding word.
		tty_attributes.c_lflag &= ~(ECHOE);		
 
		// DISABLE this: When any of the characters INTR, QUIT, SUSP, or DSUSP are received, generate the corresponding signal.	
		tty_attributes.c_lflag &= ~(ISIG);		
 
		// Minimum number of characters for non-canonical read.
		tty_attributes.c_cc[VMIN]=1;			
 
		// Timeout in deciseconds for non-canonical read.
		tty_attributes.c_cc[VTIME]=0;			
 
		// Set the baud rate
		cfsetospeed(&tty_attributes,B9600);		
		cfsetispeed(&tty_attributes,B9600);
 
		tcsetattr(fd, TCSANOW, &tty_attributes);
 
		txBuffer[0]='A';
		write(fd,txBuffer,1);
 
		// Read a char
		if (read(fd,&rxBuffer,1)==1) {
			printf("%c",rxBuffer[0]);
			printf("\n");
		}	
	}	
 
	close(fd);	
	return EXIT_SUCCESS;
}
 
tutorial/serial_port.txt · Last modified: 2010/03/02 14:32 by tanzox
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki