gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

serial communication with xbee problem

Thu Nov 20, 2014 10:34 am

hello everyone
i am using the serial library of wiringPi
here is a code

Code: Select all

;
#include<stdio.h>
#include<errno.h>

#include<wiringSerial.h>

#include<string.h>

/*
*
*/
int main() {
int fd;
if ((fd = serialOpen(device, 9600))<0) {
fprintf(stderr, "unable to open serial device");
return 1;
}
if (wiringPiSetup () == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
while(1)
serialPutchar(fd,'A');
}
compile is ok but there is no data receive by raspberry...
My connection is 
RPI-------------->Xbee
Rx---------------->Tx
GND-------------->GND


and i am sending data by other PC where second XBEE is connected...
in baud rate 9600



if anyone help me i will really oblise.

danjperron
Posts: 2749
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: For serial communication

Sun Nov 23, 2014 11:09 pm

Xbee devices are tricky.

Do you setup the XBee correctly? This is 99.5% of the problem.

Did you try minicom to see if you are able to communicate manually .

Code: Select all

sudo apt-get install minicom
Please select /dev/ttyAMA0 and the correct baud rate and no handshake.
If you press type +++ does the Xbee returns OK?


What about the 3.3V power. You can't use the RPI 3.3V because there is not enough current to transmit! You need to have your own regulator. A LE33CZ regulator connect to the 5V with a 1 uF capacitor will do the trick. The 1uF capacitor or more is needed because the XBee will oscillate is own power otherwise.


Daniel

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Tue Nov 25, 2014 6:23 am

hello sir
thank for the reply
xbee is working okk but it stopeed certainly and again transmit data...
Next my question is that when i transmitt data from other PC.....will it show on Lx terminal..via Xbee
TX data from other PC and received to raspberry pi via xbee...
where the received data seen mean in LX terminal
printf("serialgetchar(fd)");

User avatar
joan
Posts: 12973
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: For serial communication

Tue Nov 25, 2014 9:22 am

A first step would be to see if you can localise the error.

If you replace the XBee with a wired link (assuming both ends are 3V3 safe) does the software work properly.

It would be helpful if you edited your post and put the code in

Code: Select all

 
quotes (lower case).

danjperron
Posts: 2749
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: For serial communication

Tue Nov 25, 2014 12:35 pm

I had that problem once. It was the handshake .

Be sure that you toggle DTR and RTS correctly. It is really needed if you are using usb to xbee adapter . If the RTS signal is wrong, nothing get out of the Xbee. Maybe your data rate is too high and you overfill the buffer. Try to reduce the baud rate.

check http//ftp1.digi.com/support/documentati ... 0982_R.pdf

page 12 chapter 2 . they explain about handshake.

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Thu Nov 27, 2014 6:13 am

thank for your helpful reply...
now i am interfacing the gps module to it...
here is a code

Code: Select all

#include<stdio.h>
#include<errno.h>
#include<wiringPi.h>
#include<string.h>
#include<wiringSerial.h>
int main()
{
char gps[255];   //gps string array

int i;

if((fd=serialOpen("/dev/ttyAMA0",9600))<0)

{
fprintf(stderr,"unable to open serial device%s\n",strerror(errno));
}

else
{
printf("serial UART is Ok\n");
}

printf("raspberry gps programme");

if(wiringPiSetup()<0)     //setup wiring pi
return -1;
else
printf("setup is ok\n");

while(1)
{
for(i=0;i<=65;i++)
{
gps[i]=serialGetchar(fd);
}
if(i==65)
break;
}

printf("%s",gps);
}
fflush(fd);
}
but it does not receive at least one character...
please help me for this....


and what is ttyAMA0...
what does it mean and how to work...?
actually i am new in linux world i used only IDE's....

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: For serial communication

Thu Nov 27, 2014 6:28 am

gauravsharma0190 wrote:and what is ttyAMA0...
what does it mean and how to work...?
actually i am new in linux world i used only IDE's....
ttyAMA0 is the on-board UART. The Tx and Rx pins appear on the GPIO header, and signalling is done at 3.3V.

If you don't know what it is and where it is then you probably don't have anything connected to it, which may be why you have received no data. If you are plugging in a USB serial interface then it will probably appear as /dev/ttyUSB0

If you are new to Linux, you have a lot to learn, but there are tons of on-line resources to help you.

danjperron
Posts: 2749
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: For serial communication

Thu Nov 27, 2014 11:43 am

Hi gauravsharma0190,

/dev/ttyAMA0 doesn't have handshake. Could you show how you connect the Xbee.

something like

http://www.raspberrypi.org/forums/viewt ... 81#p600981


It is on the other side that i’m worry about . The PC side.

Some questions are still pending.

- How did you connect the Xbee to the Raspberry PI and on the PC also.
- What is your setting inside the XBee. Same baud rate.
- Did you try minicom and set the handshake to none since /dev/ttyAMA0 has none!
if you type +++ does it report OK.
- Which version of Xbee do you have Series 1, 2?
- did you disable the debug and the tty console in /boot/cmdline.txt and /etc/inittab

I’m using USB to Xbee adapter from ,
Parallax, DFRobot and one from DX.com called Foca v2.2 and they works well.

Daniel

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Wed Dec 03, 2014 4:45 am

thank you very much to all
i understood the tttyAMA0 is a driver of UART....which opens the serial port on gpio header...
ok!!
now when i short RX and TX pins the data came on terminal of raspberry pi

Code: Select all

while(1)
{
serialPutchar(fd.'a');
printf("%d\n",serialGetchar(fd));
}
fflush();
}
here 97 is received....
thats mean serial data which i  sent is received......
but in my gps code it is not received....why????
should there is need to change the UART like  edit /boot/cmdline.txt

User avatar
joan
Posts: 12973
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: For serial communication

Wed Dec 03, 2014 5:08 am


gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Wed Dec 03, 2014 6:05 am

yes john
i seen it.
it is for USB which you connected to your pi board.

but my GPS sends serial data and have extra pin of 3V3.
they are RX TX and GND.
now i connected it with my Pi's UART...

now my question is....
should there is need to change in /boot/cmdline.txt and /etc/inittab file to enable the UART

Code: Select all

#include<stdio.h>
#include<errno.h>
#include<wiringPi.h>
#include<string.h>
#include<wiringSerial.h>
int main()
{
char gps[255];   //gps string array

int i;

if((fd=serialOpen("/dev/ttyAMA0",9600))<0)

{
fprintf(stderr,"unable to open serial device%s\n",strerror(errno));
}

else
{
printf("serial UART is Ok\n");
}

printf("raspberry gps programme");

if(wiringPiSetup()<0)     //setup wiring pi
return -1;
else
printf("setup is ok\n");

while(1)
{
for(i=0;i<=65;i++)
{
gps[i]=serialGetchar(fd);
}
if(i==65)
break;
}

printf("%s",gps);
}
fflush(fd);
}
in this code i adjust ttyAMA0 driver to 9600 baud rate .
so will it automatically set to this baud rate or will i change it by changing the /boot/inittab file

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Fri Dec 05, 2014 5:57 am

can anyone solve my problem i am stucking in it much time...
second thing
in the above code in my post when i add the statement after while loop like this

Code: Select all

while(1)
{
for(i=0;i<=65;i++)
{
gps[i]=serialGetchar(fd);
}
if(i==65)
break;
}
printf("the data is\n");
printf("%s",gps);


fflush(fd);
}
when i run the code it will not come out from loop and i don't get ""the data is"". from it..
That means it stucks in the loop..............??? ;)

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: For serial communication

Fri Dec 05, 2014 7:59 am

gauravsharma0190 wrote: i don't get ""the data is"". from it..
That means it stucks in the loop..............??? ;)
Correct! It's stuck in the loop. Now you get the opportunity to find out why.

I suggest you print out gps inside your loop so that you can see what's going on.

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Fri Dec 05, 2014 11:02 am

OK!!!!!
I agree with you ame.... i will do that if it also won't see anything then???

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Mon Dec 08, 2014 4:37 am

can anyone tell me the problem....
problem are on above posts...
thanks

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: For serial communication

Mon Dec 08, 2014 5:33 am

gauravsharma0190 wrote:can anyone tell me the problem....
problem are on above posts...
thanks
Did you try printing the received character inside the loop?

The problem is you haven't tried debugging things.

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Mon Dec 08, 2014 5:47 am

sir
i tried it but nothing happens......don't know what's happening???
it stucks in the loop

User avatar
DougieLawson
Posts: 31214
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: For serial communication

Mon Dec 08, 2014 9:58 am

Code: Select all

while(1)
{
  for(i=0;i<=65;i++)
  {
    gps[i]=serialGetchar(fd);
  }
  if(i==65) break;
}
printf("the data is\n");
printf("%s",gps);
The only way to get stuck in that loop is if serialGetchar() blocks waiting for more data from the device it's reading. You're making an assumption that there are always 66 bytes of data sent on the serial interface.

Each time round the loop you need to test whether serialGetchar() returns -1 (for no data available). Else, in the worst case, you could wait up to 660 seconds (eleven minutes) for your program to end.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Wed Dec 10, 2014 8:54 am

int serialGetchar (int fd) ;
[*]
Returns the next character available on the serial device. This call will block for up to 10 seconds if no data is available (when it will return -1)[*]
i did as you told me but nothing happened

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: For serial communication

Wed Dec 10, 2014 9:06 am

Please *think* about the problem.

If nothing is received it means that either:
a) Nothing was sent
b) Your serial port is mis-configured
c) You're looking in the wrong place

There are many more possible causes, each with diminishing likelihood.

What port are you opening with your variable 'fd'?

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Wed Dec 10, 2014 11:43 am

hello ame
i am using ttyAMA0 for my serial port by using fd
and i configured the serial comm. by /boot/cmdline.txt
/etc/inittab

danjperron
Posts: 2749
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: For serial communication

Wed Dec 10, 2014 1:48 pm

I reread the post and I didn't find the answer of my post I did earlier.
Maybe you could start by this if you need us to have a better understanding.

1- First A layout picture of your settings

2- Maybe just use a pc and the Raspberry PI and see if you are able to connect both Xbee using terminal application.
minicom for the RPi and the Xbee application for the pc. Do they communicate manually.

3- Add leds with a resistor to the TXM and RCV pins to the XBee connected to the Raspberry PI. This way you could see if something transmit. (Anode on 3.3V, cathode with a 2K resistor on the XBee pins).

Also don't send and waiting to receive using SerialGetChar(). You are creating a lockup. Change attribute and set a time out with the termios options (tcgetattr and tcsetatr). http://ulisse.elettra.trieste.it/servic ... onfig.html
Or use "struct timeval"
http://stackoverflow.com/questions/1052 ... l-port-c-c

The other simpler method is to check the number of available character "serialDataAvail" before SerialGetChar(). This way you don't create lockup because you could add your own time out routine if you don't received anything.


On your P.C. what king of Xbee setup you have? I assumed a USB XBee Adapter. If it is true you could removed the XBee and connect the TXM and RCV pins of the adapter directly to the RCV,TXM of the RPI since the USB adapter will already set the pins to 3.3V. This way you could check the direct link without any Xbee adapters and see if you have communication.

Since You give just little code without showing us How the layout is, it is very hard to figure out what you are doing wrong.

Daniel

danjperron
Posts: 2749
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: For serial communication

Wed Dec 10, 2014 2:18 pm

If you need a simple example program to check your Xbee.

I did a sample program to display a counter on a window PC computer in visual C++

A python program will send via Xbee a number and on the PC side A window will display the number.

http://www.raspberrypi.org/forums/viewt ... 17#p597417

And this is some code I did when I removed my big obsolete Slackware server and replace it with a Raspberry PI. Before it was a serial link but since RS-232 was not available, I swapped the RS-232 with a Xbee to control my old Roomba 430. And yes it's still working. With this roomba I'm able to power it ON using the IR signal.

http://www.raspberrypi.org/forums/viewt ... 29#p464729

Since I only use the transmit mode. It will show only the method I used to setup the communication using standard linux communication. I know people use WiringPi a lot but it is always good to know that you could use already available build function.

Daniel

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Mon Dec 15, 2014 4:55 am

hello DougieLawson

Code: Select all

The only way to get stuck in that loop is if serialGetchar() blocks waiting for more data from the device it's reading. You're making an assumption that there are always 66 bytes of data sent on the serial interface.

Each time round the loop you need to test whether serialGetchar() returns -1 (for no data available). Else, in the worst case, you could wait up to 660 seconds (eleven minutes) for your program to end
i agree with you but gps sent a data continuously and i want to see weather anything is received by pi or not....
do you have any idea,,, how can do it...
thank for your kind reply

gauravsharma0190
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am

Re: For serial communication

Mon Dec 15, 2014 10:05 am

i tried minicom...
i configure it like /dev/ttyAMAO
baud rate 9600 8N1
now save it with gps
but error occurs like"""cannot save to /etc/minicom/minirc.gps

what is the problem..i googled and follow the steps to starts the minicom..

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 12 guests