简体   繁体   English

使用USB串行转换器通过Mac OS X与电源通信

[英]Talking to a power supply via mac os x using a usb-serial converter

I am trying to talk to a power supply from my mac OS X Yosemite. 我正在尝试与Mac OS X Yosemite的电源进行通讯。 The code works fine on a linux machine, but when I try it on my mac, it does not work. 该代码在linux机器上可以正常工作,但是当我在mac上尝试时,它不起作用。 I am using a usb-serial converter and have downloaded the PL-2303 driver. 我正在使用USB串行转换器,并已下载PL-2303驱动程序。 The driver shows up in my /dev folder as cu.usbserial and tty.usbserial. 驱动程序在我的/ dev文件夹中显示为cu.usbserial和tty.usbserial。

The part of my code that fails: 我的代码中失败的部分:

fd = initserial("/dev/cu.usbserial");

int initserial(char port[])
{
  struct termios shimtermios;
  int fd;

  if((fd=open(port,O_RDWR)) < 0)  {
    perror("Opening the RS-232 port failed for initserial\n");
    exit(-1);
  }

  if (tcgetattr(fd, &shimtermios) < 0) { 
    perror("couldn't get terminal attributes\n");
    exit(-2);
  }

  shimtermios.c_iflag=012005;
  shimtermios.c_oflag=014004;
  shimtermios.c_cflag=03206276;
  shimtermios.c_lflag=05060;
  cfsetospeed(&shimtermios,B19200);

  if (tcsetattr(fd, TCSAFLUSH, &shimtermios) < 0) {
    perror("couldn't set terminal attributes\n");
    exit(-3);
  }
  return (fd);
}

I get the following error message 我收到以下错误消息

couldn't set terminal attributes
: Invalid argument

Please let me know if you have any experience with this linux/unix issue. 如果您对此linux / unix问题有任何经验,请告诉我。 Thank you so much! 非常感谢!

See the termios man page for the valid masks that can be used for c_iflag , c_oflag , c_cflag , and c_lflag ). 有关可用于c_iflagc_oflagc_cflagc_lflag的有效掩码,请参见termios手册页 You should OR the mask constants together rather than hard-coding the values. 您应该将掩码常量或在一起,而不是对值进行硬编码。

Also from experience, FTDI USB-serial converters tend to work better on OS X than pl2303. 同样从经验来看,FTDI USB串行转换器在OS X上的性能往往优于pl2303。 OS X has a built-in driver for these (AppleUSBFTDI). OS X具有用于这些的内置驱动程序(AppleUSBFTDI)。

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

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