1

I'd like a little clarification on what socat does to the serial communication, because for some reason I can only get the communication to my Arduino to work when using socat.

This is the command I use to set up socat:

socat -d -v -x PTY,link=/tmp/serial,wait-slave,raw /dev/ttyACM0,raw

The code I used in C++ is:

int file;

file = open("/tmp/serial", O_RDWR | O_NOCTTY | O_NDELAY);
if (file == -1)
  {

    perror("open_port: Unable to open \n");
  }
  else
    fcntl(file, F_SETFL, 0);

struct termios options;


 //* Get the current options for the port...


tcgetattr(file, &options);

cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);

options.c_oflag = 0;
//options.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OLCUC | OPOST);


options.c_cflag |= (CLOCAL | CREAD);

cfmakeraw(&options);

tcsetattr(file, TCSANOW, &options);

sprintf(sendpack,"$C000C000C000C000\r");
num = write(file,sendpack,18);  
tcflush(file, TCIOFLUSH); // clear buffer

So when I run this through socat the arduino reacts as it is supposed to, but if I change the open() line from /tmp/serial to /dev/ttyACM0 the arduino no longer performs at all. Can someone exlplain to me what socat is doing to the data to make things work, and how I can change my C code to do the same thing? I cannot use socat in my final implementation because it causes a delay that would make driving my robot unpredictable.

user2744971
  • 123
  • 1
  • 2
  • 8
  • 1
    Try to open port at first via `socat` and see, what `stty -F /dev/ttyACM0` says and then the same, but via opening `/dev/ttyACM0` directly. Also try this call `void cfmakeraw(struct termios *termios_p);` after `tcgetattr()`. – yegorich Dec 18 '13 at 20:53
  • 1
    Yes, that should reveal it, though you may want to do this from another terminal *while* socat is running. I know at least one program which on exit returns the serial port to the settings that were in effect before it was run. – Chris Stratton Dec 18 '13 at 20:55
  • thanks for the tip using stty. however, something else must be different because when I set up the device with the same settings socat had, it still doesn't work. What settings could socat be changing that aren't displayed in stty? – user2744971 Dec 19 '13 at 19:14
  • @user2744971 have you tried cfmakeraw() after tcgetattr() and not just before tcsetattr() as I mentioned in my first comment? As you can see socat makes raw mode for the seeial port: `/dev/ttyACM0,raw` – yegorich Dec 20 '13 at 08:42

0 Answers0