Reading and Writing on same serial port

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 /* Standard input/output definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* 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

Reply to
emb in linux
Loading thread data ...

Care to tell us 1) what you think your code is supposed to do, and 2) what it actually does?

--
Grant Edwards                   grante             Yow!  Hand me a pair of
                                  at               leather pants and a CASIO
 Click to see the full signature
Reply to
Grant Edwards

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

Reply to
Paul Keinanen

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: and I am always interested in comments, suggestions, and improvements.

--Yan

Reply to
Captain Dondo

//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.

Reply to
emb in linux

I had written a basic threading program to read and write on a same port. /*serial program:*/ #include /* Standard input/output definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* POSIX terminal control definitions */ #include /* 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.

Reply to
emb in linux

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

Reply to
TW

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               
 Click to see the full signature
Reply to
Grant Edwards

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:

Reply to
emb in linux

Hai i have tried with select call .It also stucked in the read only.the following is my code: #include #include #include #include #include #include #include #include

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

Reply to
emb in linux

What is connected to the serial port ?

/Rune

Reply to
Rune Christensen

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

Reply to
emb in linux

Have you tried to communicate with the fingerprint scanner directly from a terminal?

Could give me a link to the documentation of the finger print scanner?

/Rune

Reply to
Rune Christensen

hai rune, 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

Reply to
emb in linux

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

Reply to
Rune Christensen

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

Reply to
Bob McConnell

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)

Reply to
emb in linux

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

Reply to
Rune Christensen

Most computers are equipped with two serial ports so he can just connect the two ports togheter with a NULL modem cable and then "cat /dev/ttyS0" in one xterm and "echo test > /dev/ttyS1" in another.

--
espen
Reply to
Espen Myrland

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:

Reply to
emb in linux

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.