简体   繁体   English

Linux - 与C的多串口通信

[英]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. 我有一个带有多个串口的设备,我用嵌入式linux编程,我想同时和异步地通过这两个端口进行通信。

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? 使用termios结构和c_cflags来区分端口? 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. 你可以看到它有点含糊不清,我只是潜入水中并且用这种方式弄湿了,任何一般帮助指向我的方向都会有所帮助。

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

Please see a related answer to configure the port to the desired speed, parity, and i/o blocking characteristics. 请参阅相关答案 ,将端口配置为所需的速度,奇偶校验和I / O阻塞特性。

Even if the hardware has 4 or 24 serial ports, proper handling is to treat each individually and independently. 即使硬件具有4个或24个串行端口,正确的处理方法是单独和独立地处理每个端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM