3

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.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
user1257629
  • 45
  • 3
  • 7

2 Answers2

5

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);
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

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.

Community
  • 1
  • 1
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • If we have two serial ports A and B , think about server client communication is there any way to connect thoose two ports in order to send messeges ? – gregni Feb 09 '21 at 19:22
  • @nikos.grigoriadis: Sure. Are ports A and B on the same machine? What is the topology? – wallyk Feb 11 '21 at 07:12
  • yeah i want to make an AT command based server and client where they will communicate through seria port and reading writing will be at tty files .Yeah is on the same machine i have to test it with socat but i cant fix it am openning /dev/pts/0 at server and trying to read something through it but i cant make it work .What should i do in client open the same tty file ? or openning /dev/pts/1 ? if i open the pts/1 who to connect them with socat and test when client writing something server reads it and send responce ? If you can help me with this i would appreciate – gregni Feb 11 '21 at 09:02
  • @nikos.grigoriadis: What does the Linux command `lsusb` show? – wallyk Feb 11 '21 at 17:24