0

How can we enable serial communication between an XBee S2C and a Raspi 3 through the USB port?

For this purpose, I use Sparkfun's XBee explorer dongle. I have several of this combination to create a ZigBee network and test a protocol coded in C. However, I am having problems at reading from /dev/ttyUSB0. BTW, the problem may be at the sender side.

The XBees are running with the serial interface configuration given below:

115200 baudrate
No parity
1 stop bit
3 character times for packetization timeout
CTS flow control is enabled
RTS flow control is disabled
API mode is enabled
API output mode is native

Therefore, I initialized the port in my code as given below:

int initport(int fd)
{
int portstatus = 0;

    struct termios options;
    // Get the current options for the port...
    tcgetattr(fd, &options);
    // Set the baud rates to 9600...'textB' un
    cfsetispeed(&options, B115200);
    cfsetospeed(&options, B115200);
    // Enable the receiver and setSTX local mode...
    options.c_cflag |= (CLOCAL | CREAD);

    //options.c_cflag &= ~PARENB;
    //options.c_cflag &= ~CSTOPB;   //When these are disabled the XBee receives data.
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    //options.c_cflag |= SerialDataBitsInterp(8);           /* CS8 - Selects 8 data bits */
    options.c_cflag &= ~CRTSCTS;                            // disable hardware flow control
    options.c_iflag &= ~(IXON | IXOFF | IXANY);           // disable XON XOFF (for transmit and receive)
    //options.c_cflag |= CRTSCTS;                     /* enable hardware flow control */


    options.c_cc[VMIN] = 200;     //min carachters to be read
    options.c_cc[VTIME] = 5;    //Time to wait for data (tenths of seconds)


    // Set the new options for the port...
    //tcsetattr(fd, TCSANOW, &options);


    //Set the new options for the port...
    tcflush(fd, TCIFLUSH);
    if (tcsetattr(fd, TCSANOW, &options)==-1)
    {
        perror("On tcsetattr:");
        portstatus = -1;
    }
    else
        portstatus = 1;

    return portstatus;
}

I wanted to ask this question here because I believe I need to make some modifications in some files such as /boot/config.txt and/or /boot/cmdline.txt. In my search in the web, this kind of modifications popped up but they didn't work in my case.

Finally, the Raspis are running the latest version of Raspbian Jessie and Linux kernel 4.9.35-v7+.

$uname -a
Linux raspberrypi 4.9.35-v7+ #1014 SMP Fri Jun 30 14:47:43 BST 2017 armv7l GNU/Linux

$cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

Please don't hesitate ask any other detail about my setup.

Thank you in advance.

Samet Tonyalı
  • 214
  • 2
  • 9
  • Can you try with AT transparent mode? If "+++" sends OK in response at Minicom/Picocom? – Abhishek Parikh Jul 31 '17 at 08:25
  • 1
    @AbhishekParikh I am abroad now, but I will try this when I get back. I just want to know what we can infer if it works or does not work. – Samet Tonyalı Jul 31 '17 at 18:51
  • *"When these are disabled the XBee receives data."* -- How do you know that if you're *" having problems at reading"*? The serial port parameters must match on each side. Your code does modify the termios structure with the proper method, but the configuration is incomplete. You omit mentioning the frame size, but configure 8 bits. You only enable half of the HW flow control, and configure no flow control! There is no specific configuration of canonical or raw mode, and it's simply left to chance. You complain of read issues, but post no code for read operations. – sawdust Aug 01 '17 at 01:29
  • @sawdust Once, I was able to read data sent by XCTU at the sender side, but I was unable to read the data sent by my own program at the sender. Somehow, I lost the working setting and couldn't find the correct combination. If you are talking about the data bit configuration on the XBees it is 8 bits (115200/8/N/1). I think you are saying both the device configuration and code should comply with each other. I remember I tried almost all combinations but will try it again. Also, I will update the question soon and add some read operations code. – Samet Tonyalı Aug 01 '17 at 02:32
  • Okay No worries @SametTonyalı – Abhishek Parikh Aug 01 '17 at 05:55
  • Consider using this library of portable ANSI C code to communicate with XBee modules in API mode: https://github.com/digidotcom/xbee_ansic_library – tomlogic Aug 04 '17 at 02:07

0 Answers0