开发者

is this linux serial port configured the same as the windows one

I am trying to port a program to linux but i cannot get the serial port working.

this is the windows code

if( (idComDev[i] = CreateFile(ComStr,GENERIC_RE开发者_如何学编程AD | GENERIC_WRITE,0,0,OPEN_EXISTING,0,0)) != INVALID_HANDLE_VALUE )
    {
        SetupComm(idComDev[i],1024,1024);
        cto.ReadIntervalTimeout = MAXDWORD;
        cto.ReadTotalTimeoutMultiplier = 0;
        cto.ReadTotalTimeoutConstant = 0;
        cto.WriteTotalTimeoutMultiplier = 0;
        cto.WriteTotalTimeoutConstant = 0;
        SetCommTimeouts(idComDev[i],&cto);
        sprintf(ComStr,"COM%hd:19,n,8,1",CommNo[i]);
        BuildCommDCB(ComStr,&dcb);
        dcb.fBinary = TRUE;
        dcb.BaudRate = baud;
        dcb.fOutxCtsFlow = FALSE;               // CTS output flow control
        dcb.fOutxDsrFlow = FALSE;               // DSR output flow control
        dcb.fDtrControl = DTR_CONTROL_ENABLE;   // DTR line on
        dcb.fDsrSensitivity = FALSE;            // DSR sensitivity
        dcb.fTXContinueOnXoff = FALSE;          // XOFF continues Tx
        dcb.fOutX = FALSE;                      // XON/XOFF out flow control
        dcb.fInX = FALSE;                       // XON/XOFF in flow control
        dcb.fErrorChar = FALSE;                 // enable error replacement
        dcb.fNull = FALSE;                      // enable null stripping
        dcb.fRtsControl = RTS_CONTROL_DISABLE;  // RTS flow control
        dcb.fAbortOnError = FALSE;              // abort reads/writes on error
        dcb.wReserved = 0;                      // Not used; must be set to zero
        dcb.ByteSize = 8;                       // number of bits/byte, 4-8

        dcb.StopBits = ONESTOPBIT;              // 0,1,2 = 1, 1.5, 2
                    dcb.Parity = SPACEPARITY; // use parity as address bit
        if( CardType == 2 ) SetCommMask(idComDev[i],EV_TXEMPTY);
        SetCommState(idComDev[i],&dcb);
        dbprintf("Seg %d = COM%hd\r\n",i,CommNo[i]);
    }

and this is my linux code

idComDev[i] = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (idComDev[i] == -1)
    {
    perror("open_port: Unable to open /dev/ttyS0 - ");
    ret = false;
    }
    else
    {
    fcntl(idComDev[i], F_SETFL, 0);

    struct termios options;

    tcgetattr(idComDev[i], &options); // get current settings

    cfsetispeed(&options, B115200); // set baud rate
    cfsetospeed(&options, B115200); // set baud rate
    options.c_cflag |= (CLOCAL | CREAD);

    options.c_cflag &= ~PARENB; // set parity to no 
    options.c_cflag &= ~CSTOPB;//set one stop bit
    options.c_cflag &= ~CSIZE; // Mask the character size bits 
    options.c_cflag |= CS8; // 8 bit data

    options.c_lflag |= (ICANON);
    options.c_iflag &= ~(IXON | IXOFF | IXANY); //disable software flow controll

    tcsetattr(idComDev[i], TCSANOW, &options);// save the settings

whenever i try to write to the serial port it returns with -1 and gives me no information as to what went wrong. i tried using errorno and that said input/output error which is not helpful.

have i configured my serial port in the same way as it is in the original program ?

can anyone give advice on how to debug this problem as this is not my area of expertise


Perhaps one or more of the control lines are not set properly. Please look at tty_ioctl(4) section "Modem Control" for TIOCMSET and associated stuff. Your Windows code looks like it want to set DTR at least.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜