LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Closed Thread
  Search this Thread
Old 04-09-2013, 11:40 AM   #1
Marvin_G
LQ Newbie
 
Registered: Dec 2012
Posts: 14

Rep: Reputation: Disabled
Communication failure


hye guys, i have this C code to communicate with external device which i need to send command and retrieve data from it but it's not working although it can be compiled. I'm not really good in C, please help me guys. Thanks in advanced.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>

#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE; 
int wait_flag=TRUE;  /* TRUE while no signal received */

void signal_handler_IO (int status);   /* definition of signal handler */

int n;
int fd, res;
int connected;
struct termios termAttr;
struct sigaction saio;
char buf[255];

int main(int argc, char *argv[])
{
     fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
     if (fd == -1)
     {
        perror("open_port: Unable to open /dev/ttyO1\n");
        exit(1);
     }

     saio.sa_handler = signal_handler_IO;
     saio.sa_flags = 0;
     saio.sa_restorer = NULL; 
     sigaction(SIGIO,&saio,NULL);

     fcntl(fd, F_SETFL, FNDELAY);
     fcntl(fd, F_SETOWN, getpid());
     fcntl(fd, F_SETFL,  O_ASYNC ); /**<<<<<<------This line made it work.**/

     tcgetattr(fd,&termAttr);
     //baudRate = B115200;          /* Not needed */
     cfsetispeed(&termAttr,B115200);
     cfsetospeed(&termAttr,B115200);
     termAttr.c_cflag &= ~PARENB;
     termAttr.c_cflag &= ~CSTOPB;
     termAttr.c_cflag &= ~CSIZE;
     termAttr.c_cflag |= CS8;
     termAttr.c_cflag |= (CLOCAL | CREAD);
     termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
     termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
     termAttr.c_oflag &= ~OPOST;
     tcsetattr(fd,TCSANOW,&termAttr);
     printf("UART1 configured....\n");

     connected = 1;
     while(connected == 1){
           // some code
	   write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
	   
            if (wait_flag==FALSE) { 
            res = read(fd,buf,255);
            buf[res]=0;
            printf(":%s:%d\n", buf, res);
            if (res==1) STOP=TRUE; /* stop loop if only a CR was input */
            wait_flag = TRUE;      /* wait for new input */
          }

     }

     close(fd);
     exit(0);             
}

void signal_handler_IO (int status)
{
     printf("received data from UART.\n");
}
 
Old 04-09-2013, 01:04 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
All you said what it didn't work, not what the problem specifically is.

Have you tried using minicom to talk to this port and been successful?

I think if you're 115.2K N81 nothing special, then you don't need to set all the port settings explicitly, just read the attributes, configure the speed, and set the attributes.

Does the port normally send out periodic data? If so then do receive first and verify that you can receive and properly interpret the data. Next send it the most basic command, like a version command or something so you can see the result.

Looks like a binary code. I don't use escape sequences and hex or octal designators in a visible string, what I do instead is declare the data truly as hex. So what I would do instead would be to declare that command as an array of characters or unsigned characters.

Code:
static unsigned char myCommand[8] = { 0xf5, 0x01, 0x00, 0x01, 0x03, 0x00, 0x03, 0xf5 };
And then write(2) myCommand to the file descriptor. Also check the result from the call to write(2). Never ignore a library's function return.

Code:
if((cnt = write(fd, myCommand, sizeof(myCommand))) != sizeof(myCommand)) {
  // print error, check errno, also use strerror(errno), but print the actual errno
  // check the possible errors that can come from invoking write(2)
}
 
Old 04-11-2013, 09:39 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL : Communication Link failure error and telnet localhost 3306 : Connection close yannifan Programming 2 04-01-2011 06:44 AM
LXer: darcs: a study in communication failure LXer Syndicated Linux News 0 08-04-2008 11:30 AM
Fault in KDE processes communication: Could not read network communication list Magnus Johansson MEPIS 0 03-30-2008 12:50 PM
Tomcat: Communication link failure: EOFException Vince-0 Linux - Server 0 03-11-2008 04:38 AM
Telnet : Temporary failure in name resolution : Host name lookup failure koodoo Linux - Newbie 10 02-11-2008 07:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 03:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration