1

我正在测试使用 Prolific PL2303 芯片的 Digitus USB -> RS232 转换器。目的是连接到 GSP 模块 U-blox LEA 6H。我正在开发 Ubuntu 12.10。为了检查我是否可以正确运行转换器,我将它连接到 Arduino Mega Serial Port 1。

我连接了两个设备:

Bus 005 Device 002: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 007 Device 003: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port

第一个是 /dev/ttyUSB0 上的 Arduino,/dev/ttyUSB1 上的转换器。当我将下面的代码与 /dev/ttyUSB0 一起使用时,它工作得很好,我得到了串行端口监视器号 0...118。但是,当我将其更改为 ttyUSB1 时,结果有些不同。我很抱歉代码中的混乱,它是从 3 个不同的教程编译而来的。

PC端代码:

#include <stdio.h>    /* Standard input/output definitions */
#include <stdlib.h>
#include <stdint.h>   /* Standard types */
#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 */
#include <sys/ioctl.h>
#include <getopt.h>
#include <iostream>

/* File descriptor for the port */
int fd;
int n;
uint8_t buffer[255];  /* Input buffer */
const char port_name[] = "/dev/ttyUSB0";

int serialport_init(const char* serialport, int baud)
{
    struct termios toptions;
    int fd;

    //fprintf(stderr,"init_serialport: opening port %s @ %d bps\n",
    //        serialport,baud);

    fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)  {
        perror("init_serialport: Unable to open port ");
        return -1;
    }

    if (tcgetattr(fd, &toptions) < 0) {
        perror("init_serialport: Couldn't get term attributes");
        return -1;
    }
    speed_t brate = baud; // let you override switch below if needed
    switch(baud) {
    case 4800:   brate=B4800;   break;
    case 9600:   brate=B9600;   break;
#ifdef B14400
    case 14400:  brate=B14400;  break;
#endif
    case 19200:  brate=B19200;  break;
#ifdef B28800
    case 28800:  brate=B28800;  break;
#endif
    case 38400:  brate=B38400;  break;
    case 57600:  brate=B57600;  break;
    case 115200: brate=B115200; break;
    }
    cfsetispeed(&toptions, brate);
    cfsetospeed(&toptions, brate);

    // 8N1
    toptions.c_cflag &= ~PARENB;
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag &= ~CSIZE;
    toptions.c_cflag |= CS8;
    // no flow control
    toptions.c_cflag &= ~CRTSCTS;

    toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
    toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl

    toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
    toptions.c_oflag &= ~OPOST; // make raw

    // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
    toptions.c_cc[VMIN]  = 0;
    toptions.c_cc[VTIME] = 20;

    if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
        perror("init_serialport: Couldn't set term attributes");
        return -1;
    }

    return fd;
}

int main(void){
    fd = serialport_init(port_name, 19200);
    n=-1;
    for(uint8_t i=0; i<255; i++){
        buffer[i]=i;
    }
    n = write(fd, buffer, 120);
    if(n<0){
        fputs("write() failed!\n", stderr);
        printf("ERROR");
        return -1;
    }
    close(fd);
    std::cout << "Port closed";
    return 0;
}

Arduino奇怪的输出:

253 251 249 247 245 243 241 239 237 235 233 231 229 227 225 223 221 219 217 215 213 211 209 207 205 203 201 199 197 195 193 191 189 187 185 183 181 179 177 175 173 171 169 167 165 163 161 159 157 155 153 151 149 147 145 143 141 139 137 135 133 131 129 127 125 123 121 119 117 115 113 111 109 107 105 103 101 99 97 378 93 931 538 38 58

结果似乎是输入乘以二加一。

4

0 回答 0