user1257629
user1257629

Reputation: 45

Linux - Multiple Serial Port Communication with C

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.

Upvotes: 3

Views: 3185

Answers (2)

wallyk
wallyk

Reputation: 57774

Please see a related answer to configure the port to the desired speed, parity, and i/o blocking characteristics.

Even if the hardware has 4 or 24 serial ports, proper handling is to treat each individually and independently.

Upvotes: 1

Carl Norum
Carl Norum

Reputation: 224904

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);

Upvotes: 5

Related Questions