Pages: [1]   Go Down
Author Topic: Problems with Serial communication  (Read 484 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi There

 I'm new in the Arduino world and i try to send serial data via a c++ comandline application (from a mac) to the arduino. I try to send 8 digits in a string and display them on 8 7segment displays. everything works fine as long i send the string direct from the arduino serial monitor. wen i try to send the data from the c++ app the leds on the arduino board (mega2560) starts to blink (like the board receives data) but nothing happens on the displays. as soon as i open up the serial monitor in arduino, everything works fine again, and the data from the c++ app is displayed on the 7segments.

can anyone help me with that please

thanks

pb
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but nothing happens on the displays. as soon as i open up the serial monitor in arduino, everything works fine again
Your C++ application is not setting all the attributes of the serial port correctly. The Serial Monitor does. Opening it corrects the mismatch, and allows data to be sent properly.

All you need to do is set the serial port attributes correctly.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your quick answer. i can connect to the serial port via two ways:

1 /dev/tty.usbmodemfa121
2 /dev/cu.usbmodemfa121

i am not shure wich one are the right, but both react as the same. i remember that i never had this problem before with my old arduino nano board.

thx

pb
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i can connect to the serial port via two ways:
Which is, of course, irrelevant. What is relevant is the code you are using.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the code on my mac comandline tool looks like this:
Code:
#include <iostream>
#include <fcntl.h> 
#include <termios.h>

const int BUFFER_SIZE = 8;
int output_buffer[BUFFER_SIZE];
char output_buffer2[BUFFER_SIZE][1];
int sendHDG = 1612;

using namespace std;

void init_port(int *fd, unsigned int baud)
{
    struct termios options;
    tcgetattr(*fd,&options);
    switch(baud)
    {
case 9600: cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
break;
case 19200: cfsetispeed(&options,B19200);
cfsetospeed(&options,B19200);
break;
case 38400: cfsetispeed(&options,B38400);
cfsetospeed(&options,B38400);
break;
default:cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
break;
    }
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    tcsetattr(*fd,TCSANOW,&options);
}

int main()
{
int fd;
fd = open("/dev/tty.usbmodemfa121", O_RDWR | O_NOCTTY | O_NDELAY);
        if(fd == -1)
perror("open_port: unable to open port");

init_port(&fd,9600);         //set serial port to 9600,8,n,1
        write(fd, "12345678", BUFFER_SIZE);

return (0);
}


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, after some hours of searching, i finally find the solution:

1. Disable Auto Reset
2. Give some Delay after the port is open

http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection
http://arduino.cc/forum/index.php?topic=50484.0

thx

pb
« Last Edit: January 07, 2012, 06:18:02 pm by pepperbilly » Logged

Pages: [1]   Go Up
Jump to: