I'm trying to send data via serial connection (USB) from my PC (Ubuntu 14.04) to an Arduino Uno. The Arduino should display the received data for testing purposes. (I'm happy, if I receive anything...)
I use libserial to send the data but the Arduino receives nothing. With the help of the Arduino IDE I could send the data successfully to the Arduino. With normal console commands it is also possible to send the data.
Here is my Arduino code:
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("connecting...");
inputString.reserve(200);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
lcd.setCursor(0, 0);
lcd.print("successful connected");
}
void loop() {
lcd.setCursor(0, 1);
// print the string when a newline arrives:
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(inputString);
delay(500);
}
void serialEvent() {
if (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
}
}
And this the c++ code (on the PC side):
//Libserial: sudo apt-get install libserial-dev
#include <SerialStream.h>
#include <iostream>
#include <unistd.h>
using namespace LibSerial;
using namespace std;
int main(int argc, char** argv)
{
SerialStream my_serial_stream;
//
// Open the serial port for communication.
//
my_serial_stream.Open("/dev/ttyACM0");
my_serial_stream.SetBaudRate(SerialStreamBuf::BAUD_9600);
//my_serial_stream.SetVTime(1);
//my_serial_stream.SetVMin(0);
my_serial_stream.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
my_serial_stream.SetParity(SerialStreamBuf::PARITY_NONE);
my_serial_stream.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE);
my_serial_stream.SetNumOfStopBits(1);
int i = 0;
while(i<=5) {
usleep(1500000);
if (!my_serial_stream.good()) {
my_serial_stream << i << "\n" << endl;
cout << i << endl;
}
else {
cout << "serial is not good" << endl;
}
i++;
}
my_serial_stream.Close();
cout << "ready" << endl;
return 0;
}
Do you have any ideas why this doesn't work?
Thanks!
I found a solution, how to communicate with the Arduino via serial Port (USB). I don't use libserial.
I improved the Arduino code (display only, if there is a new line):
// include the library code:
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("connecting...");
inputString.reserve(200);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
lcd.setCursor(0, 0);
lcd.print("successful connected");
}
void loop() {
lcd.setCursor(0, 1);
if (stringComplete) {
// print the string when a newline arrives:
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(inputString);
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
if (Serial.available()) {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
}
C++ code:
//to compile use: g++ serial_success.cpp -o serial -std=c++11
//you might not need every inclusion
#include <iostream>
#include <unistd.h>
#include <string>
#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitionss
#include <time.h> // time calls
using namespace std;
#define BAUDRATE B9600
int main(int argc, char** argv)
{
int fileDescriptor = open("/dev/ttyACM0", O_RDWR | O_NOCTTY);
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
// set to 8N1
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
newtio.c_iflag = IGNPAR;
// output mode to
//newtio.c_oflag = 0;
newtio.c_oflag |= OPOST;
/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 10; /* inter-character timer 1 sec */
newtio.c_cc[VMIN] = 0; /* blocking read disabled */
tcflush(fileDescriptor, TCIFLUSH);
if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) {
perror("could not set the serial settings!");
return -99;
}
int i = 0;
string test = ">123,456,7890;";
while(i < 10) {
usleep(100000);
string res = test + std::to_string(i) + "\n";
long wrote = write(fileDescriptor, res.c_str(), sizeof(char)*res.size() );
cout << res << endl;
i++;
}
cout << "ready" << endl;
return 0;
}
Related
(excuse my english)
Hi. I'm a beginner at coding who started trying things in Arduino and c++ (because of Arduino). Recently I just learned that you could write to the Arduino board via the serial monitor, and thus via your PC through your COM port, so I tried to find a way of controlling a servo motors rotation from cmd via Arduino's serial monitor. I copied code that was given as an example in the Arduino website and via someone from youtube and got a piece of code that worked, sort of.
Because you see this code works as in it writes to the serial monitor via a c++ program on windows (any ide), but when I send a command, it runs correctly, then it runs once again but setting the input to 0 degrees thus moving the servo head to 0 degrees. I "fixed" it temporarily by preventing the servo from rotating if the input is 0 but it runs everything else in the sketch.
(I've included the code below but it's really long and I don't exactly know how to include big pieces of code (apologies if I included stuff wrong) while still including the minimum reproducible example. I'll happily change it if told to)
The main code:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <string>
#include "SerialPort.h"
char output[MAX_DATA_LENGTH];
char incomingData[MAX_DATA_LENGTH];
using namespace std;
char temp[] = "\\\\.\\COM4";
char* port = temp;
float Scalar = 0.8888;
int main() {
SerialPort arduino(port);
if (arduino.isConnected()) {
cout << "Connection established" << endl << endl;
}
else {
cout << "Error in port name" << endl << endl;
}
while (arduino.isConnected()) {
while (true) {
cout << "Enter your command (degrees 0 to 180 in integers): " << endl;
string data;
cin >> data;
if (data == "exit")
return 0;
/*if (stoi(data) != 90) {
data = to_string(
round((stof(data) - 90) * Scalar + 90)
);
}*/
//cout << data << endl;
char* charArray = new char[data.size() + 2];
copy(data.begin(), data.end(), charArray);
charArray[data.size()] = '\n';
charArray[data.size() + 1] = '\0';
cout << "char array: " << charArray << endl;
arduino.writeSerialPort(charArray, MAX_DATA_LENGTH);
arduino.readSerialPort(output, MAX_DATA_LENGTH);
cout << ">> " << output << endl;
cout << "----------\n" << endl;
delete[] charArray;
}
}
return 0;
}
The .ino sketch file:
#include <Servo.h>
Servo servo1;
int LED = 13;
int servoPos = 90; //to store servo's position
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LED, OUTPUT);
servo1.attach(11);
servo1.write(servoPos);
}
void loop() {
// put your main code here, to run repeatedly:
delay(15);
if(Serial.available() > 0) {
String info;
String return_text;
info = Serial.readStringUntil('\n');
return_text = "info variable string is: " + info;
Serial.println(return_text);
if(info.toInt() < servoPos && info.toInt() != 0) {
for( 0; servoPos > info.toInt(); servoPos-=1) {
servo1.write(servoPos);
delay(5);
}
}
else if(info.toInt() > servoPos) {
for( 0; servoPos < info.toInt(); servoPos+=1) {
servo1.write(servoPos);
delay(5);
}
}
digitalWrite(LED, HIGH);
delay(20);
digitalWrite(LED, LOW);
//Serial.println("");
//Serial.print("Servo position: ");
//Serial.println(servoPos);
}
}
The header:
#ifndef SERIALPORT_H
#define SERIALPORT_H
#define ARDUINO_WAIT_TIME 2000
#define MAX_DATA_LENGTH 255
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
class SerialPort
{
private:
HANDLE handler;
bool connected;
COMSTAT status;
DWORD errors;
public:
SerialPort(char* portName);
~SerialPort();
int readSerialPort(char* buffer, unsigned int buf_size);
bool writeSerialPort(char* buffer, unsigned int buf_size);
bool isConnected();
};
#endif // SERIALPORT_H
And the cpp file containing the functions:
#include "SerialPort.h"
SerialPort::SerialPort(char* portName)
{
this->connected = false;
this->handler = CreateFileA(static_cast<LPCSTR>(portName),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (this->handler == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
printf("ERROR: Handle was not attached. Reason: %s not available\n", portName);
}
else
{
printf("ERROR!!!");
}
}
else {
DCB dcbSerialParameters = { 0 };
if (!GetCommState(this->handler, &dcbSerialParameters)) {
printf("failed to get current serial parameters");
}
else {
dcbSerialParameters.BaudRate = CBR_9600;
dcbSerialParameters.ByteSize = 8;
dcbSerialParameters.StopBits = ONESTOPBIT;
dcbSerialParameters.Parity = NOPARITY;
dcbSerialParameters.fDtrControl = DTR_CONTROL_ENABLE;
if (!SetCommState(handler, &dcbSerialParameters))
{
printf("ALERT: could not set Serial port parameters\n");
}
else {
this->connected = true;
PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
Sleep(ARDUINO_WAIT_TIME);
}
}
}
}
SerialPort::~SerialPort()
{
if (this->connected) {
this->connected = false;
CloseHandle(this->handler);
}
}
int SerialPort::readSerialPort(char* buffer, unsigned int buf_size)
{
DWORD bytesRead;
unsigned int toRead = 0;
ClearCommError(this->handler, &this->errors, &this->status);
if (this->status.cbInQue > 0) {
if (this->status.cbInQue > buf_size) {
toRead = buf_size;
}
else toRead = this->status.cbInQue;
}
if (ReadFile(this->handler, buffer, toRead, &bytesRead, NULL)) return bytesRead;
return 0;
}
bool SerialPort::writeSerialPort(char* buffer, unsigned int buf_size)
{
DWORD bytesSend;
if (!WriteFile(this->handler, (void*)buffer, buf_size, &bytesSend, 0)) {
ClearCommError(this->handler, &this->errors, &this->status);
return false;
}
else return true;
}
bool SerialPort::isConnected()
{
return this->connected;
}
What happens:
So when I run it, first the servo rotates to 90 degrees, then when I give it an integer input the Arduino runs the servo to the desired input, powers on and off the LED, and whilst this happens the cpp main code prints what I've sent to the Arduino (or at least the visible stuff without \n and \0 e.t.c) and what the serial monitor wrote and goes back to asking me to send an input. The problem is the Arduino code runs again and moves the servo to 0 degrees and lights on and off the LED, just like before without my request.
I went and prevented the servo from moving if data == 0, but the rest of the .ino sketch still runs, so I suspect it maybe have something to do with the char that the cpp file sends to the monitor..? But I'm too smooth brained to figure it out, thus asking for any advice. The console shows the output but doesn't show anything during the second time the arduino loop happens.
If I write something in the console whilst the second (undesired) iteration happens then the arduino doesn't respond to my input, and it also messes up output from the serial monitor during the next runs by either not showing text or completely messing up the text and/or displaying weird gibberish like "³ô¦ò»]½R↨ùýº§»⌂2{²à_Ú◄/4ý¾ı" and more.
(If anything needs to be added, removed or changed because of either misconduct, etiquette or e.t.c then as I said I'll be more than happy to)
thanks in advance.
Take a look here: Serial monitoring tool
It's a tool that simplify serial monitoring. You can view/plot data, send commands, store parameters and bind data with 3D view.
There are files for generic C code and the files (cpp) for Arduino are here Arduino files
Apologies for not updating. I found the solution. It was just that I asked the function from the code online to send a char array of 255 characters when I provided it with the length of whatever I sent. Since the function/header was tasked to send 255 characters it somehow made up random data which the arduino serial COM reader read as 0. I just fixed it by telling the function how much data to send by changing MAX_DATA_LENGTH in arduino.writeSerialPort(charArray, MAX_DATA_LENGTH); to just the number of characters in the string to send.
I read all the solutions and test them. someone said used O_NDELAY and remove fnctl or the other said to use O_NONBLOCK and so on, but my problem does not solve.
I have nanopi neo and I want to use UART0. This is my code:
Main.cpp:
#include <QCoreApplication>
//#include "serialport.h"
#include "portserial.h"
#include <iostream>
#include <istream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString port;PortSerial *p;
cout<<"Enter tty:";
QTextStream s(stdin);
port = s.readLine();
p=new PortSerial(port,1);
if(p->openport(port)==1)
{
cout << "connected\n";
}
else
{
qDebug()<<"Not serial port : "<<port;
delete p;
}
return a.exec();
}
and portSerial.h and .cpp:
#ifndef PORTSERIAL_H
#define PORTSERIAL_H
#include <stdio.h> /* Standard input/output definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include "errno.h" /* Error number definitions */
#include "termio.h" /* POSIX terminal control definitions */
#include <stdlib.h>
#include "QDebug"
#define MAXDATASIZE 2048
#include <QByteArray>
#include <QThread>
#include <pthread.h>
class PortSerial:public QObject
{
Q_OBJECT
public:
QThread t;
// pthread_t pthrd;
int fd;
int status;
char * buffer;
char * buffertemp;
char buf2[MAXDATASIZE];
QString output;
QByteArray ba;
QByteArray batemp;
fd_set readfs;
timeval tv;
int n;
int devid;
bool wait;
PortSerial(QString port, int dev);
~PortSerial();
public slots:
int openport(QString port);
int init();
void closeport();
void readData();
void ifExist();
// int readifexist(char &tmp);
void writeData(QByteArray ba);
signals:
void readyread(QByteArray ba,int devids);
};
#endif // PORTSERIAL_H
and .cpp is:
#include "portserial.h"
PortSerial::PortSerial(QString port,int dev)
{
// qDebug()<<"Start the thread";
buffer = new char[1024];
devid=dev;
openport(port);
moveToThread(&t);
connect(&t, SIGNAL(started()), this, SLOT(ifExist()));
t.start();
}
PortSerial::~PortSerial()
{
closeport();
}
//____________________________
int PortSerial::openport(QString port)
{
fd=open(port.toStdString().c_str(),O_RDWR|O_NOCTTY|O_NDELAY/*O_NONBLOCK*/);//ARM
if (fd==-1)
{
return -1;//return open port error
}
else
{
// fcntl(fd,F_SETFL,O_NDELAY);
init();
return 1;
}
}
//-------------------------------------
void PortSerial::closeport()
{
qDebug()<<"Close with : " << close(fd);
}
//-------------------------------------
int PortSerial::init()
{
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);
/* Error Handling */
if ( tcgetattr ( fd, &tty ) != 0 ) {
//qDebug() << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
return -2;
}
/* Save old tty parameters */
tty_old = tty;
/* Set Baud Rate */
if(devid==1){//rfid
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);
qDebug()<<"fd is : "<< fd<<" with this baud rate: 9600";
}
else{//fp
cfsetospeed (&tty, (speed_t)B115200);
cfsetispeed (&tty, (speed_t)B115200);
qDebug()<<"fd is : "<< fd<<" with this baud rate: 115200";
}
/* Setting other Port Stuff */
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
/* Make raw */
cfmakeraw(&tty);
/* Flush Port, then applies attributes */
tcflush( fd, TCIFLUSH );
if ( tcsetattr ( fd, TCSANOW, &tty ) != 0) {
//qDebug() << "Error " << errno << " from tcsetattr" << std::endl;
return -3;
}
return 1;
}
///new code
/// *******************************************************
///
///
/// *******************************************************
void PortSerial::readData()
{
buffer[0]=0;
usleep(20000);
int res = read(fd,buffer,2048);
if(res <=0)
qDebug() << strerror(errno) ;
if(res >0)
{
buffer[res]=0;
// batemp = QByteArray((char*)buffer, res);
batemp.append(QByteArray((char*)buffer, res));
// emit readyread(ba,devid);
// qDebug()<<"_____READ________\n"<<ba.toHex()<<"\n";
}
}
void PortSerial::ifExist()
{
int c=0;
while(1){
//set timeout for wait
tv.tv_sec = 0;
tv.tv_usec = 1000;
FD_ZERO(&readfs);
FD_SET(fd, &readfs);
n=select(fd+1, &readfs, NULL, NULL, &tv /* no timeout */);
if(n==1)
{
qDebug("##########################################################################reading");
readData();
}
else
{
if(batemp.length()>0){
qDebug()<<"batemp len is:"<<batemp.length();
emit readyread(batemp,devid);
qDebug()<<"\n________________________READ byte_____________________________\n"<<batemp.toHex()
<<"\nnon hex is: "<<batemp<<"\n";
batemp.clear();
}
}
}
}
///new code
/// *******************************************************
///
///
/// *******************************************************
//------------------------------------
void PortSerial::writeData(QByteArray ba)
{
int res=write(fd,ba,ba.length());
qDebug()<<"port write : "/*<<ba.toHex()*/<<"******* res is : "<<res;
}
When I give this serial port: /dev/ttyS0 to this, First read Null (0a03 in hex) and then just read this:
root#NanoPi-NEO:~# ./FPConsole -qws
Enter tty:/dev/ttyS0
fd is : 7 with this baud rate: 9600
fd is : 8 with this baud rate: 9600
connected
##########################################################################reading
batemp len is: 2
________________________READ byte_____________________________
"0a03"
non hex is: "
"
##########################################################################reading
Resource temporarily unavailable
How Can I solve this?To read the exact RFID card data and read it more than once without resource temporarily unavailable .
For a school project, I'm using an Arduino Uno together with a Parallax RFID, a LCD screen and an esp8226-wifi module.
I'm trying to compare the scanned tag with the tags in the database and send the name of the tag owner to a terminal in the Blynk app. Everything works just fine until I put in the function that compares the tags. If I do that, everything stops working, even the code in the setup() part. How can I fix this?
I think the problem has somehing to do with the strcmp.
/* Libraries that need to be manually installed:
Blynk libraries: https://github.com/blynkkk/blynk-library/releases/download/v0.5.0/Blynk_Release_v0.5.0.zip
LiquidCrystal_I2C library: https://cdn.instructables.com/ORIG/FVH/K8OQ/J8UH0B9U/FVHK8OQJ8UH0B9U.zip
*/
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Setting up the Blynk wifi connection
#define ESP8266_BAUD 9600
char auth[] = "87b00838cd834e4e87a0422265cc7a9e";
char ssid[] = "bbox2-56b2";
char pass[] = "91C2D797F6";
//Setting up the virtual pins
WidgetTerminal terminal(V1);
BLYNK_WRITE(V1){}
//Setting up the RFID
#define RFIDEnablePin 8
#define RFIDSerialRate 2400
String RFIDTAG=""; //Holds the RFID Code read from a tag
String DisplayTAG = ""; //Holds the last displayed RFID Tag
//Setting up the LCD
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//Setting up the serial connection
SoftwareSerial EspSerial(2, 3);
ESP8266 wifi(&EspSerial);
void setup()
{
//Serial communication
Serial.begin(RFIDSerialRate);
EspSerial.begin(ESP8266_BAUD);
Serial.begin(RFIDSerialRate);
delay(10);
//Blynk setup
Blynk.begin(auth, wifi, ssid, pass);
//LCD setup
lcd.begin(16,2);//16 kolommen, 2 rijen
lcd.backlight();
//RFID setup
pinMode(RFIDEnablePin,OUTPUT);
digitalWrite(RFIDEnablePin, LOW);
terminal.println("Terminal printing succesfull");
terminal.flush();
}
void loop()
{
if(Serial.available() > 0)
{
ReadSerial(RFIDTAG);
}
if(DisplayTAG!=RFIDTAG)
{
DisplayTAG=RFIDTAG;
// PROBLEM STARTS HERE
//Tag database
char tags[10][10] = {"6196", "6753", "5655", "69EC", "9FFC"};
char owners[10][30] = {"per1", "per2", "per3", "per4", "per5"};
int i = 0;
int j = 0;
int ownerLength = 0;
char lastTag[10];
RFIDTAG.toCharArray(lastTag, 10);
while (i < 10)
{
if (strcmp(tags[i], lastTag) == 0)
{
ownerLength = strlen(owners[i]);
while (j < ownerLength)
{
terminal.print(owners[i][j]);
}
terminal.println("has entered the parking\n\r");
terminal.flush();
break;
}
i++;
}
i = 0;
j = 0;
//PROBLEM ENDS HERE
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Last tag:");
lcd.setCursor(0,1);
lcd.print(RFIDTAG);
digitalWrite(RFIDEnablePin, HIGH);
delay(1000);
digitalWrite(RFIDEnablePin, LOW);
}
Blynk.run();
}
//Function for reading the tag
void ReadSerial(String &ReadTagString)
{
int bytesread = 0;
int val = 0;
char code[10];
String TagCode="";
if(Serial.available() > 0)
{
if((val = Serial.read()) == 10)
{
bytesread = 0;
while(bytesread<10) // Reads the tag code
{
if( Serial.available() > 0)
{
val = Serial.read();
if((val == 10)||(val == 13)) // If header or stop bytes before the 10 digit reading
{
break; // Stop reading
}
code[bytesread] = val; // Add the digit
bytesread++; // Ready to read next digit
}
}
if(bytesread == 10) // If 10 digit read is complete
{
for(int x=6;x<10;x++) //Copy the Chars to a String
{
TagCode += code[x];
}
ReadTagString = TagCode; //Returns the tag ID
while(Serial.available() > 0) //Burn off any characters still in the buffer
{
Serial.read();
}
}
bytesread = 0;
TagCode="";
}
}
}
This program runs on PC and I want him wait until there are data available on serial connection from Arduino, and display them. If Arduino don't send anything, I want the program to wait indefinitely.
What happens, is that I've got only Resource temporarily unavailable messages.
After kind help from Kurt Stutsman here is a working program that waits for binary input from serial connection, (from the Arduino in this case).
#include <string.h>
#include <iostream>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h> // File control definitions
#include <termios.h> // POSIX terminal control definitions
#include <unistd.h> // UNIX standard function definitions
using namespace std;
int connect(const char* serialport)
{
struct termios toptions;
int fd;
fd = open(serialport, O_RDWR );
if (fd == -1) {
return -1;
}
if (tcgetattr(fd, &toptions) < 0)
return -1;
speed_t brate = B9600; //9600 bauds
cfsetospeed(&toptions, brate);
cfmakeraw(&toptions);
toptions.c_cc[VMIN] = 1; //I want read to wait for the input
toptions.c_cc[VTIME] = 0; //indefinitely, until it arrives. Byte after byte.
tcsetattr(fd, TCSANOW, &toptions);
if( tcsetattr(fd, TCSAFLUSH, &toptions) < 0) {
return -1;
}
return fd;
}
int main() {
int fd = connect("/dev/ttyACM0");
if (fd==-1)
{
cout<<"Error in opening serial port\n";
cout<<strerror(errno)<<'\n';
return -1;
}
while(true)
{
uint8_t b=0;
int ret=read(fd, &b, 1);
if (ret == 1)
{
cout<<"received byte "<<int(b)<<'\n';
} else if (ret == 0)
{
cout<<"EOF\n";
} else
{
cout<<strerror(errno)<<'\n';
}
}
return 0;
}
You can test it with this simple Arduino C++ code:
void setup() {
Serial.begin(9600); // connect to the serial port
}
void loop()
{
Serial.write(uint8_t(42));
delay(1000);
}
I want to control the RTS line on the serial port to wake up a device. Here is some rough test code to open the serial port. I need to set the RTS line low for 500ms. I'm having trouble googleing/understanding how to control the lines. Any Hints?
EDIT: OR controlling the CTS/DTR, any other line besides the Tx/Rx lines to connect to a pin of the robot.
#include <iostream>
#include <string.h>
#include <cstdio>
#include <fstream>
#include <sys/types.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <stdint.h> /* Standard types */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/ioctl.h>
#include <getopt.h>
using namespace std;
int fd_global;
FILE* fp_global;
int serialport_init(const char* serialport, int baud);
int serialport_read_until(int fd);
int fpeek(FILE* stream);
bool fexists(const char *filename);
void* screen_thread(void* arg) {
while(1) {
}
}
int main(void) {
pthread_t screen_thread_id;
int flags;
int baudrate = B9600; // default
fd_global = serialport_init((char*)"/dev/ttyUSB0", baudrate);
if(fd_global==-1) {
cout << "Open port: error\n";
return -1;
}
fp_global = fdopen(fd_global, "r");
if (-1 == (flags = fcntl(fd_global, F_GETFL, 0))) flags = 0;
fcntl(fd_global, F_SETFL, flags | O_NONBLOCK);
//pthread_create(&screen_thread_id, NULL, screen_thread, (void*)NULL);
serialport_read_until(fd_global);
return 0;
}
int serialport_read_until(int fd)
{
char b[1];
while(1) {
int n = read(fd, b, 1); //Read byte at a time
if(n==-1) {
printf("N");fflush(stdout);
perror("READ: ");
}
else if( n==0 ) {
printf("Z");fflush(stdout);
} else if(n>0) {
printf(":%d:",n);fflush(stdout);
}
usleep(100*1000);
if (fexists("/dev/ttyUSB0"))
printf("CONNECTED\n");
else
printf("NOT CONNECTED\n");
}
return 0;
}
bool fexists(const char *filename)
{
ifstream ifile(filename);
return ifile;
}
int serialport_init(const char* serialport, int baud)
{
struct termios toptions;
int fd;
// Open port
fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("init_serialport: Unable to open port ");
return -1;
}
// Read current termios settings
if (tcgetattr(fd, &toptions) < 0) {
perror("init_serialport: Couldn't get term attributes");
return -1;
}
// Set baud rate variable
speed_t brate = baud;
switch(baud) {
case 2400: brate=B2400; break;
case 4800: brate=B4800; break;
case 9600: brate=B9600; break;
#ifdef B14400
case 14400: brate=B14400; break;
#endif
case 19200: brate=B19200; break;
#ifdef B28800
case 28800: brate=B28800; break;
#endif
case 38400: brate=B38400; break;
case 57600: brate=B57600; break;
case 115200: brate=B115200; break;
}
cfsetispeed(&toptions, brate);
cfsetospeed(&toptions, brate);
// Setup termios for 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// Reccomended settings
toptions.c_cflag &= ~CRTSCTS; // no flow control
toptions.c_cflag |= CREAD | CLOCAL; // turn on read & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
toptions.c_oflag &= ~OPOST; // make raw
// Setting when read() releases
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html (Still a little confusing)
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;
// Apply settings
if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
perror("init_serialport: Couldn't set term attributes");
return -1;
}
return fd;
}
Would this work? I'll test it tomorrow.
int status;
status |= TIOCM_RTS;
ioctl(fd, TIOCMSET, &status);
usleep(500*1000);
status &= ~TIOCM_RTS;
ioctl(fd, TIOCMSET, &status);