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);
}
Related
I'm new to socket programming and wanted to try something simple. This program can manipulate settings on my tv. All messages are 24 bytes. There may be one or more messages returned. I cannot figure out a good solution to get all of the messages without read() blocking on me.
What is below would be what I hoped to be a simple solution. It seems to work in a lot of example code I have found. However, what happens is after the first loop it seems to just block on the read() operation infinitely. If I remove the loop and just put multiple reads, the same thing happens. As long as I don't try to read more information that is sent, I'm ok.
I did try a couple of other things like turning off blocking, and adding a timer. neither worked. At this point I can live with a couple seconds of blocking. I just want the program to exit normally after the read.
adding output for a power_on command. It correctly outputs the two lines it should then blocks indefinitely.
Dans-MBP:~ mreff555$ ./tvthing
24: *SAPOWR0000000000000000
24: *SNPOWR0000000000000001
code below:
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <sys/time.h>
#define PORT 20060
#define POWER_ON "*SCPOWR0000000000000001\n"
#define POWER_OFF "*SCPOWR0000000000000000\n"
#define POWER_STATUS "*SEPOWR################\n"
#define POWER_TOGGLE "*STPOWR################\n"
int main(int argc, char const * argv[])
{
struct sockaddr_in tvAddress;
struct hostent *host = gethostbyname("192.168.1.128");
memset(&tvAddress, 0, sizeof(tvAddress));
tvAddress.sin_family = AF_INET;
tvAddress.sin_addr.s_addr = htonl(INADDR_ANY);
tvAddress.sin_addr.s_addr = ((struct in_addr*)(host->h_addr))->s_addr;
tvAddress.sin_port = htons(PORT);
char sendBuffer[24] = {0};
char recBuffer[24] = {0};
int socket_fd;
if((socket_fd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
else
{
if(connect(socket_fd, (struct sockaddr *)&tvAddress, sizeof(struct sockaddr)))
{
perror("connection failed failed");
exit(EXIT_FAILURE);
}
memcpy(&sendBuffer, &POWER_STATUS, sizeof(sendBuffer));
write(socket_fd, sendBuffer, strlen(sendBuffer));
int ret;
while((ret = read(socket_fd, recBuffer, sizeof(recBuffer)) > 0))
{
printf("%d: %s\n", ret, recBuffer);
}
close(socket_fd);
}
}
You need to read until your buffer is full like this:
unsigned readLen = 0;
unsigned totalLen = sizeof(recBuffer);
while (readLen < totalLen) {
int ret = read(socket_fd, recBuffer + readLen, totalLen - readLen);
if (ret > 0) {
readLen += ret;
} else {
// error handling here
break;
}
}
This is needed because read() returns only the currently available amount of bytes which might be less than you have requested. From the corresponding man-page:
RETURN VALUE
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal.
If you need to receive several responses you can put the described algorithm into a function and use it repeatedly. In any case you need to know how many responses to expect otherwise your read() will block because it seems that your TV's server is programmed to keep the connection open and it is client's responsibility to choose when to disconnect.
If you decide to make your application more sophisticated you can use one of the IO Multiplexing mechanisms to make your wait for response interruptable by timer or terminal input. For example:
while (true) {
pollfd fds[] = {
{ socket_fd, POLLIN, 0 },
{ STDIN_FILENO, POLLIN, 0 }
};
int ret = poll(fds, sizeof(fds) / sizeof(*fds), -1);
if (ret > 0) {
if (fds[0].revents & POLLIN) {
readResponse(); // read and process response
}
if (fds[1].revents & POLLIN) {
break; // exit on terminal input
}
}
}
As it turns out, select is designed exactly for that purpose. It checks the specified file descriptors for a specified time interval, and if successful repeats the process. Tweaking the time interval minimizes the blocking while allowing enough time for additional messages to come in.
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#define PORT 20060
#define POWER_ON "*SCPOWR0000000000000001\n"
#define POWER_OFF "*SCPOWR0000000000000000\n"
#define POWER_STATUS "*SEPOWR################\n"
#define POWER_TOGGLE "*STPOWR################\n"
int main(int argc, char const * argv[])
{
struct sockaddr_in tvAddress;
struct hostent *host = gethostbyname("192.168.1.128");
memset(&tvAddress, 0, sizeof(tvAddress));
tvAddress.sin_family = AF_INET;
tvAddress.sin_addr.s_addr = htonl(INADDR_ANY);
tvAddress.sin_addr.s_addr = ((struct in_addr*)(host->h_addr))->s_addr;
tvAddress.sin_port = htons(PORT);
char sendBuffer[24] = {0};
char recBuffer[24] = {0};
int socket_fd;
if((socket_fd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
else
{
if(connect(socket_fd, (struct sockaddr *)&tvAddress, sizeof(struct sockaddr)))
{
perror("connection failed failed");
exit(EXIT_FAILURE);
}
struct timeval tv;
fd_set sockRead;
int selectStatus;
memcpy(&sendBuffer, &POWER_ON, sizeof(sendBuffer));
write(socket_fd, sendBuffer, strlen(sendBuffer));
do
{
FD_ZERO(&sockRead);
FD_SET(socket_fd, &sockRead);
tv.tv_sec = 2;
tv.tv_usec = 500000;
selectStatus = select(socket_fd + 1, &sockRead, NULL, NULL, &tv);
switch(selectStatus)
{
case -1:
perror("select()");
exit(EXIT_FAILURE);
break;
case 0:
break;
default:
printf("Ready for Reading\n");
read(socket_fd, recBuffer, sizeof(recBuffer));
printf("%s\n", recBuffer);
}
}while (selectStatus > 0);
close(socket_fd);
}
}
I'm trying to communicate with a device which provided a serial port. When I use the example C program provided by the device producer, it was successfully connected and I can send/receive data. But when I use my C++ program with QSerialPort, the open() method returned false and the error() returned 11 (An unidentified error occurred, as described by Qt documentation). Could anyone please help me?
OS: Cent OS 6.9
gcc/g++: 4.8
Qt: 5.6.0
An example of connecting with QSerialPort:
#include <qserialport.h>
#include <qstring.h>
int main(int argc, const char *argv[])
{
QString portName = "/dev/corser/x64ExpCL4x1_s0";
QSerialPort *pSerial = new QSerialPort();
pSerial->setPortName(portName);
pSerial->setBaudRate(QSerialPort::Baud9600);
pSerial->setParity(QSerialPort::NoParity);
pSerial->setDataBits(QSerialPort::Data8);
pSerial->setStopBits(QSerialPort::OneStop);
pSerial->setFlowControl(QSerialPort::NoFlowControl);
bool success = pSerial->open(QIODevice::ReadWrite);
if (!success) {
printf("Error: %d\n", pSerial->error());
} else {
printf("Success.\n");
pSerial->close();
}
delete pSerial;
return 0;
}
This program prints out Error: 11.
Here is a C program which is excerpted from the device's example program. This program just try to connect the serial port and then close the connection. While the original example program is complicated. It is actually kind of a "Terminal" program which sends user-input commands to the device and prints out the responses from the device.
#include <unistd.h>
#include <termios.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <ctype.h> /* Character types */
#define TRUE 1
#define FALSE 0
typedef struct
{
int open; // Flag for current open status of port
int update; // Flag indicating parameter changes
char *port; // Current port to be opened
int iod; // I/O index for open device port
struct termios stty; // Terminal port control structure
} SCOM_CTL;
struct termios ttctl = {0};
struct termios ttsav = {0};
struct termios coctl = {0};
struct termios cnctl = {0};
#define OPEN 1
#define CLOSE 2
#define UPDATE 3
#define NO_UPDATE 4
SCOM_CTL scom;
int main( int argc, char **argv )
{
scom.port = "/dev/corser/x64ExpCL4x1_s0";
scom_init();
scom_open_port();
if (scom.open == OPEN)
{
fputs("\rSuccess.\r\n", stdout);
scom_close_port(scom.iod);
}
else
{
fputs("\rError.\r\n", stdout);
}
}
// Initialize the console (stdin) for raw access.
int scom_init()
{
int i;
scom.open = CLOSE;
scom.update = NO_UPDATE;
scom.iod = -1;
tcgetattr(0, &coctl); // Save a copy to restore stdin.
tcgetattr(0, &cnctl);
cfmakeraw( &cnctl); // Set stdin to raw !
tcsetattr(0, 0, &cnctl);
}
int scom_open_port()
{
int i, iod;
if (scom.open == OPEN)
{
close(scom.iod);
scom.open = CLOSE;
}
if ( (iod = open( scom.port, (O_RDWR | O_NOCTTY))) == -1)
{
conres(); /* Reset the console. */
fputs("term : Open Failure on device\n", stdout);
return -1;
}
scom.iod = iod;
scom.open = OPEN;
scom.update = NO_UPDATE;
}
int scom_close_port()
{
scom.open = CLOSE;
close(scom.iod);
}
int conres()
{
return(tcsetattr(0, 0, &coctl));
}
This program prints out Success.
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;
}
I wanted to create C/C++ application, that creates new (virtual) device in /dev/xxx and will be able to connect with 'screen' application.
For example program running in loop, that creates new /dev/ttyABC. Then I'll use 'screen /dev/ttyABC', and when I send there some chars, then app send it back to the 'screen'.
I really don't know where start. I found some referencies on pty library but I don't even know, if I have right direction.
Could you help me? Where to look? Post example?
Thanks
You could use a Pseudoterminal via openpty to achieve this. openpty returns a pair of file descriptors (master and slave pty devices) that are connected to each other via their stdout / stdin. The output of one will appear at the input of another and vice-versa.
Using this (rough!) example...
#include <fcntl.h>
#include <cstdio>
#include <errno.h>
#include <pty.h>
#include <string.h>
#include <unistd.h>
int main(int, char const *[])
{
int master, slave;
char name[256];
auto e = openpty(&master, &slave, &name[0], nullptr, nullptr);
if(0 > e) {
std::printf("Error: %s\n", strerror(errno));
return -1;
}
std::printf("Slave PTY: %s\n", name);
int r;
while((r = read(master, &name[0], sizeof(name)-1)) > 0) {
name[r] = '\0';
std::printf("%s", &name[0]);
}
close(slave);
close(master);
return 0;
}
... Echoing some text (in another terminal session) to the slave pty sends it to master's input. E.g. echo "Hello" > /dev/pts/2
Based on the answer provided by #gmbeard , I was able to create an echo PTY device and connect to it with screen and minicom. What made the difference was using a raw PTY device by initializing a termios struct.
Here is the code
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <cstdio>
#include <pty.h>
#include <termios.h>
#define BUF_SIZE (256)
int main(int, char const *[])
{
int master, slave;
char buf[BUF_SIZE];
struct termios tty;
tty.c_iflag = (tcflag_t) 0;
tty.c_lflag = (tcflag_t) 0;
tty.c_cflag = CS8;
tty.c_oflag = (tcflag_t) 0;
auto e = openpty(&master, &slave, buf, &tty, nullptr);
if(0 > e) {
std::printf("Error: %s\n", strerror(errno));
return -1;
}
std::printf("Slave PTY: %s\n", buf);
int r;
while ( (r = read(master, buf, BUF_SIZE)) > 0 )
{
write(master, buf, r);
}
close(slave);
close(master);
return 0;
}
I am doing a program to open, write and read the port ttyUSB0, I have the next program and I don´t write anything. Can anyone help me please????
My question is that I have a problem with the write or read function, because I can´t read and write in the ttyUSB0 port, and I search a solution to write and read ttyUSB0 port.
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int serial_open(char *serial_name, speed_t baud)
{
struct termios newtermios;
int fd;
fd = open(serial_name,O_RDWR | O_NOCTTY);
newtermios.c_cflag= CBAUD | CS8 | CLOCAL | CREAD;
newtermios.c_iflag=IGNPAR;
newtermios.c_oflag=0;
newtermios.c_lflag=0;
newtermios.c_cc[VMIN]=1;
newtermios.c_cc[VTIME]=0;
cfsetospeed(&newtermios,baud);
cfsetispeed(&newtermios,baud);
if (tcflush(fd,TCIFLUSH)==-1) return -1;
if (tcflush(fd,TCOFLUSH)==-1) return -1;
if (tcsetattr(fd,TCSANOW,&newtermios)==-1) return -1;
return fd;
}
void serial_send(int serial_fd, char *data, int size)
{
write(serial_fd, data, size);
}
int serial_read(int serial_fd, char *data, int size, int timeout_usec)
{
fd_set fds;
struct timeval timeout;
int count=0;
int ret;
int n;
do {
FD_ZERO(&fds);
FD_SET (serial_fd, &fds);
timeout.tv_sec = 0;
timeout.tv_usec = timeout_usec;
ret=select (FD_SETSIZE,&fds, NULL, NULL,&timeout);
if (ret==1) {
n=read (serial_fd, &data[count], size-count);
count+=n;
data[count]=0;
}
} while (count<size && ret==1);
return count;
}
void serial_close(int fd)
{
close(fd);
}
int main(int argc, char *argv[])
{
int serial_fd, n, longitud,;
char *device=”at”;
char *data;
longitud=strlen(device);
serial_fd = serial_open("/dev/ttyUSB0",B38400);
if (serial_fd == -1) {
printf ("Error opening the serial device: %s\n",argv[1]);
perror("OPEN");
exit(0);
}
printf("SERIAL OPEN:%s\n", device);
serial_send(serial_fd, device, longitud);
printf ("String sent------> %s\n",device);
n=serial_read(serial_fd,data,longitud,10000);
printf("Se ha recibido %s \n Tamaño: %d\n n:%d \n serial_fd:%d\n",data, longitud,n,serial_fd);
puts(data);
serial_close(serial_fd);
// cout << "Hello world!" << endl;
return 0;
}
Your program works just fine with the writing and reading, but you have another problem: Undefined behavior.
You have a pointer variable in your main function named data, and you pass this to your serial_read function. But nowhere do you make this pointer actually point anywhere, so when that pointer is dereferenced you have the undefined behavior.
Local (non-static) variables are not initialized, their value is indeterminate. You need to initialize the variable, to make it actually point somewhere. I recommend you make it into an array instead.
And then when you call serial_read you only ask to read two bytes (the length of the string "at"), instead of the actual size of the data (which currently is none), you need to pass the actual size of the buffer instead.
So to sum it up:
int main(void)
{
...
char data[256];
...
n = serial_read(serial_fd, data, sizeof(data), 10000);
...
}