Hi, want to use the header uart for comms to a pic, I have stopped the uart console comms by removing ttyAMA0 references from /boot/cmdline.txt and /etc/inittab and can get comms to my laptop terraterm with minicom on Rpi.
But how do I send data from a C program ?
Running Debian with Frank-buss w1-test update (fantastic) for spi (and I2c) which appear in /dev (after adding "I2C-dev " the /etc/modules file.
the example gpio and spi both cc ok and run in sudo mode (in gpio example changed gpio to 17 to 25 and delay(1) to udelay(10000) but im really a HW engineer and Gerts and bootc's air is in my lifebelt.
can I send data byte wise to the uart direct ? with a poke (showing my age)
also does devmem work in some window?
Many thanks .
working on accel /gyro/si1143/pic33e integration.
-
- Posts: 20
- Joined: Mon Jun 04, 2012 9:57 pm
Re: uart comms in C
Are you looking for something like the below? This is from a bootloader using the serial port, written by dwelch67. See also http://www.raspberrypi.org/phpBB3/viewt ... &start=100
https://github.com/dwelch67/raspberrypi ... loader02.c
unsigned int uart_recv ( void )
{
while(1)
{
if(GET32(AUX_MU_LSR_REG)&0x01) break;
}
return(GET32(AUX_MU_IO_REG)&0xFF);
}
//------------------------------------------------------------------------
void uart_send ( unsigned int c )
{
while(1)
{
if(GET32(AUX_MU_LSR_REG)&0x20) break;
}
PUT32(AUX_MU_IO_REG,c);
}
https://github.com/dwelch67/raspberrypi ... loader02.c
unsigned int uart_recv ( void )
{
while(1)
{
if(GET32(AUX_MU_LSR_REG)&0x01) break;
}
return(GET32(AUX_MU_IO_REG)&0xFF);
}
//------------------------------------------------------------------------
void uart_send ( unsigned int c )
{
while(1)
{
if(GET32(AUX_MU_LSR_REG)&0x20) break;
}
PUT32(AUX_MU_IO_REG,c);
}
Re: uart comms in C
^not this
http://tldp.org/HOWTO/Serial-Programming-HOWTO/ (basic overview)
http://tldp.org/HOWTO/Serial-HOWTO.html (some largely useless bits and pieces related specifically to uarts)
Simon
http://tldp.org/HOWTO/Serial-Programming-HOWTO/ (basic overview)
http://tldp.org/HOWTO/Serial-HOWTO.html (some largely useless bits and pieces related specifically to uarts)
Simon
-
- Posts: 20
- Joined: Mon Jun 04, 2012 9:57 pm
Re: uart comms in C
Thanks for these suggestions. The PUT32 gives an error and I cant find the source code for the function anywhere, I used the address from the bootloader code and tried outw(65,0x20215040); which is the AUX_MU_IO_REG .This gives a segmentation fault but at least it compiles.
The GPIO code looses me when it does a malloc and maps it ?
Is it possible to do a direct memory read/write from C or does linux get in the way and im just being stupid.
Thanks.
The GPIO code looses me when it does a malloc and maps it ?
Is it possible to do a direct memory read/write from C or does linux get in the way and im just being stupid.
Thanks.
Re: uart comms in C
You don't need to do direct memory reads and writes. You need to use the POSIX interface to the serial port.
Something like this (completely untested, typed directly into the browser) code should do it for you.
Something like this (completely untested, typed directly into the browser) code should do it for you.
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) {
int fd;
// 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);
}
// Turn off blocking for reads, use (fd, F_SETFL, FNDELAY) if you want that
fcntl(fd, F_SETFL, 0);
// Write to the port
int n = write("Hello Peripheral\n");
if (n < 0) {
perror("Write failed - ");
return -1;
}
// Read up to 255 characters from the port if they are there
char buf[256];
n = read(fd, (void*)buf, 255);
if (n < 0) {
perror("Read failed - ");
return -1;
} else if (n == 0) printf("No data on port\n");
else {
buf[n] = '\0';
printf("%i bytes read : %s", n, buf);
}
// Don't forget to clean up
close(fd);
return 0;
}
-
- Posts: 20
- Joined: Mon Jun 04, 2012 9:57 pm
Re: uart comms in C
Thanks tufty, thats worked ok , i just had to modify the
int n = write("Hello Peripheral\n");
to the format int write(int fd, char *Buff, int NumBytes);
int n = write(fd,"Hello",6);
my comms default is 9600baud
cheers.
int n = write("Hello Peripheral\n");
to the format int write(int fd, char *Buff, int NumBytes);
int n = write(fd,"Hello",6);
my comms default is 9600baud
cheers.
Re: uart comms in C
Oh, duh. Yeah. My bad.beautifulsmall wrote:Thanks tufty, thats worked ok , i just had to modify the
int n = write("Hello Peripheral\n");
to the format int write(int fd, char *Buff, int NumBytes);
int n = write(fd,"Hello",6);
my comms default is 9600baud
cheers.
If you want to set the baud rate, you need to get into meddling with termios. For example...
Code: Select all
struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
tcsetattr(fd, TCSANOW, &options);
-
- Posts: 20
- Joined: Mon Oct 08, 2012 5:14 am
- Location: Bangalore, India
UART communication in C
beautifulsmall wrote:Thanks tufty, thats worked ok , i just had to modify the
int n = write("Hello Peripheral\n");
to the format int write(int fd, char *Buff, int NumBytes);
int n = write(fd,"Hello",6);
my comms default is 9600baud
cheers.
Hi,
I am using the code given above [ with corrections]
I am able to send data from Pi to the terminal in the computer but I am not able to receive the data into PI.
The program stops after the read function is executed; initially I thought that its waiting for data but later i see that even after the data is sent from computer the program remain there, I am not able to understand what's the problem.
Can you please help me solve the same
Thanks & Regards
Sumanth
Sumanth
-
- Posts: 20
- Joined: Mon Oct 08, 2012 5:14 am
- Location: Bangalore, India
Re: uart comms in C
Just changed the function fcntl(fd, F_SETFL, FNDELAY) instead of fcntl(fd, F_SETFL, 0).
With this change the program did not wait for the data but error is "Read failed- : Resource temporarily unavailable".
Can someone help me solve this.
With this change the program did not wait for the data but error is "Read failed- : Resource temporarily unavailable".
Can someone help me solve this.
Thanks & Regards
Sumanth
Sumanth
Re: uart comms in C
When you say it stops, do you mean it hangs in place, or that the program exits? Are you getting any terminal output on the Pi? What terminal software are you using on the PC, and are you sure you're set up to not use flow control, 8N1 @ <baudrate>, and all that cack?
About the only thing that occurs to me software-wise is that it may be blocking on the read, in which case :
a - my memory as to how to make an fd nonblocking is flaky (looking back, the implementation above is certainly "iffy"). Try fcntl(fd, F_SETFL, O_NONBLOCK);
b - If it's blocking, you might get it to work by sending 256 characters or more down the serial port (enough to get it past the block).
c - you'd be able to see what's going on by running the program through gdb.
"c" above holds in all cases.
Simon
About the only thing that occurs to me software-wise is that it may be blocking on the read, in which case :
a - my memory as to how to make an fd nonblocking is flaky (looking back, the implementation above is certainly "iffy"). Try fcntl(fd, F_SETFL, O_NONBLOCK);
b - If it's blocking, you might get it to work by sending 256 characters or more down the serial port (enough to get it past the block).
c - you'd be able to see what's going on by running the program through gdb.
"c" above holds in all cases.
Simon
Re: uart comms in C
Actually, thinking about it a bit more, I'm being a tool, and my reading code is neither one thing nor the other.
Read the documents I linked to earlier, and you'll be able to work it out. Understanding what's going on with POSIX read() and write() isn't immediately obvious, but it's worth working through it yourself. Any reply I make to explain would, of necessity, be as long or longer than the howtos I linked above.
Simon
Read the documents I linked to earlier, and you'll be able to work it out. Understanding what's going on with POSIX read() and write() isn't immediately obvious, but it's worth working through it yourself. Any reply I make to explain would, of necessity, be as long or longer than the howtos I linked above.
Simon
Re: uart comms in C
You'll need to change the read a bit and make it into a loop. If the read is non-blocking, then it will immediately exit and say there are no characters available. So you could loop until you got characters, but that is not the most efficient way to do it.
Probably the best way is to keep it blocking, but only read one character at a time. When that character is CR (value 13... or maybe 10 in some circumstances) then you know you have a complete line of text.
(I haven't tested this either.)
Of course the if(i == 0) will never trigger, because the read will wait until there is something to read. However that is not always what you want to do. There's a good discussion about the alternatives here.
Probably the best way is to keep it blocking, but only read one character at a time. When that character is CR (value 13... or maybe 10 in some circumstances) then you know you have a complete line of text.
(I haven't tested this either.)
Code: Select all
i = 0;
do
{
char c;
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);
}
-
- Posts: 20
- Joined: Mon Oct 08, 2012 5:14 am
- Location: Bangalore, India
Re: uart comms in C
I get the error as "Read failed- : Resource temporarily unavailable"
The application does not wait for data now, I did follow the instruction given in the link -
http://www.irrational.net/2012/04/19/us ... rial-port/
even now the i get the same error.
The application does not wait for data now, I did follow the instruction given in the link -
http://www.irrational.net/2012/04/19/us ... rial-port/
even now the i get the same error.
Thanks & Regards
Sumanth
Sumanth
Re: uart comms in C
Yep, you're still getting EAGAIN, meaning that there's nothing available on the serial port at the time you're trying to read.
I'l say it again. Go read the documentation I linked to earlier, understand what's going on, and work it out for yourself. Don't rely on code snippets typed untested into the browser by people like me. When you've written your own code and it doesn't work, *then* come back for help.
I'l say it again. Go read the documentation I linked to earlier, understand what's going on, and work it out for yourself. Don't rely on code snippets typed untested into the browser by people like me. When you've written your own code and it doesn't work, *then* come back for help.
- [email protected]
- Posts: 2024
- Joined: Tue Feb 07, 2012 2:14 pm
- Location: Devon, UK
Re: UART communication in C
You might want to have a look at the serial library that's in wiringPi. It'll solve all these issues (well, it should!)sumanharapanahalli wrote:beautifulsmall wrote:Thanks tufty, thats worked ok , i just had to modify the
int n = write("Hello Peripheral\n");
to the format int write(int fd, char *Buff, int NumBytes);
int n = write(fd,"Hello",6);
my comms default is 9600baud
cheers.
Hi,
I am using the code given above [ with corrections]
I am able to send data from Pi to the terminal in the computer but I am not able to receive the data into PI.
The program stops after the read function is executed; initially I thought that its waiting for data but later i see that even after the data is sent from computer the program remain there, I am not able to understand what's the problem.
Can you please help me solve the same
https://projects.drogon.net/raspberry-p ... l-library/
-Gordon
--
Gordons projects: https://projects.drogon.net/
Gordons projects: https://projects.drogon.net/
Re: uart comms in C
this all wfm but...
it seems one needs to run minicom first, then quit else program hangs.
ie after reboot
why might this be?
gdb -e run produces no output in the case of hang.
it seems one needs to run minicom first, then quit else program hangs.
ie after reboot
why might this be?
gdb -e run produces no output in the case of hang.
Re: uart comms in C
I think I've provided the complete code to set up the serial port to rid all the useless console features of linux:
http://www.raspberrypi.org/phpBB3/viewt ... 82#p372182
This above code should go after you open the port and before you set its property with tcsetattr.
The first link provided by tufty gives you some sample code but without the complete set of parameters on my post it will be useless to you.
http://www.raspberrypi.org/phpBB3/viewt ... 82#p372182
This above code should go after you open the port and before you set its property with tcsetattr.
The first link provided by tufty gives you some sample code but without the complete set of parameters on my post it will be useless to you.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's
Re: uart comms in C
int n = write("Hello Peripheral\n");
to the format int write(int fd, char *Buff, int NumBytes);
int n = write(fd,"Hello",6);
my comms default is 9600baud
It's work,thx.
but how if i want sent a int type?
sory I am newbe
to the format int write(int fd, char *Buff, int NumBytes);
int n = write(fd,"Hello",6);
my comms default is 9600baud
It's work,thx.
but how if i want sent a int type?
sory I am newbe
Re: uart comms in C
To send an int, e.g.budi wrote:int n = write("Hello Peripheral\n");
to the format int write(int fd, char *Buff, int NumBytes);
int n = write(fd,"Hello",6);
my comms default is 9600baud
It's work,thx.
but how if i want sent a int type?
sory I am newbe
int myInt;
int newInt;
myInt = 78123;
write(fd, &myInt, sizeof(myInt)); /* pass address and size in bytes of myInt to send an int */
...
read(fd, &newInt, sizeof(newInt)); /* to receive an int */
Re: uart comms in C
Don't.budi wrote:but how if i want sent a int type?
sory I am newbe
If you just send strings, then you can use non-printable characters, like New-Line, to frame it. If the receiver gets out of step it just has to wait for the next framing character to get back in step. But if you send "binary" values, such as integers, there are no framing characters available because the individual bytes of the integer can have any values, including zero and new-line. The receiver has to work by dead-reckoning and if it gets out of step there is no way for it to synchronise ever again. There are techniques you can use that help, but they are complex and none of them are perfect. Because the bytes can be zero, you can't use the standard string functions, which all depend on the only zero being at the end of the string.
Also, using sizeof(int) only works if an integer is the same size on both machines. If one machine uses 4-byte integers and the other uses 2-byte integers, that simple code will fail.
To send an integer, convert it to a string.
Code: Select all
int myInt = 42;
char myname[]="Fred";
double pi = 3.141592265
char s[100];
sprintf (s, "%s, %d, %f\n", myname,myInt,pi);
write(fd, s, strlen(s));
Code: Select all
int myint;
char myname[100];
double pi;
int i;
char s[100], c;
for (i=0; i<sizeof(s); i++)
{
read(fd, &c, 1);
s[i] = c;
if (c == '\n')
break;
}
sscanf(s, "%s, %d, %f\n", myname, &myint, &pi);
Re: uart comms in C
thx a lot that's work
but why sometimes its running a previous program, although I have been delete/command that?
example:
first I wrote
char s[100];
sprintf (s, "K%03i%03i\n", x,y);
write(fd, s, strlen(s));
then I change it
char s[100];
sprintf (s, "K%03i%03i\n", 10,-20);
write(fd, s, strlen(s));
it's still run the firs program

but why sometimes its running a previous program, although I have been delete/command that?
example:
first I wrote
char s[100];
sprintf (s, "K%03i%03i\n", x,y);
write(fd, s, strlen(s));
then I change it
char s[100];
sprintf (s, "K%03i%03i\n", 10,-20);
write(fd, s, strlen(s));
it's still run the firs program
Re: uart comms in C
That's usually the result of one of two mistakes:
- You forgot to save the file before compiling it.
- You forgot to compile it.
Re: uart comms in C
Hi,
I wrote a code to use uart with microcontroler. I'm sending few string from RPI to microcontroler which parses this instructions and makes some things, but unfortunately I have a big lag like 3-4s. I use 115200baudrate, nonblock mode. I'm reading a data in infinite loop, which breaks when it reads good message.
Could anyone have a problem something like that?
Edit. I added tcflush function after each of loop and this solved the problem.
Regards slqa
I wrote a code to use uart with microcontroler. I'm sending few string from RPI to microcontroler which parses this instructions and makes some things, but unfortunately I have a big lag like 3-4s. I use 115200baudrate, nonblock mode. I'm reading a data in infinite loop, which breaks when it reads good message.
Could anyone have a problem something like that?
Edit. I added tcflush function after each of loop and this solved the problem.
Regards slqa
Last edited by slqa on Sat Nov 23, 2013 7:31 am, edited 1 time in total.
Re: uart comms in C
thank a lot rurwin
Re: uart comms in C
There's another two causes of the same problem.
1. You're not executing the code-path you think you are.
2. You're not editing the code-path that is executing.
In both these cases single-stepping in a debugger or inserting lots of debug printf's can show you where you are going wrong.
I get all four bugs on a regular basis. Number 2 above happens a lot when you've got two sections of code that are almost identical.
1. You're not executing the code-path you think you are.
2. You're not editing the code-path that is executing.
In both these cases single-stepping in a debugger or inserting lots of debug printf's can show you where you are going wrong.
I get all four bugs on a regular basis. Number 2 above happens a lot when you've got two sections of code that are almost identical.