Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - Multiple Serial Port Communication with C

Tags:

c

serial-port

I have a device with multiple serial ports that I am programming with embedded linux and I would like to communicate over these two ports simultaneously and asynchronously.

I know how to write to one serial port such as:

bytes_sent = write( fd, &(string[i]), 1 );

But that's to only one serial port

do I use the termios struct and the c_cflags to differentiate ports? As you can see it's a little vague, I'm just kind of diving in and getting my feet wet with this, any general help to point me vaguely in the right direction will help.

like image 211
user1257629 Avatar asked Feb 14 '26 04:02

user1257629


1 Answers

How did you get the file descriptor for your first serial port? Assuming it was something like:

fd = open("/dev/serialPort0", O_RDWR);

You should just be able to do:

fd2 = open("/dev/serialPort1", O_RDWR);

And get a file descriptor to use for the other serial port. Write to each however you'd like:

char str1[] = "Hello, port 1!\n";
char str2[] = "hello, port 2!\n";

write(fd, str1, sizeof str1);
write(fd2, str2, sizeof str2);
like image 174
Carl Norum Avatar answered Feb 15 '26 19:02

Carl Norum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!