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 am writing an application in C on a Beagleboard (running Ubuntu) which reads sound samples from a microphone (using ALSA APIs), compresses them and writes it to the serial port to a radio (which broadcasts it). For duplex communication, i need to receive the data from the radio (by reading from the serial port), uncompress it and send it to the speaker (again through ALSA APIs). The problem here is that the whole operation is performed in a loop. I am writing byte by byte into the port and sending it to the radio. However the radio writes 80 bytes as and when it receives a packet and there is no synchronization between the two devices. Effectively i am unable to read any data from the serial port. Can anyone suggest a better method of implementation ?
when I get you right you send out and receive data from a character device; all within a single loop. You miss synchronization between the I/o packets and you are unable to receive data.
Let us start with your devices. Take a look into the /dev path:
# ls -l /dev/*tty*
The device files listed in /dev are interfaces to the (device)drivers. The ls command also shows the major number - the driver number of a system device. When you check the /dev directory you probably will notice that many devicefiles share one major number. The serial interface for example has many major numbers even it is still the same physical interface. The various numbers represent different drivers assigned to that interface, terminals, modems etc.
First please check that you are using the right driver, not a terminal driver for example for your serial communication.
The R/W access to character devices utilizes file I/O. You already know this from I/O redirection in your shell. The command
# cat readme.txt > /dev/lp0
redirects the terminal I/O of the file "readme.txt" to the local printer. In a c code you of course can do the same:
int fd; # filedescriptor
fd = open( /dev/stty0", O_RDWR); # alow all I/O
if (fd >= 0)
write(fd, buffer, buffersize);
close(fd)
Use the system function ioctl to gain complete control over your block device and do not forget to fflush your I/O buffers.
Thanks Martin. I am now using two separate threads - one for reading from the mic and writing to the serial port and the other for reading from the serial port and writing to the speaker. I am facing another problem here. The speaker expects samples at 8kHz. So I am using a non blocking read to get 80 bytes from the serial port and passing it as soon as possible to the speaker. Here again I have synchronization issues because of the non blocking read. I am not able to get 80 bytes. A blocking read may not provide data at the required speed. Please suggest what can be done.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.