LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-09-2009, 10:13 AM   #1
maus
LQ Newbie
 
Registered: Jun 2009
Posts: 14

Rep: Reputation: 0
Problem writing on serial device


Hi,

I'm writing a program which reads and writes data on the serial device.

Reading works fine. But with writing I'm struggling.

Here is my code reduced to the minimum:

Code:
	//----< Open serial device >----------------------------------
	int fileDescriptor;
	fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
	
	if (fileDescriptor == -1) {
		perror("Error while opening serial interface occurred!");
		return -99;
	}

	// set new parameters to the serial device
	struct termios newtio;
	tcgetattr(fileDescriptor, &oldTio);
	// flush it before restore it.
	bzero(&newtio, sizeof(newtio));
	//	newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
	

	// canonical disable -> RAW input
	newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
	newtio.c_cflag = BAUDRATE | CRTSCTS	| CLOCAL | CREAD;

	// When the OPOST option is disabled, all other option bits in  c_oflag are ignored.

	//	newtio.c_oflag &= ~OPOST;
	// newtio.c_oflag |= OPOST;
	newtio.c_oflag = 0;

	secs vor next char during burst
	
	tcflush(fileDescriptor, TCIFLUSH);
	if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) {
		perror("could not set the serial settings!");
		return -99;
	}

	
	//----  Open serial device ----------------------------------
	char acki[32] = { "test test\n\0" };


// finally writing
if (write(fd, acki, strlen(acki)) != strlen(acki)) {
			perror("can't write);
		}
I'm testing my program with a Nullmodem-Cable plugged in to /dev/ttyS0 and /dev/ttyUSB0

One end is the program by me, at the other end I use Screen. I checked the setup by running Screen on both ends and writing messages to each other end.

I guess the error is somewhere in the Serial-Settings. Any suggestions? Thanks
 
Old 10-09-2009, 12:18 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,220

Rep: Reputation: 164Reputation: 164
Quote:
I guess the error is somewhere in the Serial-Settings. Any suggestions? Thanks
clarify
 
Old 10-09-2009, 12:51 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,753

Rep: Reputation: 719Reputation: 719Reputation: 719Reputation: 719Reputation: 719Reputation: 719Reputation: 719
Without seeing all of your code it is difficult to provide help. What you posted does not look like it will compile without errors. Homework?

One error that pops out:

Code:
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
vs
write(fd, acki, strlen(acki))
 
Old 10-09-2009, 04:18 PM   #4
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 3,885

Rep: Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774
Err, but what does happen? Do you get nothing, lost characters or are all you characters corrupt?

And you cable, what connections have you made in addition to txdata and rxdata? And are you trying to use software or hardware flow control?
 
Old 10-10-2009, 05:06 AM   #5
maus
LQ Newbie
 
Registered: Jun 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
Without seeing all of your code it is difficult to provide help. What you posted does not look like it will compile without errors. Homework?

One error that pops out:

Code:
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
vs
write(fd, acki, strlen(acki))
Sorry, I merged the given code from two functions into one. Of course fd is the valid and open filedescriptor. Anyway, writing works just fine, and returns the number of written bytes. No homework. I'm trying to interrogate my solar power converter.

Again, I'm fine while reading from the serial device without any problems. I just have the impression that in case of writing nothing comes out.

Quote:
Originally Posted by salasi View Post
Err, but what does happen? Do you get nothing, lost characters or are all you characters corrupt?

And you cable, what connections have you made in addition to txdata and rxdata? And are you trying to use software or hardware flow control?
I'm loosing all characters which I try to write out.
I'm using a ordinary Null-Modem cable which worked fine during a test with a Minicom or Screen apps at both ends.

I guess I'm using the standard software flow control.
 
Old 10-10-2009, 05:30 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,753

Rep: Reputation: 719Reputation: 719Reputation: 719Reputation: 719Reputation: 719Reputation: 719Reputation: 719
If you can read then the serial port parameters are ok and so you should be able to transmit characters as well.
If nothing comes out then you still might have errors in your code which is why I asked if you would post your entire program.
 
Old 10-14-2009, 04:46 PM   #7
maus
LQ Newbie
 
Registered: Jun 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Hi,

I was wrong with the CRTSCTS Option. There is no HW flow control. Now I'm able to write.

Thanks for your Support.
 
  


Reply

Tags
linux, serial


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
problem reading usb serial device ! unikz Linux - Hardware 1 08-09-2009 05:31 AM
Serial input device using usb-serial connector slackiz Linux - Hardware 4 01-12-2009 05:04 AM
Serial driver problem on reading data from device archieval Programming 2 07-22-2008 11:42 PM
Serial Device identification problem sparrott Linux - Hardware 2 08-18-2006 02:41 PM
reading and writing to a serial device (modem) Xanadu Linux - Laptop and Netbook 0 01-04-2005 10:05 AM


All times are GMT -5. The time now is 04:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration