Pages: [1]   Go Down
Author Topic: strange behaviour of Prolific PL2303 Ubuntu vs Arduino Mega  (Read 440 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also described on:
http://stackoverflow.com/questions/15887418/prolific-pl2303-ubuntu-vs-arduino-mega

 am testing Digitus USB -> RS232 converter, which is using Prolific PL2303 chip. The aim is to connect to GSP module U-blox LEA 6H. I am working on Ubuntu 12.10. To check if I can run the converter correctly I wired it up to Arduino Mega Serial Port 1.

I have two devices connected:
Code:
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

The first one is Arduino on /dev/ttyUSB0, the converter on /dev/ttyUSB1. When I use the code below with /dev/ttyUSB0, it works perfectly and I get on serial port monitor numbers 0...118. However, when I change it to ttyUSB1, the outcome is somewhat different. I am sorry for the mess in the code, it is compiled from 3 different tutorials.

The code for PC:

Code:
#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 strange output:

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 95 93 91 89 87 85 73 53 33

It seems that the outcome is the input multiplied by two plus one. All configuration is correct, please go for more sophisticated answers than "Is baudrate the same in PC and Arduino?".
Logged

Pages: [1]   Go Up
Jump to: