【问题标题】:How to open COM port using USB to RS232 converter?如何使用 USB 转 RS232 转换器打开 COM 端口?
【发布时间】:2014-06-10 09:59:02
【问题描述】:

所以我一侧有 USB 端口和转换器电缆,另一侧有 RS232 电缆。 我正在尝试使用 CreateFile() API 打开 COM 端口,到目前为止我设法做到了:

HANDLE dev = CreateFile(devicePath, (GENERIC_READ | GENERIC_WRITE), 0,  NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    ShowError("connectCOM()");
    if (dev == INVALID_HANDLE_VALUE)
    {
        return false;
    }

但我收到 AccessDenied 错误。我现在卡住了,因为我是 C 编程和设备通信的新手。

如果是虚拟 COM 端口,调用 CreateFile API 之前的必要步骤是什么?

【问题讨论】:

  • 你的devicePath的价值是多少?
  • 怎么知道USB com口是“COM1”而不是“COM3”等?
  • 我在设备管理器中查到了。我没有弄清楚如何以编程方式检测它。也许你有一个想法?

标签: c port usbserial


【解决方案1】:

第一种方式可能是这样,假设您的串行 USB 转换器创建/dev/ttyUSB0,它还涉及设置端口的系统调用:

int usbdev;
char command[10]; 
char response[10];
system("stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parity -icanon min 1 time 1");
usbdev = open("/dev/ttyUSB0", O_RDWR);
write(usbdev, command, 2);
read(usbdev, response, 1);
close(usbdev);

另一种实现方式,

#include <stdio.h>      // standard input / output functions
#include <stdlib.h>
#include <string.h>     // string function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <termios.h>    // POSIX terminal control definitions

int main()
{
    /* Open File Descriptor */
    int USB = open( "/dev/ttyUSB0", O_RDWR | O_NDELAY );

    /* Error Handling */
    if ( USB < 0 )
    {
        //cout << "Error " << errno << " opening " << "/dev/ttyUSB0" << ": " << strerror (errno) << endl;
        perror("USB ");
    }

    /* *** Configure Port *** */
    struct termios tty;
    memset (&tty, 0, sizeof tty);

    /* Error Handling */
    if ( tcgetattr ( USB, &tty ) != 0 )
    {
        //cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << endl;
        perror("tcgerattr ");
    }

    /* Set Baud Rate */
    cfsetospeed (&tty, B9600);
    cfsetispeed (&tty, B9600);

    /* Setting other Port Stuff */
    tty.c_cflag     &=  ~PARENB;        // Make 8n1
    tty.c_cflag     &=  ~CSTOPB;
    tty.c_cflag     &=  ~CSIZE;
    tty.c_cflag     |=  CS8;
    tty.c_cflag     &=  ~CRTSCTS;       // no flow control
    tty.c_lflag     =   0;          // no signaling chars, no echo, no canonical processing
    tty.c_oflag     =   0;                  // no remapping, no delays
    tty.c_cc[VMIN]      =   0;                  // read doesn't block
    tty.c_cc[VTIME]     =   5;                  // 0.5 seconds read timeout

    tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines
    tty.c_iflag     &=  ~(IXON | IXOFF | IXANY);// turn off s/w flow ctrl
    tty.c_lflag     &=  ~(ICANON | ECHO | ECHOE | ISIG); // make raw
    tty.c_oflag     &=  ~OPOST;              // make raw

    /* Flush Port, then applies attributes */
    tcflush( USB, TCIFLUSH );

    if ( tcsetattr ( USB, TCSANOW, &tty ) != 0)
    {
        //cout << "Error " << errno << " from tcsetattr" << endl;
    }

    /* *** WRITE *** */

    unsigned char cmd[] = {'I', 'N', 'I', 'T', ' ', '\r', '\0'};
    //int n_written = write( USB, cmd, sizeof(cmd) -1 );

    /* Allocate memory for read buffer */
    char buf [256];
    memset (&buf, '\0', sizeof buf);

    /* *** READ *** */
    int n = read( USB, &buf , sizeof buf );

    /* Error Handling */
    if (n < 0)
    {
        //cout << "Error reading: " << strerror(errno) << endl;
        perror("read error ");
    }

    /* Print what I read... */
    //cout << "Read: " << buf << endl;
    printf("%s",buf);;

    close(USB);
}

我的USB串口转换器在我的办公室,暂时不在我身边,所以我明天可以检查一下,

【讨论】:

  • "假设您的串行 USB 转换器创建 /dev/ttyUSB0" 这个假设可能是错误的,因为 OP 使用 CreateFile(),这似乎是 Windows 的一部分。
  • @glglgl 哦,真的吗?我没有标记,我不是windows开发人员,你会建议我删除我的答案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
相关资源
最近更新 更多