【问题标题】:How to make Scilab open a serial communication with /dev/ttyACM0 USB port in Linux (Ubuntu)如何使 Scilab 在 Linux (Ubuntu) 中打开与 /dev/ttyACM0 USB 端口的串行通信
【发布时间】:2013-03-20 15:28:02
【问题描述】:

我正在尝试打开Scilab 和 Arduino 之间的串行通信。但是,Linux Ubuntu 始终在 /dev/tty**ACM0** 端口中识别 Arduino。当我在 Scilab 中写 h=openserial(1,"9600,n,8,1) 时,我知道我是在对它说,在 Linux 中打开与 COM1/dev/tty**S0** 的串行通信。

但是,例如,如果我使用h=openserial(N,"9600,n,8,1),假设N=port number,在Windows 中我将始终使用COMN,在Linux 中使用/dev/tty**S**(N-1)

如何在 Scilab for Linux 中通过/dev/tty**ACM0** 端口打开串行通信?

【问题讨论】:

  • 我从头重写了串行通信库,使其与MATLAB功能相媲美,请找到原型here

标签: serial-port arduino communication scilab


【解决方案1】:

查看来自Serial Communication Toolbox for Scilab 存储库的openserial.sci

function h=openserial(p,smode,translation,handshake,xchar,timeout)
//port name
  if ~exists("p","local") then p=1; end
  if type(p)==1 | type(p)==8 then
    if p<=0 then error("port number must be greater than zero"); end
    if getos() == "Windows" then
      port="COM"+string(p)+":"
    else
      port="/dev/ttyS"+string(p-1)
    end
  elseif type(p)==10
     port=p
  else
     error("port to open must be either a number or a string")
  end

端口始终设置为/dev/ttyS&lt;PORT_NUMBER&gt;。因此,在您的本地工具箱文件中,您可以尝试将openserial.sci 中的以下行编辑为如下内容:

function h=openserial(p,smode,translation,handshake,xchar,timeout)
//port name
  if ~exists("p","local") then p=1; end
  if type(p)==1 | type(p)==8 then
    if p<=0 then error("port number must be greater than zero"); end
    if getos() == "Windows" then
      port="COM"+string(p)+":"
    else
      port="/dev/ttyS"+string(p-1)
    end
  elseif type(p)==10
     port=p
  elseif type(p)=="ACM0"
     port="/dev/ttyACM0"
  else
     error("port to open must be either a number or a string")
  end

然后调用openserial如下:

h=openserial("ACM0","9600,n,8,1)

还要确保/dev/ttyACM0 是正确的设备节点。这是来自ls -l 的示例输出,您可以运行以确认:

$ ls -l /dev/ttyACM0
crw-rw---- 1 root dialout 188,  0 Mar 12 18:16 /dev/ttyACM0

如果您以普通用户身份打开串行端口时遇到错误,您可以将自己添加到正确的组中。根据上面的例子,在我的 openSUSE 发行版上,组名是dialout。您的可能会有所不同,因此请在以下命令中替换该组名:

sudo usermod -a -G dialout <USER_NAME>

【讨论】:

  • Tuxdude,我按照你的建议做了,我收到了以下信使:!--错误 999 TCL_EvalStr,在第 1 行无法打开“ACM0”:执行“打开 ACM0”时没有这样的文件或目录r+”从函数openserial的第17行的“set porthandle [open ACM0 r+]”中调用:h=openserial("ACM0","9600,n,8,1")
  • @FabioSilva - 你确定你的串口设备节点是/dev/ttyACM0 吗?
  • @FabioSilva - 更新了答案,提供了有关普通用户可能存在的权限问题的更多信息。但是你的错误信息no such file or directory表示设备节点不正确。
  • 即使执行了所有步骤,我仍然收到相同的错误消息。为了解决我的问题并满足我的需求,我现在使用 Octave,它运行良好。我认为这确实是 Scilab 或串行工具箱的错误。无论如何,谢谢你的帮助。问候。
  • 知道 scilab 在哪里安装 atom 吗?
【解决方案2】:

只需输入:

h = openserial("/dev/ttyACM0", "9600, n, 8, 1");

你就完成了。

【讨论】:

    【解决方案3】:

    保持简单,STRINGS 是一个有效的移植选项,正如 Luis 的帖子:

    "...只需输入:

    h = openserial("/dev/ttyACM0", "9600, n, 8, 1");
    

    你已经完成了......”

    例如,假设您的 arduino 在 Scilab 上使用串行端口“/dev/ttyACM0”类型:

    n=300 // plot 300 data points from serial port "/dev/ttyACM0"
    h=openserial("/dev/ttySACM0","9600,n,8,1")
    i=1;
    while i<=n
    data(i) = strtod(readserial(h)); // char to number
    plot(i,data(i),'b-o'); // real time plot
    drawnow(); // show data
    i=i+1;
    end
    

    【讨论】:

      猜你喜欢
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      相关资源
      最近更新 更多