You are here:

C++/C write to /dev/

Advertisement


Question
Hi,

    I'm writing a C++ program to control an SSC (servo controller) through the serial bus on Linux.  The SSC recieves 3 bytes:
Byte 1 : Sync Marker (255)
Byte 2 : Servo # (0-7)
Byte 3 : Servo Position (0-254)
Which must be sent as individual byte values.
I opened /dev/ttyS1 and tried to send 3 bytes to it using the write(fd, buf, 3) command.  I don't know if this is the correct way to do this however.  write asks for a constant void*, but I want to send 3 one-byte char messages.  Here is a piece of my code.  PLEASE HELP, I've been working on this for 2 days already.

void send_command_gripper(ssc *s, short unsigned sync,  short unsigned servo, short unsigned position) {//////FIX ME! HOW DO I SEND A BYTE?!
  //unsigned char charsync=(unsigned char)sync;
  //cout<<"buf: "<<buf;
  char buf[3];
  snprintf(buf, 3,"%c%c%c",sync, servo, position);
  cout<<"BUF "<<buf<<'
';
  outstream.write(
  write(s->fd,buf,3);
}


Answer
Might I first suggest you re-read the man page for write and check the returned value and handle error returns. Then you might use something like:

unsigned char message( sync );
if ( write(s->fd, &message, 1)!=1 )
{
  // handle error
}

message = servo;
if ( write(s->fd, &message, 1)!=1 )
{
  // handle error
}

message = position;
if ( write(s->fd, &message, 1)!=1 )
{
  // handle error
}

The above should achieve what you asked: 3 calls to write, each sending 1 byte of the whole command. You can of course modify the exact details of the error handling logic as required, for example one variation would be:

unsigned char message( sync );
if ( 1==write(s->fd, &message, 1) )
{
   message = servo;
   if ( 1==write(s->fd, &message, 1) )
   {
       message = position;
       if ( write(s->fd, &message, 1)!=1 )
       {
          // command bytes sent OK
       }
       else
       {
          // handle error
       }
   }
   else
   {
      // handle error
   }
}
else
{
  // handle error
}

You will note that I use an unsigned char to hold the message and pass its address to write as write requires a pointer to the data it sends.

Please note that the code presented here is for example use only and I have not tested it or even compiled it. I apologise for any typos or mistakes.

In your original code I have no idea what the line:

  outstream.write(

is supposed to achieve. And why you did not just do something like:

buf[0] = sync;
buf[1] = servo;
buf[2] = position;

rather than use snprintf (a sledge hammer to crack a nut) I do not know. I would also have though that unsigned char would have been the correct type for the sync, servo and position parameters, then you could do away with the message local variable and just write:

if ( write(s->fd, &sync, 1)!=1 )
{
  // handle error
}

if ( write(s->fd, &servo, 1)!=1 )
{
  // handle error
}

if ( write(s->fd, &position, 1)!=1 )
{
  // handle error
}

Hope you find this useful.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.