MeiOokami
Posts: 9
Joined: Tue Mar 05, 2013 8:43 pm

UART Issues

Tue Mar 05, 2013 9:12 pm

So I've been working off some examples on here and was trying to send out a simple repeating line on the uart of the pi.
My code so far:

Code: Select all

 
 #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main(int argc, char ** argv) {
  char buf[256]={},c;
  int fd,n,i;
  // Open the Port. We want read/write, no "controlling tty" status, and open it no matter what state DCD is in
  fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("open_port: Unable to open /dev/ttyAMA0 - ");
    return(-1);
  }

struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
tcsetattr(fd, TCSANOW, &options);

// Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
  fcntl(fd, F_SETFL, 0);

  // Write to the port
while (1)
{
   n = write(fd,"Hello!",6);
  if (n < 0) {
    perror("Write failed - ");
    return -1;
  }
}

  // Read up to 255 characters from the port if they are there
i = 0;
do
{
   n = read(fd, (void*)&c, 1);
  if (n > 0)
  {
    buf[i++] = c;
  }
}
while (c != '\r' && i < 255 && n > 0);
buf[i] = '\0';

if (n < 0) {
    perror("Read failed - ");
    return -1;
} else if (i == 0)
  printf("No data on port\n");
else {
    printf("%d bytes read : %s", n, buf);
}

  // Don't forget to clean up
  close(fd);
  return 0;
}

I'm using teraterm to check what it is out putting set to 9600 with 1 stop bit and no parity. but instead of the [Hello!] I keep getting... well garbage with roughly the same number of characters... so it's something close to working. This code does compile with no warnings.

I'm a bit stumped as I even tried another UART setup code and got the same output.

screen shot:
https://www.dropbox.com/s/kojrdc2gnqodndf/Error.png?m

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: UART Issues

Tue Mar 05, 2013 11:33 pm

MeiOokami wrote:So I've been working off some examples on here and was trying to send out a simple repeating line on the uart of the pi.
My code so far:

Code: Select all

 
 #include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main(int argc, char ** argv) {
  char buf[256]={},c;
  int fd,n,i;
  // Open the Port. We want read/write, no "controlling tty" status, and open it no matter what state DCD is in
  fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("open_port: Unable to open /dev/ttyAMA0 - ");
    return(-1);
  }

struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
tcsetattr(fd, TCSANOW, &options);

// Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
  fcntl(fd, F_SETFL, 0);

  // Write to the port
while (1)
{
   n = write(fd,"Hello!",6);
  if (n < 0) {
    perror("Write failed - ");
    return -1;
  }
}

  // Read up to 255 characters from the port if they are there
i = 0;
do
{
   n = read(fd, (void*)&c, 1);
  if (n > 0)
  {
    buf[i++] = c;
  }
}
while (c != '\r' && i < 255 && n > 0);
buf[i] = '\0';

if (n < 0) {
    perror("Read failed - ");
    return -1;
} else if (i == 0)
  printf("No data on port\n");
else {
    printf("%d bytes read : %s", n, buf);
}

  // Don't forget to clean up
  close(fd);
  return 0;
}

I'm using teraterm to check what it is out putting set to 9600 with 1 stop bit and no parity. but instead of the [Hello!] I keep getting... well garbage with roughly the same number of characters... so it's something close to working. This code does compile with no warnings.

I'm a bit stumped as I even tried another UART setup code and got the same output.

screen shot:
https://www.dropbox.com/s/kojrdc2gnqodndf/Error.png?m
I'm guessing you tried the wiringSerial library too (from seeing -lwiringPi on your build script). Have you checked the settings of terraterm though?

I'd start by running minicom on the Pi to terraterm on your PC just to be paranoid and make sure that they're both talking the same...

What I also found is that it's best to never assume what a serial port might be set to by default - so always set/clear as many bits/options as possible - which is what I do in the serialOpen() code

-Gordon
--
Gordons projects: https://projects.drogon.net/

MeiOokami
Posts: 9
Joined: Tue Mar 05, 2013 8:43 pm

Re: UART Issues

Wed Mar 06, 2013 1:04 am

I've been messing around with the teraterm options to match the expected output of the pi. With most options off. But I will have to try minicom tomorrow. Was hoping it would work without it.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: UART Issues

Wed Mar 06, 2013 10:46 am

MeiOokami wrote:I've been messing around with the teraterm options to match the expected output of the pi. With most options off. But I will have to try minicom tomorrow. Was hoping it would work without it.
Minicom is just the Linux equivalent of teraterm. Get those 2 talking to verify the link is working fine, then move to programs.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART Issues

Wed Mar 06, 2013 11:22 am

You are setting baud-rate, but you are not setting the number-of-bits or parity, both of which may cause the problem you are seeing. Number of stop-bits probably wouldn't, but you should set that too.

MeiOokami
Posts: 9
Joined: Tue Mar 05, 2013 8:43 pm

Re: UART Issues

Thu Mar 07, 2013 1:52 am

Minicom is just the Linux equivalent of teraterm. Get those 2 talking to verify the link is working fine, then move to programs.

-Gordon[/quote]

Minicom doesn't seem to work either: it gets a character, just the wrong one. tried different settings on teraterm and minicom with no luck...
still not seeing what's worng, so I hope it's just the adapter for serial to usb messing things up. going to try to use a known working uC to see what it is sending.

Edit: I just scoped the sent bits for an "H" for teraterm and from minicom... turns out the bit are inverted except for the stop and start bits. sorry for the trouble and thanks for your help.
Sadly it's the active low I need and not the active high the pi is out putting.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: UART Issues

Thu Mar 07, 2013 2:39 am

Are you connecting DIRECTLY between PC and PI with no buffers or RS232 type devices on PI end?

If you have wired directly from Pi to PC without some form of chip in the way you are asking for trouble and not surprised it is wrong

I have not seen any problems with getting correct active state on UART from Pi to connect to proper devices

Also my first serial test is always loop it back to check I see teh correct thing at each end first, before connecting to anything else to prove software
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

MeiOokami
Posts: 9
Joined: Tue Mar 05, 2013 8:43 pm

Re: UART Issues

Thu Mar 07, 2013 4:23 am

Yeah got it now. just needed a CMOS NOT Gate in between the two... well NAND with inputs tied but still.
Guess it was dumb to think it would connect so easily, just my first run in with UART worked better without the interfacing chip.

Thanks for the support anyways.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: UART Issues

Thu Mar 07, 2013 7:36 am

You should not use a CMOS NOT gate; you should use a proper RS232 buffer. RS232 buffers always invert, so I'm a little worried that you seem to already have an inversion at the PC end. The Pi will not like the +/- 12V that is produced from a proper RS232 buffer; it's GPIO lines are unprotected and must not be exposed to voltages outside the range 0-3.3V.

MeiOokami
Posts: 9
Joined: Tue Mar 05, 2013 8:43 pm

Re: UART Issues

Thu Mar 07, 2013 8:16 am

Ah good point. Even if the output is 0 to 3.3v from the nand... Well at least I know it works, and will aquire the proper chip soon.

Bright Sparks NZ
Posts: 20
Joined: Fri Dec 21, 2012 8:11 pm

Re: UART Issues

Thu Mar 07, 2013 9:38 am

I made up a nifty little 'level shifter' inverter with just a pair of NPN transistors and 6x1k resistors. This comes in real handy for a variety of projects needing TTL / RS232 level shifting. A good idea to protect your Pi as well ! Proto board and scematic here...
https://picasaweb.google.com/picaxe/Ope ... dwareIdeas
~ Andrew

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: UART Issues

Thu Mar 07, 2013 1:54 pm

Consider yourself VERY lucky that the PC you used had a bad RS232 interface and did not blow your Pi GPIO RXD pin.

RS232 can be upto -12V and +12V.

If you had used another computer you probably would not have been so lucky.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

MeiOokami
Posts: 9
Joined: Tue Mar 05, 2013 8:43 pm

Re: UART Issues

Tue Mar 12, 2013 1:02 am

Yeah I was lucky I did not damage my UART RX pin :oops: . Well it works between minicom and teraterm so looks like it's alive still.

Now just to get the read function to read when I want it to.

MeiOokami
Posts: 9
Joined: Tue Mar 05, 2013 8:43 pm

well I did say "issues" in the title...

Thu Mar 14, 2013 6:30 pm

Hi,
Even after following the steps to free up the UART found here: http://www.irrational.net/2012/04/19/us ... rial-port/, when I try to run my program I get this error:

Code: Select all

open_port: Unable to open /dev/ttyAMA0 - : Permission denied
Now I know a quick "sudo chmod a+rwx /dev/ttyAMA0" will allow this program to run, I don't want to have to type that in every time i boot up the pi. As I don't want to have to do anything to the pi after the same program is setup to auto run on boot. But that's another problem that may be fixed by this one's solution.

Is there anyway to permanently set these permissions? and is anyone else running into this problem?

-Thanks again for your help.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: well I did say "issues" in the title...

Thu Mar 14, 2013 6:40 pm

MeiOokami wrote:Hi,
Even after following the steps to free up the UART found here: http://www.irrational.net/2012/04/19/us ... rial-port/, when I try to run my program I get this error:

Code: Select all

open_port: Unable to open /dev/ttyAMA0 - : Permission denied
Now I know a quick "sudo chmod a+rwx /dev/ttyAMA0" will allow this program to run, I don't want to have to type that in every time i boot up the pi. As I don't want to have to do anything to the pi after the same program is setup to auto run on boot. But that's another problem that may be fixed by this one's solution.

Is there anyway to permanently set these permissions? and is anyone else running into this problem?

-Thanks again for your help.
run:

sudo vigr

to edit the group file. Add pi to the 'dialout' group.

ie. it should then look like:

dialout:x:20:pi



-Gordon
--
Gordons projects: https://projects.drogon.net/

MeiOokami
Posts: 9
Joined: Tue Mar 05, 2013 8:43 pm

Re: UART Issues

Sat Mar 16, 2013 12:48 am

Ah, Yup that worked well. even solved a segmentation fault.

Thanks for the help.

Return to “C/C++”