LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 03-10-2010, 12:31 AM   #1
anujmehta
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Rep: Reputation: 0
Serial port : not able to write big chunk of data


Hi

I am trying to send text data from one PC to other using Serial cable. One of the PC is running linux and I am sending data from it using write(2) system call. The log size is approx 65K bytes but the write(2) system call returns some 4K bytes (i.e. this much amount of data is getting transferred). I tried breaking the data in chunks of 4K but write(2) returns -1.

My question is that "Is there any buffer limit for writing data on serial port? or can I send data of any size?. Also do I need to continously read data from other PC as I write 4K chunk of data"

Do I need to do any special configuration in termios structure for sending (huge) data?
 
Old 03-10-2010, 09:44 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Can you post the relevant code fragment(s)? Also, get the output of perror() when write() returns -1. I just cobbled up a quick test, and saw no such problems. Are you sure you are not getting some intervention from a flow-control mechanism?
--- rod.

Last edited by theNbomr; 03-10-2010 at 09:47 AM.
 
Old 03-10-2010, 08:52 PM   #3
anujmehta
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Hi rod

Thanks for your reply. As I have mentioned earlier that using write(2) system call I was not able to send complete data in one shot. I did looping and tried to transfer the remaining bytes. This way I am able to transfer complete data.
I had observed that in general I was able to transfer 32 or 48 bytes in one write call. Is this fine for serial port or is there any way of optimizing it?

Code:
/*
 * buff - buffer to be transfered
 * len - length if 'buff'
 */
void sendData(int len, const char * buff)
{
  int writtenBytes = write(fd, buff, len);
  string str(buff);
  if(writtenBytes < len)
  {
      int begin = 0;
      while (len > 0)
      {
          usleep(9000);
          len -= writtenBytes;
          writtenBytes = write(fd, str.substr(begin, len).c_str(), len);
          if(writtenBytes == -1)
               writtenBytes = 0;
          begin += writtenBytes;
      }
  }
  else if(writtenBytes < 0)
      TRACE << "Error in sending data" << endl;
}

Last edited by anujmehta; 03-10-2010 at 09:00 PM. Reason: added code
 
Old 03-11-2010, 11:05 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I don't see anything that looks out of sorts with your code, and it is remarkably similar to my test code. Is there a consistency to the size of the bytes written per write? What about the nature of the data? Are there end-of-lines embedded in the data? If so, I guess I would try setting the serial port to raw mode:
Code:
struct termios  serialPortCfg;

    tcgetattr(fd, &serialPortCfg );
    serialPortCfg.c_oflag &= ~OPOST;
    tcsetattr( fd, TCSANOW, &serialPortCfg );
I'm not completely sure about how cooked output deals with CRs & LFs or NULLs. A reasonable speculation might be that NULLs could cause the output stream to be broken into blocks, but that shouldn't be the case here, as it looks like your data is one long string. Can you confirm that the buffer length value actually being requested to write() is greater than the value returned as writtenBytes?
Sorry for the sketchy reply; I'm at a loss for better suggestions.

--- rod.
 
Old 03-11-2010, 09:08 PM   #5
anujmehta
LQ Newbie
 
Registered: Mar 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Rod thanks for your reply. I have set both input and output in raw mode. I am reading data from a file and it do contain new lines.

Code:
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG)
options.c_oflag &= ~OPOST
Yes the value of buffer size is greater than writtenBytes
I didn't thought of CR, LF and NULL, probably either of them is the reason for the data going in packets of size 32/48.

Thanks again for your help.
 
  


Reply

Tags
serial port, write


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
[SOLVED] Serial port : Read data problem, not reading complete data anujmehta Linux - Networking 5 09-06-2010 06:10 AM
How to write to a serial port ? TheIndependentAquarius Linux - General 2 10-20-2009 02:18 AM
the get data from serial port function read() blocks when data not available DEF. Programming 1 06-11-2009 04:21 AM
TO write to a serial port payu21 Linux - Newbie 1 05-24-2009 09:23 AM
is there any shell command to read and write data from parallel and serial port? baosheng Linux - Hardware 2 01-13-2007 08:35 PM


All times are GMT -5. The time now is 08:25 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