Hello I am having some trouble trying to program an Arduino to take commands from a c++ program. I am using termios to connect to the Arduino. The Arduino is in gateway mode (basically it is not executing code itself but is waiting for input from my program to interact with hardware I connect to it).
My termios set up is like this:
SerialHandler::SerialHandler(char* fPath, int bRate)
{
filePath = fPath;
baudRate = 0;
//Open the file, file is of type int
file = open(filePath, O_RDWR | O_NOCTTY);
if(file<0) //If there is an error opening the file
{
perror(filePath);
exit(-1);
}
//Save the old port settings
tcgetattr(file, &oldtio);
bzero(&newtio, sizeof(newtio));
//now to load the baudrate
getBaudRate(bRate, baudRate);
newtio.c_cflag = baudRate | CRTSCTS | CS8 | CLOCAL | CREAD;
//IGNPAR ignore bits with parity errors
//ICRNL map CR to NL ..May have to change to raw input processing
newtio.c_iflag = IGNPAR;
//Raw output
newtio.c_oflag = 0;
//ICANON - enable canonical input
//disables echo functionality, doesnt send signals to calling program
newtio.c_lflag = ICANON;
//Clean and activate port
tcflush(file, TCIFLUSH);
tcsetattr(file, TCSANOW, &newtio);
}
And my code to write and read to the Arduino is this:
void SerialHandler::getSerialOutput(char* readBuffer, int& bufferPoint)
{
cout <<"Beginning read\n";
bufferPointer = read(file, outputBuffer,255);
cout <<"Finished Read\n";
for(int i=0;i<bufferPointer;i++)
{
cout << outputBuffer[i]<<endl;
readBuffer[i] = outputBuffer[i];
}
bufferPoint = bufferPointer;
}
void SerialHandler::writeSerial(char* writeBuffer, int length)
{
cout << "Writing: " << writeBuffer<<endl;
write(file,writeBuffer,length);
cout << "Finished Write \n";
}
Initially I send the Arduino a test command ("AT\r\n") and it responds with ("OK") but after this any subsequent read/writes fail.
On the test command the Arduino's RX and TX pins light up (meaning it is recieving and transmitting data) but and commands I send after that fail. The Arduino does not light up when I try to write to it and any read commands hang indefinitely.
I think the problem is something like the file handler closing but I am unsure of how to test this or it could be another problem altogether.
write()
, handle partial writes. – user529758BXXXX
constants and is not arbitrary? – user529758