Fwd: Re: C program to write to the com port - RESOLVED

2005-09-09 Thread vittorio

As a C++ absolute beginner I'm trying to compile your testssc.c file with

g++ testssc.c -o testssc
(under freebsd 5.4, gcc version 3.4.2)

But...
I only get:

SerialPort.C: In function `int main(int, char*)':
SerialPort.C:62: error: invalid conversion from `unsigned char*' to `char*'
SerialPort.C:62: error:   initializing argument 1 of `int snprintf(char*, 
size_t, const char*, ...)'
SerialPort.C:66: error: `err' undeclared (first use this function)
SerialPort.C:66: error: (Each undeclared identifier is reported only once for 
each function it appears in.)
SerialPort.C:69:3: warning: no newline at end of file

Could you please help to straighten things up?

Vittorio

--  Messaggio inoltrato  --

Subject: Re: C program to write to the com port - RESOLVED
Date: 02:23, venerdì 09 settembre 2005
From: Paul Hamilton [EMAIL PROTECTED]
To: freebsd-questions@freebsd.org

.


/*  Name: testssc.c
 *  compile with:  gcc testssc.c -o testssc
 *
 *  Your serial cable should be plugged into com port 1.
 *  You only need the pin 3 and pin 5 (DB9) plugged into the controller.
 *  The servo should be plugged into the first servo channel/port.
 *  This test program when run will move the servo from midrange,
 *  to position 01.  This is for demonstrational use only.
 *  Tested with FreeBSD 5.4
 *  Paul Hamilton  8th Aug 2005
 */

#include sys/time.h
#include sys/ioctl.h
#include errno.h
#include fcntl.h
#include termios.h /*Originally it was termio.h*/
#include stdio.h
#include unistd.h
// Use serial port 0  (com port 1)
static char *opt_comport=/dev/cuaa0;

int main(int argc, char **argv)
{
int fd;
struct termios options;
unsigned char buf[4];

// ok, lets try opening the com port
printf(Opening Com port: %s\n\n, opt_comport);
if((fd = open(opt_comport, O_RDWR | O_NOCTTY ))  0)
  {
 printf(Problems opening %s\n, opt_comport);
 return (-1);
  }
// set the required com port parrameters
options.c_cflag = ~CSIZE;  /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag = ~PARENB; // set no parity
options.c_cflag = ~CSTOPB; // set 1 stop bit
options.c_oflag = ~OPOST;  // Raw output

tcgetattr(fd, options);

/*
 * Set the baud rates to 9600...
 */
cfsetispeed(options, B9600);
cfsetospeed(options, B9600);

/*
 * Enable the receiver and set local mode...
 */
options.c_cflag |= (CLOCAL | CREAD);

/*
 * Set the new options for the port...
 */
tcsetattr(fd, TCSANOW, options);

// ok, lets transmit our 3 bytes through com port 1
snprintf(buf,4,%c%c%c%c,0xff,0x00,0x01,0);
printf(buf=%x,%x,%x,%x\n, buf[0],buf[1],buf[2],buf[3]);

  if (write(fd, buf, 3) != 3)
err(1, write);

close(fd);
};



Cheers,

Paul Hamilton

PS.  I have three books on programming in C winging their way to Australia.
I have a lot to learn  :-)

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]

---
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Fwd: Re: C program to write to the com port - RESOLVED

2005-09-09 Thread Giorgos Keramidas
On 2005-09-09 13:53, vittorio [EMAIL PROTECTED] wrote:

 As a C++ absolute beginner I'm trying to compile your testssc.c file with

 g++ testssc.c -o testssc
 (under freebsd 5.4, gcc version 3.4.2)

It's not a C++ program.  You should use `cc', not `g++'.

 SerialPort.C: In function `int main(int, char*)':
 SerialPort.C:62: error: invalid conversion from `unsigned char*' to `char*'
 SerialPort.C:62: error:   initializing argument 1 of `int snprintf(char*,
 size_t, const char*, ...)'
 SerialPort.C:66: error: `err' undeclared (first use this function)
 SerialPort.C:66: error: (Each undeclared identifier is reported only once for
 each function it appears in.)
 SerialPort.C:69:3: warning: no newline at end of file

 Could you please help to straighten things up?

The snprintf() function is what's causing you trouble in this line:

snprintf(buf,4,%c%c%c%c,0xff,0x00,0x01,0);

As I said to Paul, in personal email messages, when there is a structure that
the serial data has to conform too, I usually prefer using explicitly named
fields in structs, temporary buffers, and memcpy() or plain assignments
instead of printf()-family functions.

#define SERVO_CMD_MAXBUF4

struct servo_cmd {
unsigned char sc_id;
unsigned char sc_cmd;
unsigned char sc_arg;
};

int
servo_cmd_send(struct servo_cmd *sp)
{
unsigned char buf[SERVO_CMD_MAXBUF];

buf[0] = sp-sc_id;
buf[1] = sp-sc_cmd;
buf[2] = sp-sc_arg;
buf[3] = '\0';  /* Command end char. */

...
}

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Fwd: Re: C program to write to the com port - RESOLVED

2005-09-09 Thread Roland Smith
On Fri, Sep 09, 2005 at 01:53:49PM +0200, vittorio wrote:
 
 As a C++ absolute beginner I'm trying to compile your testssc.c file with
 
 g++ testssc.c -o testssc
 (under freebsd 5.4, gcc version 3.4.2)

The C and C++ languages are different. Use cc or gcc instead of g++ to
compile C code.

Roland
-- 
R.F.Smith (http://www.xs4all.nl/~rsmith/) Please send e-mail as plain text.
public key: http://www.xs4all.nl/~rsmith/pubkey.txt


pgpOtOStnYFks.pgp
Description: PGP signature


Re: Fwd: Re: C program to write to the com port - RESOLVED

2005-09-09 Thread Helge Preuss

vittorio wrote:


[...]
SerialPort.C: In function `int main(int, char*)':
SerialPort.C:62: error: invalid conversion from `unsigned char*' to `char*'
SerialPort.C:62: error:   initializing argument 1 of `int snprintf(char*, 
size_t, const char*, ...)'

SerialPort.C:66: error: `err' undeclared (first use this function)
SerialPort.C:66: error: (Each undeclared identifier is reported only once for 
each function it appears in.)

SerialPort.C:69:3: warning: no newline at end of file

Could you please help to straighten things up?
 


A relevant line from man snprintf:
 int snprintf(char *str, size_t size, const char *format, ...);
So you might want to change line 27 to
   char buf[4];
and perhaps line 62 too:

 snprintf(buf,4,%c%c%c%c,(char)0xff,0x00,0x01,0);
another relevant line from man err:

 #include err.h


HTH, haven't tried it

Helge


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]