Connecting PIC16F877A using Easypic5 via RS232 with Linux

I searched the forums for RS232 issues, but all of the results don't help me solving my problem, so I had to open new thread.

This is the problem:

I have PIC16F877A micro-controller and I'm programming it with Easypic5(the PIC programming device(board) from MikroElektronika) and I want to connect the EasyPic5 which has integrated RS232 port with my desktop computer that runs Ubuntu 11.10.

I don't need help with the code that runs the PIC micro-controller, I need help with C code that runs on the Linux computer in order to communicate with the PIC.

I made a simple program that runs on the PIC and send the char 'A' through the RS232 Port. What I want is receive that character in my own program which runs on the computer written in C. I tested my PIC program using CuteCom (a software for Linux that works with parallel ports, alternative to Windows utility Hyperterminal) and it works fine. So I concluded that the code in the PIC is fine. But when I want to receive the character in my own program I can't read nothing.

So, this is the workflow of the code:

  1. Open the port /dev/ttyS0
  2. Configure the termios structure and apply it to /dev/ttyS0
  3. Read from that port

What I find difficult is 2nd step, configuring the termios structure for the port. So everytime I run the code, the read() command return -1 indicating an error.

What's interesting, whenever I run the code and CuteCom utility together, my code works!

I guess that running CuteCom sets other (the right ones) values for the termios structure and my program can read from the port.
The only thing that I set from the program that runs in PIC and is included in the termios structure is baudrate:2400.

Here is the code:



#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
int main()
{
	struct termios old, new;
	int fd,procitani;
	char vlez[3]={0};

	fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY );
	if (fd<0)
	{
		printf("error");
		return (-1);
	}
	tcgetattr(fd,&old);
  bzero(&new,sizeof(new));

	new.c_cflag=B2400 | CRTSCTS | CS8 | CLOCAL | CREAD;
	new.c_iflag=IGNPAR | ICRNL;
	new.c_oflag=0;
	new.c_lflag=ICANON;

	new.c_cc[VINTR]=0;
	new.c_cc[VQUIT]=0;
	new.c_cc[VERASE]=0;
	new.c_cc[VKILL]=0;
	new.c_cc[VEOF]=4;
	new.c_cc[VTIME]=0;
	new.c_cc[VMIN]=1;
	new.c_cc[VSWTC]=0;
	new.c_cc[VSTART]=0;
	new.c_cc[VSTOP]=0;
	new.c_cc[VSUSP]=0;
	new.c_cc[VEOL]=0;
	new.c_cc[VREPRINT]=0;
	new.c_cc[VDISCARD]=0;
	new.c_cc[VWERASE]=0;
	new.c_cc[VLNEXT]=0;
	new.c_cc[VEOL2]=0;

	tcflush(fd,TCIFLUSH);
	tcsetattr(fd,TCSANOW,&new);

	for(;1;)
	{
		fflush(stdout);
		procitani=read(fd, vlez,1);
		if(procitani<0)
		{
		printf("read: %d ",procitani);
			break;
		}
		vlez[procitani]=0;

		printf("%s",vlez);
		fflush(stdout);
	}
close(fd);

	return 0;
}

---------- Post updated at 06:35 PM ---------- Previous update was at 01:20 PM ----------

Anyone?

We are not "on call" here. If you don't get a reply instantly, wait!

Let me hunt up the code I use...

---------- Post updated at 11:48 AM ---------- Previous update was at 11:44 AM ----------

Try perror("read failed"); to see why read failed.

I rolled my own serial-port-opening function to make ordinary rs232 communication less of a hassle, with the result being tcgets.h and tcgets.c. You can open a serial port like int fd=termios_open("/dev/ttyS0", "2400N81"); . Even if you don't use it, it should hopefully be a good example