ihapenciuc
Posts: 6
Joined: Fri Jul 06, 2012 2:07 pm

serial communication

Wed Aug 01, 2012 7:15 pm

Dear Raspberry users,

I m trying to read an write on a serial port (/dev/ttyUSB0 created by Prolific in Debain wheezy beta).
I have the flowing code in one .c file I compile using gcc:

Code: Select all

#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>


int set_interface_attribs (int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                printf ("error %d from tcgetattr \n");
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // ignore break signal
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                printf ("error %d from tcsetattr \n");
                return -1;
        }
        return 0;
}

void set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                printf ("error %d from tggetattr \n");
                return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                printf ("error %d setting term attributes \n");
}

int main(void)
{
	int fd;
	char *portname = "/dev/ttyUSB0";
	char buf[100];
	int n, i;


	fd	= open (portname, O_RDWR | O_NOCTTY | O_SYNC);

	if (fd < 0)
	{
        printf ("error %d opening %s \n", portname);
        return;
	}
	else
	{
	printf("open port %s \n", portname);
	}

	set_interface_attribs (fd, B4800, 0);  // set speed to 115,200 bps, 8n1 (no parity)
	printf("set port\n");
	
	set_blocking (fd, 0);                // set no blocking
	printf("set bloking\n");
	
	write (fd, "hello!\n", 7);           // send 7 character greeting
	printf("did write\n");
	sleep(5);
	n = read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read
	printf("did read %s\n", n);
    for(i=0;i<n;i++)
	{
	printf("%s",buf[i]);
	}
	printf("\n");
	close(fd);
return (0);
}
it is getting complied ok (some warning regarding memset) but since I am new with Linux I try not to care much since the executable is obtained.

Now if I run the ./serial_out (as is the name of my app) and I send nothing from PC I get on terminal:

Code: Select all

pi@raspberrypi:~/cam2bmp$ ./serial_out 
open port /dev/ttyUSB0 
set port
set bloking
did write
did read (null)
Great until now.

but if I send some characters from pc to raspberry (even 2) I get:

Code: Select all

pi@raspberrypi:~/cam2bmp$ ./serial_out 
open port /dev/ttyUSB0 
set port
set bloking
did write
Segmentation fault
Can anyone tell me what I am doing wrong?

Please!

Andrei

User avatar
Jim JKla
Posts: 2218
Joined: Sun Jan 29, 2012 11:15 pm
Location: Newcastle upon Tyne UK

Re: serial communication

Wed Aug 01, 2012 7:19 pm

could it be that

Code: Select all

set bloking
should be

Code: Select all

set blocking
and it's a typo just a guess I could well be wrong ;)
Noob is not derogatory the noob is just the lower end of the noob--geek spectrum being a noob is just your first step towards being an uber-geek ;)

If you find a solution please post it in the wiki the forum dies too quick

ihapenciuc
Posts: 6
Joined: Fri Jul 06, 2012 2:07 pm

Re: serial communication

Wed Aug 01, 2012 7:56 pm

Very funny :)

Now really... what is wrong with the code (not spelling)

muzzdeni
Posts: 1
Joined: Sun Aug 05, 2012 3:48 am

Re: serial communication

Sun Aug 05, 2012 3:50 am

In you main section.

n = read (fd, buf, sizeof buf); // read up to 100 characters if ready to read

should it not be n = read( fd, &buf, sizeof(buf));

ihapenciuc
Posts: 6
Joined: Fri Jul 06, 2012 2:07 pm

Re: serial communication

Mon Aug 06, 2012 11:33 am

Thank you muzzdeni, the " &buf " was the key. silly me.

Return to “Beginners”

Who is online

Users browsing this forum: gabriel.rosseel, rpdom and 56 guests