如何在Mac插件中使用ioctl()设置RTS? [英] How can I set the RTS with ioctl() in a Mac plugin?

查看:101
本文介绍了如何在Mac插件中使用ioctl()设置RTS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在一个小型终端应用程序中使用ioctl来设置RTS,但不能在我的Mac插件代码中进行设置,尽管两者都运行相同的代码.在插件中,我只能获取"串行端口标志/引脚,而不能设置"它们.在终端应用程序中,我既可以获取并设置"它们,也可以得到ENODEV的错误信息.错误号为19,消息为设备不支持该操作."

I am able to set the RTS with ioctl in a small Terminal app, but not in my Mac plugin code, although both run the same code. In the plugin I can only "get" the serial ports flags/pins, but not "set" them. In the Terminal app I can both "get and "set" them. I get an errno of ENODEV. The error number is 19 and the message is "Operation not supported by device."

如果这是一个安全问题(在浏览器的上下文中),是否有办法获得使用ioctl修改标志的权限?我有一个连接到USB端口的串行设备.我正在使用 FTDI vcp(虚拟 com 端口)驱动程序.在Windows方面一切都顺利.顺便说一句,我同时使用Safari和Firefox会得到相同的结果.下面是我的代码:

If this is a security issue (being in the context of a browser) is there a way to get permission to modify the flag with ioctl? I have a serial device attached to a usb port. Am using the FTDI vcp (virtual com port) driver. Everything smooth on the Windows side. Btw, I get the same result using both Safari and Firefox. Below is my code:

int disableRTS ()
{
    char fd, ret, flags;

    // open device
    if ((fd = open("/dev/cu.mydevice", O_RDWR | O_NDELAY)) < 0)
    {
        fprintf(stderr, "failed to open device");
        return -1;
    }

    // Get the current state of the bits
    ioctl(fd, TIOCMGET, &flags);
    fprintf(stderr, "Flags are %x.\n", flags);    

    flags &= ~TIOCM_RTS;  // Disable the RTS bit
    ret = ioctl(fd, TIOCMSET, &flags);

    if (ret == -1)
        fprintf(stderr, "TIOCMSET failed\n");
    else
        fprintf(stderr, "TIOCMSET succeeded. flags: %x.\n", flags);

    return 0;
}

=========

=========

如果刷新浏览器页面,强制再次执行代码,则ioctl()返回0,表示成功.不幸的是,我需要它第一次工作.即使我编写一个循环并使用usleep()方法暂时暂停并进行后续尝试,它也会失败.但是,当我刷新时,它会成功.我还在Mozilla提供的第二个专门用于NPAPI的项目"BasicPlugin.xcodeproj"中重复了该问题.我的第一个插件项目是Firebreath项目.它们首先都失败,然后在页面重新加载上成功.我也有2个单独的Mac应用程序,它们可以正常运行.一种是SerialTools,它使用与我的终端应用程序和插件完全相同的方法来设置RTS开启(和DTR关闭).

If I refresh the browser page, forcing the code to be executed again, ioctl() returns 0, indicating success. Unfortunately, I need it to work the first time. Even if I write a loop and pause momentarily, using the usleep() method, and make subsequent tries, it fails. But then, when I refresh it succeeds. I've also duplicated the issue in a second, exclusively NPAPI project "BasicPlugin.xcodeproj", supplied by Mozilla. My first plugin project is a Firebreath project. They both fail at first, then succeed on page reload. I also have 2 separate Mac apps that work properly. One is SerialTools, and it uses the exact same method of setting the RTS on (and DTR off) as the my Terminal app and the plugins.

========

我已经获得了代码级的Apple支持,因此可能会推出解决方案 .工程师说,代码在插件中的运行方式不同于插件中的运行方式是怪诞"的,并且正在与Safari插件工程师交流.

I've been able to get code-level Apple support on this, so a solution may be coming. The engineer said it was "bizarre" that the code runs differently in a plugin than outside of one, and is speaking with Safari plugin engineers.

推荐答案

答案是,无论何时在对ioctl()的调用中使用TIOCMSET或TIOCMGET时,第三个参数都必须为int.我正在使用一个字符.咄.简直不敢错过.TIOCMSET和TIOCMGET的定义如下:

The answer is that whenever TIOCMSET or TIOCMGET are used in a call to ioctl() the 3rd parameter must be an int. I was using a char. Duh. Can't believe I missed this. TIOCMSET and TIOCMGET are defined as follows:

  #define   TIOCMSET    _IOW('t', 109, int) /* set all modem bits */
  …
  #define   TIOCMGET    _IOR('t', 106, int) /* get all modem bits */

因此,不难想象使用char作为我的"flags" var类型可能会导致不可预测的行为.

and so it is not difficult to imagine that using a char as the type for my "flags" var might cause unpredictable behavior.

这篇关于如何在Mac插件中使用ioctl()设置RTS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆