0
votes

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.

1
Always check the return value of write(), handle partial writes.user529758
Hi do you mean assign the return value to an int ? I just tried that and the result was 4 for the command that worked and 10 for the other command. Which makes sense because the first command is 4 chars long and the second is 10 but the program is still not actually writing the command to the Arduino.scrineym
seems like a baudrate issue. How do you obtain baud rate info? You know that the baudrate argument needs to be one of the BXXXX constants and is not arbitrary?user529758
Yep the getBaudRate() method figures out which baud rate is necessary from bRate which is passed in, and then sets the variable baudRate to the correct BXXXX.scrineym

1 Answers

0
votes

Try to remove CRTSCTS from newtio.c_cflag line. CRTSCTS is needed when you connect all the cables of the serial connection. When using Arduino you connect only tx rx and gnd lines. See termios man page for more information http://linux.die.net/man/3/termios