?
Solved

write() system call in unistd.h

Posted on 2003-03-03
11
Medium Priority
?
4,541 Views
Last Modified: 2008-02-01
I'm trying to communicate via an RS232 serial port in a Linux PC.  I'm using low-level system calls, such as open() and write().  My question involved the write() function.  

#include <unistd.h>
ssize_t write(int fildes, const void *buf, size_t nbyte);

I can do the following without a problem:

write(fd, "HELLO\r", 6)

In this case I'm simply writing 6 ASCII bytes.  However, I want to write specific 8-bit hex values, such as 0x12 or 0x34.  Now I figured I could just declare a pointer of type const void in my program, and have it point to whatever hex values I want to write to the serial port.  Here's an example of a little program I wrote to test a few things out:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

void main (void)
{
const void *buf;
int temp, fd, n;

temp = 0x25;  /* Give bottle hex byte */

buf = temp;   /* Pointer now points to hex value */
printf("buf = %p\n", buf);

fd = open("/dev/ttyS0", 0_RDWR); /* open serial port 1 */
n = write(fd, buf, 1); /* hope to write single hex byte */

if (n < 0)
printf("write was unsuccessful!\n");
else
printf("write was successful!\n");

}

After printing out the values for this pointer, they do in fact equal the hex values that I stored in the array.  However, in Linux, my write() returns a '-1', indicating that the write did not complete.  Thanks for the antipated help though!
0
Comment
Question by:TA_Brian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8056098
two mistakes i can see

temp=buf; isnt right
it should be
temp=&buf;
also sizeof temp will be  4 bytes,
so u shud be writing 4 bytes
OR
do this ..declare temp as
unsigned char temp=0x25;

then u can do
buf=&temp;
and write shud go like this
write(fd,buf,1);
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8056107
btw if u receive error while writing ( return value = -1)
then u shud use perror to see what error number it generated.
 
and check the error number with the ones shown in the manual of write
0
 

Author Comment

by:TA_Brian
ID: 8058860
Well, now I'm getting my write() call to complete, but when I go to read from this serial port, it's saying 0 bytes are available on this port.  Do you have any idea why this would be the case?  I'm currently using a loopback cable on port 1 of my serial port.  I have verified that writes are working properly when I write straight ASCII text and then read it back.

write(fd, "HELLO", 5);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:TA_Brian
ID: 8059244
Well, now I'm getting my write() call to complete, but when I go to read from this serial port, it's saying 0 bytes are available on this port.  Do you have any idea why this would be the case?  I'm currently using a loopback cable on port 1 of my serial port.  I have verified that writes are working properly when I write straight ASCII text and then read it back.

write(fd, "HELLO", 5);
0
 

Author Comment

by:TA_Brian
ID: 8059304
Well, now I'm getting my write() call to complete, but when I go to read from this serial port, it's saying 0 bytes are available on this port.  Do you have any idea why this would be the case?  I'm currently using a loopback cable on port 1 of my serial port.  I have verified that writes are working properly when I write straight ASCII text and then read it back.

write(fd, "HELLO", 5);
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8061671
you will have to start a read thread before writing anything else.. i guess whatever u write is getting lost .. as it is not getting buffered anywhere..
so in your program start a read thread .. and after that write something on ur port..
OR
modify ur program to just read from the port .. and with some external command try to write on the port .. like this
cat <some_text_file> >>/dev/ttyS0
OR
make two programs .. one is for reading and another one for writing ..
the reader program should keep reading in loop..

0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8061687
get hold of some book/tutorial on writing character device drivers for linux
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8061701
sorry may be it should be serial device
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8061704
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 300 total points
ID: 8061723
sample program to read from tty device.. run this separately and use other program to write on the device

#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

#define LEN 512

int main(int argc, char **argv) {
  struct termios term_info;
  struct termio old_term_info;
  int fd;
  int old_out_baud_rate;
  char str[LEN];
  int nread;
  fd_set rfds;

  if ((fd = open("/dev/ttyS0", O_RDWR)) < 0) {
    perror("Can't open serial port");
    exit(1);
  }

  if (tcgetattr(fd, &term_info) == -1) {
    perror("Can't get serial port attributes");
    close(fd);
    exit(2);
  }
  old_term_info = term_info;

  // set h/w control
  term_info.c_cflag |= CRTSCTS | CS8;

  if (term_info.c_lflag & ICANON) {
    fprintf(stderr, "In canonical mode");
    term_info.c_lflag &= ~ICANON;
  }

  // set timeout
  term_info.c_cc[VTIME] = 150; // 15 seconds
  term_info.c_cc[VMIN] = 0;

  cfsetispeed(&term_info, B2400);
  cfsetospeed(&term_info, B2400);
  if (tcsetattr(fd, TCSANOW, &term_info) == -1) {
    perror("Can't set serial port attributes");
    close(fd);
    exit(3);
  }

  while ((nread = read(fd, str, LEN)) > 0) {
    fprintf(stderr, "Read %d %d\n", nread, str[0]);
    write(1, str, nread);
  }

  /*
   * reset state
   */
  if (tcsetattr(fd, TCSANOW, &old_term_info) == -1) {
    perror("Can't reset serial port attributes");
    close(fd);
    exit(4);
  }

  close(fd);
  exit(0);
}
0
 

Author Comment

by:TA_Brian
ID: 8066559
Thanks a lot man, I appreciate the help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question