I am using C in Linux for serial port communication. I looked up online and saw that many people use the system calls to finish this task: open(), to open the serial port; write(), to write to the buffer and send to the port; read(), to read from the buffer. I was able to using open() to open a port (“/dev/ttyACM0”) successfully, and the function returns the correct value for the file descriptor (23, in our case). I set up the port as most people did online, and Ill attach that part of the code at the end. However, when we tried to use write() function to simply pass the string testBuffer = “12” to the port, an error message – “broken pipe” pops up. The three parameters for the write function are file descriptor value(fd), content(testbuffer), and the size of the content (sizeof(testBuffer)). I have allocated the space for the testBuffer, so the buffer should not be the problem. I can manually send data to the port to the Arduino by using command “echo” in the terminal, so the other end of the connection should not be the problem neither. Then, a grad student told me that I might need to manually make the second argument of the function to the packet protocol that the port can recognize it. Therefore, I tried to make testBuffer to 8 hex numbers array, then the error message becomes “Bad destination address”. So right now I’m lost about where the error exactly is.
/* allow the process to receive SIGIO /
fcntl(port, F_SETOWN, getpid());
/ Make the file descriptor asynchronous (the manual page says only
O_APPEND and O_NONBLOCK, will work with F_SETFL...) /
fcntl(port, F_SETFL, FASYNC);
/
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
cfsetispeed(&specs, B9600);
cfsetospeed(&specs, B9600);
specs.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD;
/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
/
specs.c_iflag = IGNPAR | ICRNL;
/
Raw output.
/
specs.c_oflag = 0;
/
ICANON : enable canonical input
disable all echo functionality, and don't send signals to calling program
*/
specs.c_lflag = ICANON;
// Set the timeouts
// VMIN is the minimum amount
// of characters to read.
specs.c_cc[VMIN]=1;
// The amount of time to wait
// for the amount of data
// specified by VMIN in tenths
// of a second.
specs.c_cc[VTIME]=0;
tcflush(port, TCIFLUSH);
tcsetattr(port, TCSANOW, &specs);
Attach is a file of the screenshots that I took that shows the error. May need to run Linux to run this. Just cd to the folder's directory and type make. Afterwards, run X-Plane and the error should pop up.
testing.zip (465 KB)