Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
Hello all.. I am working on an embedded project and I would like to send single bytes over /dev/ttyS0. The problem is, these terminal programs are set up to transmit ascii, not non-ascii data - at least as far as I know.
I tried using libserial and c++, but having issues sending raw data as well.
I don't care what method is used, I just need to send a few bytes.. (nrz format) 0xfd, 0xc2 to debug my code on a pic microcontroller.
Does anyone have any examples of code or know if theres a method to do this in linux? Maybe minicom, kermit or straight from the command line?
Thanks
Derek
I think C-Kermit provides the 'transmit' command to send a file of arbitrary byte content. There is always the route of a good old hand-crafted purpose-built tool based on examples from Serial Programming Guide for POSIX Operating Systems. Using tools like setserial and stty may allow you to configure a serial port to the point where you can use shell commands to send arbitrary bytes to the serial device, without any interference from the device driver.
--- rod.
Excellent. I like the hand crafted approach, but am still having issues working from the POSIX link you sent me. When write is called, i'm getting an error from the compiler ->
"invalid conversion from ;int' to 'const void*'.
please see below code:
Code:
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
void main()
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
/*
* Get the current options for the port...
*/
tcgetattr(fd, &options);
/*
* Set the baud rates to 9600...
*/
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_oflag &= ~OPOST; //raw output
/*
* Enable the receiver and set local mode...
*/
options.c_cflag |= (CLOCAL | CREAD);
/*
* Set the new options for the port...
*/
tcsetattr(fd, TCSANOW, &options);
int n = write(fd, 0xfd, 8);
if (n < 0)
fputs("write() of 8 bytes failed!\n", stderr);
close(fd);
}
Obviously the write function doesn't want an integer.. how do I make this work?
Thanks!
I hate to restart this thread, but I'm still having an issue apparently..
I thought this worked ok, but it seems that when I execute the code up above my serial port locks up. I changed to code to just send one character "f".
Code:
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
void main()
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
/*
* Get the current options for the port...
*/
tcgetattr(fd, &options);
/*
* Set the baud rates to 9600...
*/
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_oflag &= ~OPOST; //raw output
/*
* Enable the receiver and set local mode...
*/
options.c_cflag |= (CLOCAL | CREAD);
/*
* Set the new options for the port...
*/
tcsetattr(fd, TCSANOW, &options);
int n = write(fd, "f", 1);
if (n < 0)
fputs("write() of 1 byte failed!\n", stderr);
close(fd);
}
When I run minicom everything works fine.. I send an f and my little microchip blinks an LED.
When I run the above code nothing happens. I have a scope connected to my serial TX line and I don't see anything. So.. I tried this after a reboot:
1. command line -> "echo blah > /dev/ttyS0"
I see nice pulses on my scope.
2. minicom - I can send anything and see nice pulses.
3. Run my program - I see nothing. I close my program and then try to do the "echo..." from the command line and nothing happens. The echo just hangs there and I have to do a ctrl-c to kill it.
It seems that I lose the ability to send anything after I run my program. Except minicom - it works after I run my program.
Any ideas on this one? lockfiles? permissions?
I noticed also that when I run my program, after I do a write, it hangs for about 30 seconds and then returns to the command line.
You've probably messed up the port settings. All those functions where you set an option return the old option. First, have it working with your external tools. Next, run your program and step through it with a debugger and see what all those returned values are. Those returned values are the ones that were last set by your external tools. You should be setting then in your program to match those working ones.
Thank you Judy for helping me out so far..
I think I'm in over my head here in setting these bits. One thing I noticed was that I wasn't doing this:
tcgetattr(fd, &options) before doing all the bitmasking.
It seems that 0xcbd is the magical combo for the cflags (I printed out that value right after the tcgetattr), but I just can't seem to configure anything to come close to it. I am close to throwing in the towel though.
Rod, that link is where I came up with my non-working code. I think I'm just not "getting it".
All of the examples that I've tried from the internet don't seem to work for me... and I've tried many.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.