[英]opening a usb-serial port using the device VID/PID
In Linux usb-serial converters usually show up as a node in the /dev directory: /dev/ttyUSBx. 在Linux中,USB串行转换器通常在/ dev目录:/ dev / ttyUSBx中显示为一个节点。
To use the serial converter the first step it to open the port, then configure it and so on. 要使用串行转换器,第一步是打开端口,然后进行配置,依此类推。
port = open("/dev/ttyUSB0", O_RDWR);
If you want to use a serial device (I2C or SPI), ftdi offers devices (like FT4232 or FT232h) which can be used either as a normal UART port or i2c/spi. 如果要使用串行设备(I2C或SPI),则ftdi提供了可以用作普通UART端口或i2c / spi的设备(如FT4232或FT232h)。
For i2c/spi operation you have to use a separate driver - I use the open source libmpsse . 对于i2c / spi操作,您必须使用单独的驱动程序-我使用开源libmpsse 。 This is a library you have to install so it will work in parallel with the standard FTDI driver since it's build on top of that.
这是您必须安装的库,因此它基于此库而与标准FTDI驱动程序并行工作。
So now, if I want o open a port as UART I use the normal open function (mentioned above). 因此,现在,如果我想打开端口作为UART,则可以使用常规打开功能(如上所述)。 And if I want to connect a i2c/spi device I use the libmpsse open function which opens the port based on VID/PID:
如果要连接i2c / spi设备,请使用libmpsse open函数,该函数根据VID / PID打开端口:
struct mpsse_context *Open(int vid, int pid, enum modes mode, int freq, int endianess, int interface, const char *description, const char *serial)
Now for the question - can I open the port as UART by using the device vid/pid instead of the path to it's dev mode? 现在要问的问题-我可以使用设备vid / pid代替其开发模式的路径来将端口作为UART打开吗? It all comes down to ftdi function calls but I can't seem to find an example.
全部归结为ftdi函数调用,但我似乎找不到一个示例。
Why I need to do it this way? 为什么我需要这样做? I don't want to have to know the node path.
我不想知道节点路径。 I should be able to use just the VID/PID and interface number - it's a lot more flexible.
我应该能够只使用VID / PID和接口号-灵活得多。
Any help is appreciated! 任何帮助表示赞赏!
I eventually found a solution so I'm posting for anyone that might need this. 我最终找到了一个解决方案,所以我为可能需要此服务的任何人发布信息。
You can open the serial port by using one of the ftdi_usb_open_xxx()
functions. 您可以使用
ftdi_usb_open_xxx()
函数之一打开串行端口。 For me, ftdi_usb_open_desc_index
did the trick; 对我来说,
ftdi_usb_open_desc_index
。 index is useful in case you have more than one chip of the same kind connected. 如果您连接了多个相同类型的芯片,则index很有用。
Then you configure the port with ftdi_set_baudrate
and ftdi_set_line_property
. 然后,使用
ftdi_set_baudrate
和ftdi_set_line_property
配置端口。 And read/write with ftdi_read_data()
/ ftdi_write_data()
. 并使用
ftdi_read_data()
/ ftdi_write_data()
读写。
Here's a short example: 这是一个简短的示例:
struct ftdi_context ftdic;
// ftdilib initialization
if(ftdi_init(&ftdic) == 0)
{
ftdi_set_interface(&ftdic, IFACE_C);//this is where my serial device is connected
if(ftdi_usb_open_desc_index(&ftdic, vid, pid, NULL, NULL, 0) == 0)
{
printf("serial port open\n");
if(ftdi_set_baudrate(&ftdic, 9600) < 0)
{
printf("baudrate incorrect\n");
}
if(ftdi_set_line_property(&ftdic, BITS_8, STOP_BIT_1, NONE)<0)
{
printf("line settings incorrect");
}
for(;;)
{
unsigned char c;
ftdi_read_data(&ftdic, &c, 1);
printf("0x%2x ",c);
}
}
else
{
printf("could not open serial port \n");
}
}
else
{
printf("init failed\n");
}
This way you don't seem to have to wait for data to be available so you won't have to deal with blocking ports. 这样,您似乎不必等待数据可用,因此您不必处理阻塞端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.