LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-16-2011, 12:18 AM   #1
sacbravo
LQ Newbie
 
Registered: Feb 2011
Location: Bangalore, India
Distribution: Fedora
Posts: 16

Rep: Reputation: 0
Program to perform loopback test on serial port


Hello,

I want to perfrom loopback test on serial port using a loopback connector. I need to write a code using qt 4.7.4. But I am totally new to serial port programming. I am not supposed to use minicom or any 3rd party software. Is it possible to do this using script??


echo "hello" > /dv/ttyS0

and

cat /dev/ttyS0 > /root/temp.txt

work??
 
Old 12-16-2011, 03:05 AM   #2
eeekster
Member
 
Registered: Sep 2011
Posts: 163

Rep: Reputation: Disabled
Quote:
Originally Posted by sacbravo View Post
echo "hello" > /dv/ttyS0

and

cat /dev/ttyS0 > /root/temp.txt

work??
Why don't you just try it?
 
Old 12-16-2011, 04:48 AM   #3
sacbravo
LQ Newbie
 
Registered: Feb 2011
Location: Bangalore, India
Distribution: Fedora
Posts: 16

Original Poster
Rep: Reputation: 0
I tried it, but the terminal hangs wheni run both in different terminals.
 
Old 12-16-2011, 05:55 AM   #4
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hello,

I had a serial device set up as loopback (meaning it simply echoed back any character it received), and I measured effective throughput speed. For this, I hoped I could use time, as in :

time bash -c '...'

where '...' would be some command I could run.

But the first problem was that I wanted to use the device at 2000000 bps, so I wasn't able to use ttylog or screen (they both seem to go up to 115200 bps only). However, worked with /dev/ttyUSB0 as a file (using file redirection and cat) worked fine:

# initialize serial port
stty 2000000 -ixon icanon </dev/ttyUSB0

# check settings
stty -a -F /dev/ttyUSB0

# in one terminal - read from serial port
while (true) do cat -A /dev/ttyUSB0 ; done

# in other terminal - write to serial port
echo "1234567890" > /dev/ttyUSB0

# back to first terminal, I now have:
# $ while (true) do cat -A /dev/ttyUSB0 ; done
# 1234567890$
# ...



I did something similar - I cat a file to a serial port, and had the serial port read back - but from a single terminal command (so I could use it as argument to time).

I thought that I could use a Bash process substitution, to have the "writing" and "reading" part go, sort of, in "parallel" - if I try it with named pipes, it works:

# mkfifo my.pipe # same as below:
$ mknod my.pipe p

$ comm <(echo -e "test\ntest\ntest\n" > my.pipe) <(cat my.pipe)
test
test
test
comm: file 2 is not in sorted order



Up there, I'm not using comm for any other purpose, than to (sort of) merge the two processes into a single command (I guess, I could have just as well used echo instead).

Unfortunately, that trick does not seem to work with a serial port, because when I try it, I sometimes get:

$ comm <(echo "1234567890" > /dev/ttyUSB0) <(while (true) do cat -A /dev/ttyUSB0 ; done)
cat: /dev/ttyUSB0: Invalid argument


..., however, usually I just get no output whatsoever. This tells me that: either there is no control of which process starts first, and so cat may start reading before the port is ready (however, that doesn't seem to be a problem in the first example above); or in Linux/Bash, you cannot both read and write to a serial port at the same time, and so the "Invalid argument" would occur in those moments when both read and write seem to happen at the same time.


So then after all this I tried this code mentioned below:

// from: between write and read:serial port. - C - http://www.daniweb.com/forums/thread286634.html
// gcc -o sertest -Wall -g sertest.c

#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main(int argc, char *argv[])
{
char line[1024];
int chkin;
char input[1024];
char msg[1024];
char serport[24];

// argv[1] - serial port
// argv[2] - file or echo

sprintf(serport, "%s", argv[1]);

int file= open(serport, O_RDWR | O_NOCTTY | O_NDELAY);

if (file == 0)
{
sprintf(msg, "open_port: Unable to open %s.\n", serport);
perror(msg);
}
else
fcntl(file, F_SETFL, FNDELAY); //fcntl(file, F_SETFL, 0);

while (1)
{

printf("enter input data:\n");
scanf("%s",&input[0]);

chkin=write(file,input,sizeof input);

if (chkin<0)
{
printf("cannot write to port\n");
}

//chkin=read(file,line,sizeof line);

while ((chkin=read(file,line,sizeof line))>=0)
{
if (chkin<0)
{
printf("cannot read from port\n");
}
else
{
printf("bytes: %d, line=%s\n",chkin, line);
}
}

/*CODE TO EXIT THE LOOP GOES HERE*/
if (input[0] == 'q') break;
}

close(file);
return 0;
}



And please let me know what happens after that.
 
1 members found this post helpful.
Old 12-16-2011, 06:21 PM   #5
eeekster
Member
 
Registered: Sep 2011
Posts: 163

Rep: Reputation: Disabled
Quote:
Originally Posted by sacbravo View Post
I tried it, but the terminal hangs wheni run both in different terminals.
How do you expect the cat command to terminate? It's just waiting for more data.
 
Old 12-16-2011, 09:46 PM   #6
sacbravo
LQ Newbie
 
Registered: Feb 2011
Location: Bangalore, India
Distribution: Fedora
Posts: 16

Original Poster
Rep: Reputation: 0
I tried this code on Qt but it again gets stuck somewhere. Please suggest where am i going wrong


#include "mainwindow.h"
#include "ui_mainwindow.h"
//#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>
#include <stdlib.h>
// time calls


int fd;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow:n_pushButton_clicked()
{
// file description for the serial port

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

if(fd == -1) // if open is unsucessful
{
ui->textEdit->setText("port opening unsuccessful:");
}
else
{
fcntl(fd, F_SETFL, 0);
ui->textEdit->setText("port open successful");

}

struct termios port_settings; // structure to store the port settings in

cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);

port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
tcflow(fd, 0);
tcsetattr(fd, TCSANOW, &port_settings);// apply the settings to the port



//Create byte array
char *stringdata= "HDHDHD";

write(fd, stringdata, 5); //Send data

int rbyte;
char recv_bytes[2]={0};
char tempdata[100]={0};

rbyte = read(fd, recv_bytes, 2); //READ DATA
char *dat = strcpy(tempdata,recv_bytes);
if(rbyte > 0)
{
QString data = QString(tempdata);
ui->textEdit_2->setText(data);
}


}

void MainWindow:n_pushButton_2_clicked()
{

int tcflush(int fildes, int queue_selector);
sleep(1);
//fd =
::close(fd);


}
 
Old 12-16-2011, 09:49 PM   #7
sacbravo
LQ Newbie
 
Registered: Feb 2011
Location: Bangalore, India
Distribution: Fedora
Posts: 16

Original Poster
Rep: Reputation: 0
Mr Arya, What was the problem with the code you mentioned at the end??
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial loopback test (uart) shankar.489 Linux - Newbie 1 11-09-2011 09:22 AM
test serial port & Eaton IPP web interface zelycorn Red Hat 1 06-18-2011 03:37 PM
[SOLVED] problem with ethernet loopback test using loopback plug/stub: receiving socket not se mcha Linux - Kernel 3 08-26-2010 11:28 AM
How to test Serial Port ? kushalkoolwal Debian 4 11-17-2006 03:43 PM
problem with serial port in loopback mode .. (termios) mta Programming 1 06-06-2006 11:56 AM


All times are GMT -5. The time now is 09:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration