LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Serial Port

[ILUG] Serial Port

linux user linux.user at ireland.com
Tue Apr 11 12:26:26 IST 2000


all the declarations do at the top.

1) if you want to be able to write to the port
get "root" to change the permissions on the port
(if you not accessing the port as root)
carefull though if the buffer of the serial port isn't massive
so be carefull how much you write at the one time.

2)
int serial_port_fd = 0;

serial_port_fd = open("/dev/ttyS0", O_RDWR | O_NONBLOCK);
/dev/ttyS0 is the 1st serial port com 1 in windows.
its being opent in read write mode and non_blocking.
(the latter can be hand if you want to ckeck if there's something to read from the port with out being blocked).

if(serial_port_fd == -1)
 /* do error handeling */


3)
struct termios port_settings; /* do a man for the include file */

tcgetattr(serial_port_fd, &port_settings);
this system call will get you the current settings on the port 
at boot time the should be 

	#define	TTYDEF_IFLAG	(BRKINT | ISTRIP | ICRNL | IMAXBEL | IXON | IXANY)
	#define TTYDEF_OFLAG	(OPOST | ONLCR | XTABS)
	#define TTYDEF_LFLAG	(ECHO | ICANON | ISIG | IEXTEN | ECHOE|ECHOKE|ECHOCTL)
	#define TTYDEF_CFLAG	(CREAD | CS7 | PARENB | HUPCL) 

your probably want to change these.
/****************************************************************
Advanced programming in the UNIX environment(1992) by W. Richard 
Stevens. devotes a whole chapter to this and explains all the flags (theres about 60 of them)
*****************************************************************/
/* this command removes the ICANON  and  ISIG flags */
      port_settings.c_lflag &= ~(ICANON | ISIG); 
      port_settings.c_cflag &= ~(PARENB | CSIZE);  /* disable parity checking */
      

/* having removed the byte size "CSIZE" in the line above */
      /* change byte size from default 7 */ 
      port_settings.c_cflag |= CS8;      /* to 8 bits per bytes */

      /* set baud rates in and out to 9600 */
      cfsetospeed(&port_settings, B9600);
      cfsetispeed(&port_settings, B9600); 


tcsetattr(serial_port_fd, TCSANOW, &port_settings);


you should check the return values of both tcgetattr tcsetattr.


the seetings above are dependant on the device thats connected to your serial port.

if all the above has worked your ready to use you read & write system calls.

you could use something like 
unsigned char incoming_msg[255];
unsigned char outgoing_msg[255];

bzero(outgoing_msg,255); bzero will initialise your buffers for you is it's string.h  I think.

int bytes_read = 0;
int bytes_written = 0;

bytes_read = read(serial_port_fd,incoming_msg,255);

bytes_read may be less than 255.
if you decided to set up the port as non blocking then bytes_read will be zero.

like wise to write stuff out you could use

char a_string[15] = "today is april ";
int date = 11;
sprintf(outgoing_msg,"%s %d",a_string,date);
 
bytes_written = write(serial_port_fd,outgoing_msg,255);

outgoing_msg isn't 255 long but is padded with zeros;








 Where do you want to go today ?    Somewhere stable - away from Windows. 
_____________________________________

Get your free E-mail at http://www.ireland.com




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell