[C++]Przesy

Potrzebujesz pomocy z C, C++, perl, python, itp.
Tomass
Posty: 1
Rejestracja: 20 lutego 2014, 16:01

[C++]Przesyłanie informacji przez USB (SerialPort)

Post autor: Tomass »

Witam, mam pytanie,
Przesyłam informację poprzez port USB z Arduino UNO R3 do komputera z Linux'em x86, noi raz działa, a raz nie.
PC LINUX x86 [C++ KOD]:

Kod: Zaznacz cały

//C++ (LINUX PC x86)
 
#include <unistd.h>
#include <sstream>
#include <string>
#include <iostream>
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
 
/* My Arduino is on /dev/ttyACM0 */
char *portname = "/dev/ttyACM0";
char buf[256];
 
int main(int argc, char *argv[])
{
    while(true)
    {
        int fd;
 
        /* Open the file descriptor in non-blocking mode */
        fd = open(portname, O_RDWR | O_NOCTTY);
 
        /* Set up the control structure */
        struct termios toptions;
 
        /* Get currently set options for the tty */
        tcgetattr(fd, &toptions);
 
        /* Set custom options */
 
        /* 9600 baud */
        cfsetispeed(&toptions, B9600);
        cfsetospeed(&toptions, B9600);
        /* 8 bits, no parity, no stop bits */
        toptions.c_cflag &= ~PARENB;
        toptions.c_cflag &= ~CSTOPB;
        toptions.c_cflag &= ~CSIZE;
        toptions.c_cflag |= CS8;
        /* no hardware flow control */
        toptions.c_cflag &= ~CRTSCTS;
        /* enable receiver, ignore status lines */
        toptions.c_cflag |= CREAD | CLOCAL;
        /* disable input/output flow control, disable restart chars */
        toptions.c_iflag &= ~(IXON | IXOFF | IXANY);
        /* disable canonical input, disable echo,
        disable visually erase chars,
        disable terminal-generated signals */
        toptions.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
        /* disable output processing */
        toptions.c_oflag &= ~OPOST;
 
        /* wait for 24 characters to come in before read returns */
        toptions.c_cc[VMIN] = 5;
        /* no minimum time to wait before read returns */
        toptions.c_cc[VTIME] = 0;
 
        /* commit the options */
        tcsetattr(fd, TCSANOW, &toptions);
 
        /* Wait for the Arduino to reset */
        usleep(1000*1000);
        /* Flush anything already in the serial buffer */
        tcflush(fd, TCIFLUSH);
        /* read up to 128 bytes from the fd */
        int n = read(fd, buf, 5);
 
        /* print how many bytes read */
        printf("%i bytes got read...\n", n);
        /* print what's in the buffer */
        printf("Buffer contains... \n%s\n", buf);
 
        //std::string str(buf);
        //std::cout << str << std::endl;
    }
    return 0;
}
Arduino UNO R3 [KOD]:

Kod: Zaznacz cały

//(Arduino UNO)
void setup()
{
        Serial.begin(9600);
}
void loop()
{
        Serial.print("12345");
        delay(1000);
}

Wynik:

Kod: Zaznacz cały

//Wyniki
5 bytes got read...
Buffer contains... 
12345
5 bytes got read...
Buffer contains... 
51234
5 bytes got read...
Buffer contains... 
45123
5 bytes got read...
Buffer contains... 
34512
5 bytes got read...
Buffer contains... 
23451
5 bytes got read...

Buffer contains... 
23451
5 bytes got read...
Buffer contains... 
12345

Wiecie dlaczego nie uzyskuję za każdym razem informacji "12345", i w jaki sposób to poprawić?
ODPOWIEDZ