?
Solved

write() system call in unistd.h

Posted on 2003-03-03
11
Medium Priority
?
4,577 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
  • 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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 

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

Become a Leader in Data Analytics

Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.  

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

594 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