Connecting PIC16F877A using Easypic5 via RS232 with Linux


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Connecting PIC16F877A using Easypic5 via RS232 with Linux
# 1  
Old 01-26-2012
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:

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?
# 2  
Old 01-26-2012
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Hardware

Bluetooth Dongle Pairing but Not Connecting (Linux)

I have been trying to get bluetooth working correctly on Linux for a while now. I am using two systems that are having identical issues: Linux Mint 15 64-bit & Ubuntu 13.04 32-bit. Both are using the Cinnamon desktop. I have the following bluetooth dongle: ... (6 Replies)
Discussion started by: Deluge
6 Replies

2. Red Hat

CVS repository connecting error in Linux 5.3

Hi Friends, I configured CVS Repository in RHEL 5.3 Server. But connecting the CVS Repository get the error like. # cvs -d:pserver:cvs@localhost:/backup/cvs/cvsrep login Logging in to :pserver:cvs@localhost:2401/backup/cvs/cvsrep CVS password: cvs : connect to :2401 failed: Connection... (0 Replies)
Discussion started by: srinivas814
0 Replies

3. UNIX for Advanced & Expert Users

Help connecting linux box to Windows - winbindd problem

Hi experts - I hope you can help me. I am trying to resolve Windows host names (aka Netbios names, aka "UNC names) from a Linux box. I have added "dns wins" to the "hosts" line at /etc/nsswitch.conf, and installed samba 3.2.2 and ran "winbindd -D". Now, when I go: "wbinfo -N venus" (where... (1 Reply)
Discussion started by: maryg
1 Replies

4. UNIX for Dummies Questions & Answers

Help connecting to linux server from shh client in windows!

I just installed the latest version of unbuntu server and want to connect from windows using a ssh client. This is my first linux server, so bare with me =) For my server... cat /etc/hostsgives me 127.0.0.1 localhost 127.0.1.1 ubuntuHomeI tried using ssh secure shell and putty in vista.... (11 Replies)
Discussion started by: Bandit390
11 Replies

5. UNIX for Advanced & Expert Users

connecting UNIX/Linux to NTP server

Hello i want to connect my solaris & Linux boxes to ntp server i used the command /usr/sbin/ntpdate -s -b -p 8 -u <NTP-IP> and added the NTP server as server in /etc/ntp.conf please help in completing the process and verifying it (1 Reply)
Discussion started by: learn82
1 Replies

6. Solaris

rs232 pci-card

hello mates, do you know a manufactor for rs232 serial pci cards for the sparc architecture? it seems that there is no original product from sun for that purpose. strange, isn't it? problem is that i have a V250 with only one db9 rs232 serial port, and i need it for a modem connection, but i also... (1 Reply)
Discussion started by: pressy
1 Replies

7. UNIX for Dummies Questions & Answers

Connecting 2 linux boxes with COM

hi there! I have a small, probably very dumb question. Is it possible to connect two linux boxes with each other over COM ports for terminal connection? (4 Replies)
Discussion started by: D-Lexy
4 Replies

8. UNIX for Advanced & Expert Users

VNC via Rs232

Hi ! Is there somebody who is able to connect a VNC session via serial cable ? There is a hint on the VNC homepage that it is possible to configure that. But how? thanks joerg (2 Replies)
Discussion started by: joerg
2 Replies

9. Programming

RS232 communication optimisation

Need help for RS232 communication with switches in order to optimise the data traffic. How to manipulate different flags in termios struct to do the same? (1 Reply)
Discussion started by: ManishSaxena
1 Replies

10. UNIX for Dummies Questions & Answers

Problem Connecting from Windows base OS to linux

I have a network consisting of Linux Server, Win-Nt & 95 OS. I am able to connect from Linux to Win-NT using either ftp / telnet in time. But it takes me around & more than 50 seconds to connect from Wint -Nt/95 to Linux using either ftp/telnet. My network is small & it hardly consists 20-25... (3 Replies)
Discussion started by: S.Vishwanath
3 Replies
Login or Register to Ask a Question