Do you have a question? Post it now! No Registration Necessary
- emb in linux
January 16, 2006, 8:52 am

Hai all
I have successfully communicated serial ports by running receive and
transmitting programs in different terminals.Now i need to read and
write data on same port.Here i am unable to do this.I am giving my
code.If it has any bugs,help me out.
waiting for your responses guys..
/*program:*/
#include <stdio.h> /* Standard input/output definitions */
#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 definitions */
int main()
{
int fd,i=0;
char c,buff[100];
struct termios options;
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY );
if (fd == -1)
perror("Unable to open /dev/ttyS1\n");
else
{
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN]=1;
tcsetattr(fd,TCSANOW, &options);
fprintf(stdout,"In server\n");
while(1)
{
i=write(fd,"haranath",8);
if(i<=0)
fprintf(stderr,"Write() failed!\n"); */
i=read(fd,buff,1);
if(i<=0)
fprintf(stderr,"Read() failed!\n");
write(1,buff,i);
}
close (fd);
}
return (0);
}
Thanking you.
With Regards,
emb in linux
I have successfully communicated serial ports by running receive and
transmitting programs in different terminals.Now i need to read and
write data on same port.Here i am unable to do this.I am giving my
code.If it has any bugs,help me out.
waiting for your responses guys..
/*program:*/
#include <stdio.h> /* Standard input/output definitions */
#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 definitions */
int main()
{
int fd,i=0;
char c,buff[100];
struct termios options;
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY );
if (fd == -1)
perror("Unable to open /dev/ttyS1\n");
else
{
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN]=1;
tcsetattr(fd,TCSANOW, &options);
fprintf(stdout,"In server\n");
while(1)
{
i=write(fd,"haranath",8);
if(i<=0)
fprintf(stderr,"Write() failed!\n"); */
i=read(fd,buff,1);
if(i<=0)
fprintf(stderr,"Read() failed!\n");
write(1,buff,i);
}
close (fd);
}
return (0);
}
Thanking you.
With Regards,
emb in linux

Re: Reading and Writing on same serial port

In addition, people who insists of using multiple tasks, processes or
threads (whatever they are called in each environment) to write to a
serial port have not considered what to do when there are multiple
pending write requests:
1) Do a byte by byte merge between each stream
2) Wait until the first transmission is completed, then send the next
frame
3) If a low priority thread has started transmission and a high
priority thread becomes active, send all the high priority bytes and
then continue with the low priority thread final bytes.
4) etc.
Also what to do with the received frame:
1) The next ready thread will get the next byte
2) Do a round robin between all ready threads
3) Based on the received byte, forward to a specific thread
4) Copy the received byte to all threads
5) etc.
The simplest thing would be to create a separate thread that owns the
port and implement any multiple access rule in that thread.
Paul

Re: Reading and Writing on same serial port

Any examples of this? I am about to write a "driver" or whatever you want
to call it that would do exactly that for a block of SRAM... And I really
don't want to reinvent the wheel.
As to the OP, I've got my serial code here:
<http://www.seiner.com/ts7000/index.php/Modbus_and_Serial_Comm and I am
always interested in comments, suggestions, and improvements.
--Yan

Re: Reading and Writing on same serial port

I had written a basic threading program to read and write on a same
port.
/*serial program:*/
#include <stdio.h> /* Standard input/output definitions */
#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 definitions */
#include <pthread.h> /* Posix thread definations */
void *thFun(void *);
int fd;
int main()
{
int res;
pthread_t th1;
char buff;
struct termios options;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY );
if (fd == -1)
perror("Unable to open /dev/ttyS0\n");
else
{
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN]=1;
tcsetattr(fd,TCSANOW, &options);
res=pthread_create(&th1,NULL,thFun,"Thread");
if(res!=0)
{
printf("Thread creation failed\n");
exit(1);
}
while(1)
{
read(0,&buff,1);
write(fd,&buff,1);
} }
void *thFun(void *arg)
{
char ch;
while(1)
{
read(fd,&ch,1);
write(1,&ch,1);
}
}
Here a thread is created to continously read the data on serial port
and as soon as data appears it should display on stdout.
But it is get stucked in the read call of thread.

Re: Reading and Writing on same serial port

You need to set the fd you are reading from to use non-blocking I/O (see
the manpages of open(), read(), write(), select() and fcntl()).
BTW, in my opinion it's an overkill to use different threads for reading
and writing - a polling approach using select() is most likely more
maintainable and you don't get headaches when trying to synchronize
access from multiple threads to one resource.
Regards,
Thomas

Re: Reading and Writing on same serial port

Perhaps there isn't any data to read?

No, he doesn't. He want's blocking I/O. That's the whole
point of having a reader thread.

Probably so.
--
Grant Edwards grante Yow! It's today's SPECIAL!
at
Grant Edwards grante Yow! It's today's SPECIAL!
at
We've slightly trimmed the long signature. Click to see the full one.

Re: Reading and Writing on same serial port
Hai all,
Here i am explaining my need.
I have to do the following tasks on a serial port:
1)opening
2)writing
3)reading
If I am able to read the data from other port why i am not able to do
on same port.
what i mean is ,i am reading the data on com1 by opening the com2 (In
between Null modem cable is connected).
It is not happenning with threads too.
or if any body has a code of just opening a port,writing data on to a
port and finally reading the data then send to me plz...
Help needed urgently.
Thanking you.
With Regards,
haranath.T
TW wrote:

Here i am explaining my need.
I have to do the following tasks on a serial port:
1)opening
2)writing
3)reading
If I am able to read the data from other port why i am not able to do
on same port.
what i mean is ,i am reading the data on com1 by opening the com2 (In
between Null modem cable is connected).
It is not happenning with threads too.
or if any body has a code of just opening a port,writing data on to a
port and finally reading the data then send to me plz...
Help needed urgently.
Thanking you.
With Regards,
haranath.T
TW wrote:


Re: Reading and Writing on same serial port

Hello Rune,
I have to communicate with a sagem fingerprint scanner which is
connected to serial port(com1).It basically a protocol implementation
of finger print scanner.
i need to transmit the bytes to the serial port.
Thanking you.
With Regards
haranath.t

Re: Reading and Writing on same serial port
Sorry i could not give u that link and I dont have that previllages
since that documentation is highly confidentional and propriatary.
I would like to disuss with u more elaboratelly.i am having a yahoo
messenger account.and i can have MSN too.
With Regards,
Haranath.t

Re: Reading and Writing on same serial port

Does the product uses an ascii protocol or a binary protocol over the
serial interface?
If it's an ascii protocol have you then tried the product with a serial
terminal?
What baud rate does the product use?
Does it use hardware or software flow control?
/Rune

Re: Reading and Writing on same serial port
Hai Rune,
Thanks for responding for my quiry.
coming to the point,It is a binary protocol.
for a moment leave that protocol aside.can't we write and read the data
respectivelly on one port with any mechanism(for ex:Threads,select call
etc...).I need to clear that point first.
Thanks for everything.
With Regards,
emb in linux(Haranath.T)
Thanks for responding for my quiry.
coming to the point,It is a binary protocol.
for a moment leave that protocol aside.can't we write and read the data
respectivelly on one port with any mechanism(for ex:Threads,select call
etc...).I need to clear that point first.
Thanks for everything.
With Regards,
emb in linux(Haranath.T)

Re: Reading and Writing on same serial port

Sometimes in Linux there are some differences when using file i/o for
binary files instead of normal ascii files.

You can only read data from a serial if there is some data in the FIFO
or else the system call with block your process and wait for data on the
serial port.
I suggest that your take two computers one with a serial terminal and
another computer with your program. You then connect these two computers
with a null modem cable.
When you program works you should be able to send data from the terminal
to your program and send data from your program to the terminal.
Another test is to connect pin 2 and pin 3 on the serial connector on
that computer with your program then you should be able to send and
recieve data but remember to send before recieve or else your system
will block :-)
I will take a look of your code later but I need to do something else now.
Could you mail me the serial communication code and the test program
that you use?
/Rune

Re: Reading and Writing on same serial port
Hai all,
Sorry to all,its not the problem with linux or my code.The problem is
with my Thinclient which has cyrix based processor.My code is working
fine on any intel based processors.
Thanks for giving your hand for me and once again i feel very sorry for
the inconvenience.
With Regards,
Haranath.T
Rune Christensen wrote:

Sorry to all,its not the problem with linux or my code.The problem is
with my Thinclient which has cyrix based processor.My code is working
fine on any intel based processors.
Thanks for giving your hand for me and once again i feel very sorry for
the inconvenience.
With Regards,
Haranath.T
Rune Christensen wrote:


Re: Reading and Writing on same serial port

If the material is that sensitive, why are you even discussing it on a
public forum? You should be talking directly to the people in your
company that created it, or the vendor you buy it from. No one here
can help you without access to the actual documentation.
Bob McConnell
N2SPP

Re: Reading and Writing on same serial port

Hai
i have tried with select call .It also stucked in the read only.the
following is my code:
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<termios.h>
#include<sys/time.h>
#include<sys/types.h>
int main()
{
int i,res,fd;
fd_set rfds;
struct termios options;
struct timeval tv;
char buff[1];
fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY);
printf("%d",fd);
fcntl(fd,F_SETFL,0);
tcgetattr(fd,&options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |=(CLOCAL | CREAD);
options.c_cflag &=~PARENB;
options.c_cflag &=~CSTOPB;
options.c_cflag &=~CSIZE;
options.c_cflag |=CS8;
options.c_cflag |=~(ICANON | ECHO | ECHOE);
options.c_oflag |=~OPOST;
options.c_cc[VMIN]=1;
options.c_cc[VTIME]=1;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW,&options);
if (fd<0)
{
perror("open failed()");
exit(1);
}
else
{
i=fgetc(stdin);
buff[0]=i;
buff[1]='';
if(write(fd,buff,1)<0)
fprintf(stderr,"Write() failed!\n");
tv.tv_sec=0.1;
tv.tv_usec=0;
res=select(1,&rfds,NULL,NULL,NULL);
printf("%d",res);
if(res==-1)
perror("select()");
else if(res)
{
read(fd,buff,1);
write(1,buff,1);
}
close(fd);
return 0;
}
}

Re: Reading and Writing on same serial port

//supposed to do//
my code is supposed to write some bytes of data on the serial port by
using write call and read the bytes of data from same port by read
call.
But my code gets stucked in the read call.
Is here i need to use threading!
With
Regards
emb
in linux.
Site Timeline
- » Problems Creating Montavista Application
- — Next thread in » Embedded Linux
-
- » apache embedded
- — Previous thread in » Embedded Linux
-
- » Crosscompiling for ARM: reloc type R_ARM_ABS32 is not supported for PIC - ...
- — Newest thread in » Embedded Linux
-
- » Re: Capacitors at RF
- — The site's Newest Thread. Posted in » Electronics Repair
-
- » Replacement for 741
- — The site's Last Updated Thread. Posted in » Electronics Design
-