ARM9 Linux Serial Port Access - WriteSerial


This example writes a text string to serial port of ARM9. The program is written using EABI GCC Cross C Compiler (http://www.codesourcery.com). OpenPort(), ReadPort(), WritePort(), GetBaud() auxiliary functions have been implemented by BiPOM in comm.c source file to simplify port access. Usage: WriteSerial Examples: Write the string "Sample Text" to serial port /dev/ttyUSB0 at 9600 baud, 7 data bits, even parity, 1 stop bit: WriteSerial /dev/ttyUSB0 9600 7E1 "Sample Text" Write the string "Sample Text" to serial port /dev/ttyUSB1 at 4800 baud, 8 data bits, no parity, 1 stop bit: WriteSerial /dev/ttyUSB1 4800 8N1 "Sample Text" Write the string "Sample Text" to serial port /dev/ttyS1 at 115200 baud, 7 data bits, odd parity, 2 stop bits: WriteSerial /dev/ttyS1 115200 7O2 "Sample Text" Typically /dev/ttyUSB0, /dev/ttyUSB1, etc. are USB to COM converter serial devices. /dev/ttyS1, etc. are native COM ports.

Toolkit:ARM Development System

Location:\bipom\devtools\GCC\AT91SAM9\Examples\Linux\WriteSerial\comm.c

Code Example


	// initial setup

	int hPort = OpenPort(Port.c_str(), Baud, DataBits, StopBits, Parity);

	if(hPort < 0)
	{
		printf("\r\nFailed open port: '%s'", Port.c_str());
		return 1;
	}
	
	// write data to port

	int n = WritePort(hPort, (char*)Text.c_str(), Text.length(), 1);
	
	if(n != Text.length())
	{
		printf("\r\nCannot send data to port (n=%d)", n);
		return 1;
	}
	
	printf("\r\nData was sent successfully");
	
	// close port

	close(hPort);