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

Giorgos Keramidas keramida at ceid.upatras.gr
Fri Sep 9 05:39:56 PDT 2005


On 2005-09-09 13:53, vittorio <vdemart1 at tin.it> 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_MAXBUF	4

	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. */

		...
	}



More information about the freebsd-questions mailing list