Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I need to access the data from CC2530ZNP usb dongle to my application on linux PC ,
i'm using the following c code read the data print on console .
Below is the code which I use to read the data .
When I do it first time it works fine , when I close the program and re-run it again it wont, it opens the port well but it does not read anything , the read buffer is empty ..
If I restart my system it works again for the first time .. why it is unable to read the data in second time ..? I'm using ubuntu 11.04 ...pls help
while (STOP==FALSE) { /* loop for input */
res = read(fd,buf,255); /* returns after 5 chars have been input */
buf[res]=0; /* so we can printf... */
printf(":%s:%d\n", buf, res);
if (buf[0]=='z') STOP=TRUE;
}
tcsetattr(fd,TCSANOW,&oldtio);
}
I'm sorry I cannot try to duplicate your problem since I don't have your hw and sw, so, this is no better than a guess:
Are you quite sure that the ``tcsetattr(fd,TCSANOW,&oldtio);'' is actually getting executed. Since you don't handle signals, if your program was stopping because of a signal the last call would not be executed. The behavior of your program is consistent with the settings of the serial driver not getting restored at the end of the run.
At least, print the return value of the last tcsetattr() call and maybe also errno.
void finalize(void)
{
//...
tcsetattr(fd,TCSANOW,&oldtio);
// check for error
if (fd) fclose(fd);
}
int main(int argc, char *argv[])
{
atexit(finalize);
//...
exit(0);
}
I've run into this issue more times that I can count, and it has nearly always been the port not being properly released. Adding fclose should clear it up.
Hi,
I tried closing the fd but no success, and also checked I checked the return value of the last tcsetattr() it was one
so I have written bit better code to read the data and print
It works for some times and if remove the device and reconnect , it connect with out any error but it read nothing in the read buffer .
after trying many times(removing and connecting ) it works again , then if I remove it again it goes to the same state .
what may be the problem ...? why it reads nothing when I read ..? and also I tried in minicom , there also it gives same problem , it connects well and able to read if I remove the device and reconnect, it shows cant open dev/ttyACM0! .
I have attached my code for the reference
// this is the mainline thingee
int main(int argc, char *argv[])
{
char sCmd;
char sResult[254];
if (argc < 2 || argc > 2)
{
printf("adrserial needs 1 parameter for the serial port\n");
printf(" ie. use 'adrserial 0' to connect to /dev/ttyS0\n");
return 0;
} // end if
printf("Type q to quit.\n\n");
if (OpenAdrPort(argv[1]) < 0) return 0;
while (1)
{
int iSpot;
// opens the serial port
// return code:
// > 0 = fd for the port
// -1 = open failed
int OpenAdrPort(char* sPortNumber)
{
char sPortName[64];
printf("in OpenAdrPort port#=%s\n", sPortNumber);
sprintf(sPortName, "/dev/ttyACM%s", sPortNumber);
printf("sPortName=%s\n", sPortName);
// make sure port is closed
CloseAdrPort(fd);
sleep(1);
fd = open(sPortName, O_RDWR | O_NOCTTY | O_NDELAY);
sleep(1);
CloseAdrPort(fd);
sleep(1);
fd = open(sPortName, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0)
{
printf("open error %d %s\n", errno, strerror(errno));
}
else
{
struct termios my_termios;
printf("fd is %d\n", fd);
tcgetattr(fd, &my_termios);
// NOTE: you may want to save the port attributes
// here so that you can restore them later
printf("old cflag=%08x\n", my_termios.c_cflag);
printf("old oflag=%08x\n", my_termios.c_oflag);
printf("old iflag=%08x\n", my_termios.c_iflag);
printf("old lflag=%08x\n", my_termios.c_lflag);
printf("old line=%02x\n", my_termios.c_line);
printf("new cflag=%08x\n", my_termios.c_cflag);
printf("new oflag=%08x\n", my_termios.c_oflag);
printf("new iflag=%08x\n", my_termios.c_iflag);
printf("new lflag=%08x\n", my_termios.c_lflag);
printf("new line=%02x\n", my_termios.c_line);
} // end if
return fd;
} // end OpenAdrPort
// writes zero terminated string to the serial port
// return code:
// >= 0 = number of characters written
// -1 = write failed
int WriteAdrPort(char* psOutput)
{
int iOut;
if (fd < 1)
{
printf(" port is not open\n");
return -1;
} // end if
iOut = write(fd, psOutput, strlen(psOutput));
if (iOut < 0)
{
printf("write error %d %s\n", errno, strerror(errno));
}
else
{
printf("wrote %d chars: %s\n", iOut, psOutput);
} // end if
return iOut;
} // end WriteAdrPort
// read string from the serial port
// return code:
// >= 0 = number of characters read
// -1 = read failed
int ReadAdrPort(char* psResponse, int iMax)
{
int iIn;
printf("in ReadAdrPort iMax=%d\n", iMax);
if (fd < 1)
{
printf(" port is not open\n");
return -1;
} // end if
strncpy (psResponse, "N/A", iMax<4?iMax:4);
iIn = read(fd, psResponse, iMax-1);
printf(" chars: %s\n", psResponse);
if (iIn < 0)
{
if (errno == EAGAIN)
{
return 0; // assume that command generated no response
}
else
{
printf("read error %d %s\n", errno, strerror(errno));
} // end if
}
else
{
psResponse[iIn<iMax?iIn:iMax] = '\0';
// printf("read %d chars: %s\n", iIn, psResponse);
} // end if
return iIn;
} // end ReadAdrPort
// closes the serial port
void CloseAdrPort()
{
// you may want to restore the saved port attributes
if (fd > 0)
{
close(fd);
} // end if
} // end CloseAdrPort
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.