Linux - HardwareThis forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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");
}
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.
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)
}
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.