RS 232, CentOS 5.6, gcc 3.4.9


 
Thread Tools Search this Thread
Top Forums Programming RS 232, CentOS 5.6, gcc 3.4.9
# 1  
Old 06-24-2011
RS 232, CentOS 5.6, gcc 3.4.9

Hello!
Can somebody help me with one problem. I write driver for I/o from COM port.
That's the main code:

Code:
#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitionss
#include <time.h>   // time calls
#include <sys/ioctl.h>
#include <linux/serial.h>			// Здесь описана serial_icount_struct

int	fd;
int open_port(void)
{
int	iret;
int	fl_flags;

	 
	fd = open("/dev/ttyS0", O_NONBLOCK | O_RDWR);
 
	if(fd == -1) // if open is unsucessful
	{
		
		printf("open_port: Unable to open /dev/ttyS0. \n");
		return -1;
	}
	else
	{
		fl_flags = fcntl(fd,F_GETFL);
		iret = fcntl(fd, F_SETFL, FNDELAY);
//		iret = fcntl(fd, F_SETFL, fl_flags & ~O_NONBLOCK);
		if (iret)
			printf("Error fcntl %s\n", strerror(errno));
		printf("port is open.\n");

	// Переведем устройство в exclusive mode
	iret = ioctl(fd, TIOCEXCL, 0);
	if (iret < 0) {
		return -1;
	};

		return 0;
	}

}

int configure_port()
{
int	iret;
	struct termios port_settings;      // structure to store the port settings in
 
	iret = tcgetattr(fd, &port_settings);
	if (iret) 
		printf("Error tcgetattr %s\n", strerror(errno));
	cfsetispeed(&port_settings, B110);   // set baud rates
	cfsetospeed(&port_settings, B110);
 
/*	cfsetispeed(&port_settings, B9600);    // set baud rates
	cfsetospeed(&port_settings, B9600);

    	port_settings.c_cflag     &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
    	port_settings.c_cflag     |= CS8 | CREAD | HUPCL;

    	port_settings.c_iflag      = IGNBRK | IGNPAR;
    	port_settings.c_oflag      = 0;
    	port_settings.c_lflag      = 0;
    	port_settings.c_cc[VMIN]   = 1;
    	port_settings.c_cc[VTIME]  = 10;
*/
	port_settings.c_iflag      = IGNBRK | IGNPAR;
	port_settings.c_oflag      = 0;
	port_settings.c_lflag      = 0;
	port_settings.c_cc[VMIN]   = 1;
	port_settings.c_cc[VTIME]  = 10;


	port_settings.c_cflag &= ~PARENB;    // set no parity, stop bits, data bits

	port_settings.c_cflag &= ~CSTOPB;

	port_settings.c_cflag &= ~CSIZE;

	port_settings.c_cflag |= CS8;
	port_settings.c_cflag |= (CLOCAL | CREAD); 

	iret = tcsetattr(fd, TCSANOW, &port_settings);    // apply the settings to the port

	return 0;
}

int read_write()
{


	unsigned char send_bytes[] = {'h','e','l','l','o'};
	char buffer[5];

	int n_write = write(fd, send_bytes, 5);  //Send data
	printf("Wrote the bytes. \n");
	usleep(10000000);
	int n_read=read(fd,buffer,1);
	if (n_read == -1)
		printf("Error read %s\n", strerror(errno));

	printf("read %d bytes. \n", n_read);
//	bufptr=buffer;
//	while((n_read=read(fd, bufptr, buffer+sizeof(buffer)-bufptr-1))>0)	
//{
//	bufptr+=n_read;
//}	
printf(buffer); 

	return 0;

 

}

int main(void)
{
	open_port();

	configure_port();

	read_write();

 	close(fd);

	return(0);

 

}

The problem is - when I return n_read, I get -1 and the error is - Port is temporarily unavaible...
SmilieSmilieSmilie

Moderator's Comments:
Mod Comment Please use [CODE] tags when posting source listings, console output, ...
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Red Hat

How to Upgrade Centos 5.7 using Centos 5.8 ISO image on Vmware workstation

Dear Linux Experts, On my windows 7 desktop with the help of Vmware workstation (Version 7.1), created virtual machine and installed Centos 5.7 successfully using ISO image. Query : Is this possible to upgrade the Centos 5.7 using Centos 5.8 ISO image to Centos version 5.8?.. if yes kindly... (2 Replies)
Discussion started by: Ananthcn
2 Replies

2. AIX

Console Setup for RS-232 port on 9131-52A

Hi, I have tried to setup the console on a Power5 9131-52A type IBM Server. On the back there are 2 RS-232 ports labeled T1 T2. I am going under the assuming that these are the serial ports (Normally the serial ports are S1 S0). When I finish assigning the console under smit, I get no response... (3 Replies)
Discussion started by: mrmurdock
3 Replies

3. Solaris

Installing gcc - recieve error message gcc : cannot execute

AIM- Install Oracle 11g on Solaris using VMWare Steps 1.Logged on as root 2.Created subfolders /usr/local/bin & /usr/local/bin/gcc 3.Downloaded gcc & libiconv & unzipped them on my harddrive & burnt them on CD 4.Copied files from CD to /usr/local/bin/gcc 5.Terminal (root) pkgadd -d... (8 Replies)
Discussion started by: Ackers
8 Replies

4. Programming

RS-232 C++ Programming

First off, the target system is FreeBSD 6.x on an old AMD K6 333. I know C++ but I wouldn't consider myself a programmer. (Haven't written a line since college.) Anyway, I want to write a program to allow me to pan and tilt a webcam or to simply send a signal from a serial port to a relay switch... (8 Replies)
Discussion started by: Alpha_Harblo
8 Replies
Login or Register to Ask a Question

Featured Tech Videos