2014-06-10 158 views
0

所以我在一边有USB端口和转换器电缆,另一端有RS232电缆。 我尝试使用的CreateFile()API COM口,到目前为止,我成功地做到这一点:如何使用USB转RS232转换器打开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; 
    } 

,但我得到存取遭拒错误。我现在被卡住了,因为我是C编程和设备通信的新手。

在虚拟COM端口的情况下调用CreateFile API之前需要什么步骤?

+1

你的'devicePath'的价值是什么? – rodrigo

+0

值为“COM1”。 – user3139293

+0

您如何知道USB通讯端口是“COM1”而不是“COM3”等? – chux

回答

0

第一种方式可以是这样的,假设你的串行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串口转换器在我的办公室,目前不在我身边,所以我明天就能检查,

+3

“假设你的串行USB转换器创建了'/ dev/ttyUSB0'”这个假设很可能是错误的,因为OP使用'CreateFile()',这似乎是Windows的一部分。 – glglgl

+0

@glglgl哦,真的吗?我没有标记,我不是Windows开发人员,你会建议我删除我的答案吗? –