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: Re: C program to write to the com port - RESOLVED

2005-09-09 Thread Paul Hamilton
Hi vittorio,

Hey I am just a beginner myself, but this is a C program, not a C++ program.
My gcc is version 3.4.2 too.

Hmm, did you cut and paste ALL of the source code, including the #include
statements?  I also see that it's mentioning code on lines 62, 66 and 69.
My testssc.c only has around 57 lines of code.  What else have you included?


Cheers,

Paul

 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of vittorio
 Sent: Friday, 9 September 2005 7:54 PM
 To: freebsd-questions@freebsd.org
 Subject: Fwd: Re: C program to write to the com port - RESOLVED
 
 
 
 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]
 
 

___
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]


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

2005-09-08 Thread Paul Hamilton
Hi,

Thanks to the patience and persistens of Giorgos, Garret and David, I now
have a *sample* program that will transmit 3 bytes of data (mini-ssc
protocol), via a serial port to a 8 channel servo controller board.  I will
continue to develop this as needed.  

DISCLAIMER:  This is being posted for archival purposes, and no doubt can
use a lot of improvement!



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


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

2005-09-08 Thread Giorgos Keramidas
On 2005-09-09 08:23, Paul Hamilton [EMAIL PROTECTED] wrote:
 Thanks to the patience and persistens of Giorgos, Garret and David, I now
 have a *sample* program that will transmit 3 bytes of data (mini-ssc
 protocol), via a serial port to a 8 channel servo controller board.  I will
 continue to develop this as needed.

 DISCLAIMER:  This is being posted for archival purposes, and no doubt can
 use a lot of improvement!

Thanks so much for taking the time to post a followup!

Nicely commented program.  I really liked the way the terminal option
changes are explained.

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


C program to write to the com port

2005-07-31 Thread Paul Hamilton
Hi,

I am trying to write a C program that will send 3 bytes to the cuaa0 com
port at 9600 baud, 8n1.  I am trying to control a Northlight 8 Channel Servo
motor controller:
http://home.att.net/~northlightsystems/DMX512toRCservo.htm

Most of the code came from this page:
http://www.easysw.com/~mike/serial/serial.html

Here is what I have so far:-
---

#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
static char *opt_comport=/dev/cuaa0;

int main(int argc, char **argv)
{
int n;
int dcf_dev;
int sdata =  0xFF0090;  // sync byte, address, servo value to be sent to
the Servo Controller
struct termios options;

// ok, lets try opening the com port
printf(Opening Com port: %s\n\n, opt_comport);
if((dcf_dev = open(opt_comport, O_RDWR | O_NOCTTY | O_NDELAY))  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(dcf_dev, 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(dcf_dev, TCSANOW, options);

// ok, lets transmit our 3 bytes to com port 1
n = write(dcf_dev, sdata, 3);
if (n  0)
fputs(write() of 3 bytes failed!\n, stderr);
printf(Output status: %d bytes written out\n, n);
exit(1);
};

---

Now I am just a beginner at C code, so I feel pretty good getting this far
(hey, it compiles :-)  However, a miss is as good as a mile, and so it
doesn't work :-(   Having said that, I have a serial port LED breakout
device watching, and I can see a blip on the TX line when I run the compiled
program.  This is just meant to be test code, i.e.. Get it working before
cleaning it up etc :-)

I have tried connecting the computers serial port to another one, running:
'cu -s 9600 -l cuaa0' but I don't see anything.  Having said that, I don't
see anything if I run the same on the other PC (yes, the TX-RX lines are
swapped over), so maybe that is a problem with my serial cable between the
two computers.

The Servo Controller only needs two wires:  signal ground and TX  so not
much to go wrong there, and as I said above, I do see a blip on the TX LED
when I run the program.


Questions:

1. Am I really sending the data correctly at 9600baud, 8n1?

2. Am I really sending the hex bytes:  FF 00 90  out (or am I sending an
pointer address)?

3. What am I missing?


Thanks.

Cheers,

Paul Hamilton

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


Re: C program to write to the com port

2005-07-31 Thread Giorgos Keramidas
On 2005-07-31 22:50, Paul Hamilton [EMAIL PROTECTED] wrote:
 Hi,

 I am trying to write a C program that will send 3 bytes to the cuaa0 com
 port at 9600 baud, 8n1.  I am trying to control a Northlight 8 Channel Servo
 motor controller:
 http://home.att.net/~northlightsystems/DMX512toRCservo.htm

 Most of the code came from this page:
 http://www.easysw.com/~mike/serial/serial.html

 Here is what I have so far:-
 ---

 #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
 static char *opt_comport=/dev/cuaa0;

 int main(int argc, char **argv)
 {
 int n;
 int dcf_dev;
 int sdata =  0xFF0090;  // sync byte, address, servo value to be sent to
 the Servo Controller
 struct termios options;

 // ok, lets try opening the com port
 printf(Opening Com port: %s\n\n, opt_comport);
 if((dcf_dev = open(opt_comport, O_RDWR | O_NOCTTY | O_NDELAY))  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(dcf_dev, 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(dcf_dev, TCSANOW, options);

 // ok, lets transmit our 3 bytes to com port 1
 n = write(dcf_dev, sdata, 3);
 if (n  0)
 fputs(write() of 3 bytes failed!\n, stderr);
 printf(Output status: %d bytes written out\n, n);
 exit(1);
 };

 ---

 Now I am just a beginner at C code, so I feel pretty good getting this far
 (hey, it compiles :-)  However, a miss is as good as a mile, and so it
 doesn't work :-(   Having said that, I have a serial port LED breakout
 device watching, and I can see a blip on the TX line when I run the compiled
 program.  This is just meant to be test code, i.e.. Get it working before
 cleaning it up etc :-)

 I have tried connecting the computers serial port to another one, running:
 'cu -s 9600 -l cuaa0' but I don't see anything.  Having said that, I don't
 see anything if I run the same on the other PC (yes, the TX-RX lines are
 swapped over), so maybe that is a problem with my serial cable between the
 two computers.

 The Servo Controller only needs two wires:  signal ground and TX  so not
 much to go wrong there, and as I said above, I do see a blip on the TX LED
 when I run the program.

 Questions:

 1. Am I really sending the data correctly at 9600baud, 8n1?

 2. Am I really sending the hex bytes:  FF 00 90  out (or am I sending an
 pointer address)?

No.  You are using:

int sdata = 0xFF0090;

There's no guarantee that an ``int'' can hold a value greater than
0x7FFF.  This is the minimum required by the C standard (pp. 22 of
ISO/IEC 9899:1999(E)).  You should use types like uint32_t if you have
specific size requirements.

You are then passing this as a `buffer' to write(), but this is buggy
because you don't know (and shouldn't really care) what the endianess of
the machine you're using is.  This means that the value 0xFF0090, if we
do assume that sizeof(int) is 4 bytes, may be stored either as:

Low memory addressesHigh memory addresses
00 FF 00 90 (big endian order)
90 00 FF 00 (little endian order)

None of these is correct, as far as your real intention is concerned.

Try using a more explicit buffer-like structure for your data bytes,
like for instance:

int fd;
unsigned char buf[] = { 0xFF, 0x00, 0x90 };
size_t buflen = sizeof(buf) / sizeof(buf[0]);

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

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


Re: C program to write to the com port

2005-07-31 Thread Garrett Cooper

Paul Hamilton wrote:


Hi,

I am trying to write a C program that will send 3 bytes to the cuaa0 com
port at 9600 baud, 8n1.  I am trying to control a Northlight 8 Channel Servo
motor controller:
http://home.att.net/~northlightsystems/DMX512toRCservo.htm

Most of the code came from this page:
http://www.easysw.com/~mike/serial/serial.html

Here is what I have so far:-
---

   #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
   static char *opt_comport=/dev/cuaa0;

int main(int argc, char **argv)
{
   int n;
   int dcf_dev;
   int sdata =  0xFF0090;  // sync byte, address, servo value to be sent to
the Servo Controller
   struct termios options;

   // ok, lets try opening the com port
   printf(Opening Com port: %s\n\n, opt_comport);
   if((dcf_dev = open(opt_comport, O_RDWR | O_NOCTTY | O_NDELAY))  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(dcf_dev, 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(dcf_dev, TCSANOW, options);

   // ok, lets transmit our 3 bytes to com port 1
   n = write(dcf_dev, sdata, 3);
   if (n  0)
   fputs(write() of 3 bytes failed!\n, stderr);
   printf(Output status: %d bytes written out\n, n);
   exit(1);
};

---

Now I am just a beginner at C code, so I feel pretty good getting this far
(hey, it compiles :-)  However, a miss is as good as a mile, and so it
doesn't work :-(   Having said that, I have a serial port LED breakout
device watching, and I can see a blip on the TX line when I run the compiled
program.  This is just meant to be test code, i.e.. Get it working before
cleaning it up etc :-)

I have tried connecting the computers serial port to another one, running:
'cu -s 9600 -l cuaa0' but I don't see anything.  Having said that, I don't
see anything if I run the same on the other PC (yes, the TX-RX lines are
swapped over), so maybe that is a problem with my serial cable between the
two computers.

The Servo Controller only needs two wires:  signal ground and TX  so not
much to go wrong there, and as I said above, I do see a blip on the TX LED
when I run the program.


Questions:

1. Am I really sending the data correctly at 9600baud, 8n1?

2. Am I really sending the hex bytes:  FF 00 90  out (or am I sending an
pointer address)?

3. What am I missing?


Thanks.

Cheers,

Paul Hamilton
 

A few things I'm curious about (and thanks for the webpage-it might be 
helpful for me in the future with programming robots and Tern boards for 
coursework):


Is options.c_cflag set to a default value or is it unknown prior to 
runtime? This would cause issues with your = statement being the first one.
Another thing I noticed from the website is that you actually have to 
send information to the board itself, then you have to send information 
onto the servo, as to what channel on the controller you want to send 
the information to. So, you need to specify somehow via a Hex address 
what individual address on the controller you want to send the data in 
your message, then send the data that way. That requires some research 
of course and it looks like you tried to get that working, but the value 
that you input may differ. Plus, the servo value defaulted to the 1st 
address on the controller (as per the 1st webpage), so what I would do 
is iterate through channels 1 to 8 (actually iterate from 0 to 7 though 
as this is C and not something like Matlab..) to find which controller 
is actually controlling your servo, and then you have a match.
Also, are there any reserved COM addresses that you need to watch out 
for on the controller board (Yes, there are. I answered my own question 
later on...)? The Tern TD-40 board that we use at school has several 
addresses which are reserved for serial communication with the PC 
(output ports), so we have to use a set of addresses at a particular 
offset in order for the program we make to work with certain resources 
on the board. I assume something similar takes place with your controller.
When looking at the information, keep this in mind (referring to the 
documentation on the 1st webpage):


To send a new output level command, 3 bytes are needed.
The first is a “sync” byte with a 

Re: C program to write to the com port

2005-07-31 Thread David Kelly


On Jul 31, 2005, at 9:50 AM, Paul Hamilton wrote:

I am trying to write a C program that will send 3 bytes to the  
cuaa0 com

port at 9600 baud, 8n1.


Oddly I found information on how to do that very easy to find for  
Unix. Conversely wanted to do the same thing in Windows and hit a  
brick wall. Everyone does it differently with different compilers. So  
I told the boss a Windows Weenie would have to take over.


Had a perfectly good minimal text-only program which formed serial  
Modbus ASCII packets, and displayed the response, under FreeBSD.  
Would be interested in a library for mingw or similar with termios  
cloned. Else some simple documented way to do unbuffered serial I/O.  
ASCII Modbus doesn't really require unbuffered I/O but thats the path  
I started on and would be required for Modbus RTU mode.


--
David Kelly N4HHE, [EMAIL PROTECTED]

Whom computers would destroy, they must first drive mad.

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


Re: C program to write to the com port

2005-07-31 Thread Garrett Cooper

Giorgos Keramidas wrote:


On 2005-08-01 00:23, Paul Hamilton [EMAIL PROTECTED] wrote:
 


Yes, very full on code ;-)   I will see if I can get it to compile!

Thanks for that.  In the mean time I pondered the endianess thing, and I
have tried using my original code to write 1 byte at a time, 3 times to the
com port, i.e.:

   // ok, lets transmit our 3 bytes to com port 1
   n = write(dcf_dev, 0xFF, 1);
   if (n  0)
   fputs(write() of 1 byte failed!\n, stderr);
   printf(Output status: %d bytes written out\n, n);
   



That's wrong.  You are not writing the binary value 0xFF but the first
character of the string 0xFF, which is '0'.  Why don't you really use
a buffer, like I said?

#include err.h
#include unistd.h

unsigned char buf = { 0xFF, 0x00, 0x90 };
size_t buflen = sizeof(buf) / sizeof(buf[0]);

if (write(dcf_dev, buf, buflen) != buflen) {
err(1, write);

The use of err() for printing the error message is the preferred way of
showing why things have failed, so avoid cryptic messages that seem
informational but in reality hide the reason of the failure, like:

failed to write byte 1

The failure is reported, but what is missing here is the reason for the
failure, which is more useful knowledge :-)

 


   n = write(dcf_dev, 0x01, 1);
   if (n  0)
   fputs(write() of 1 byte failed!\n, stderr);
   printf(Output status: %d bytes written out\n, n);
   



Same bug as above.

 


   n = write(dcf_dev, 0x31, 1);
   if (n  0)
   fputs(write() of 1 byte failed!\n, stderr);
   printf(Output status: %d bytes written out\n, n);
   



Ditto.

 


This way, I thought I should be able to get around all of the endianess of
it all.
   



No.  Unless you start writing binary data in your string constants,
you are not going to make it work.  One way of doing this is:

write(dcf_dev, \xff\x00\x90, 3);

But using string constants for binary data is going to get tricky
immediately after you find yourself in the need for embedding the string
terminating ASCII NUL character, '\0'.

Just don't :-)

 


How are you on Serial port programming?  Do you think I have the port
programmed properly?
   



The serial port setup part seemed fairly correct.  Now you must make
sure you send the correct binary data to it.
 

   As for buffering, it's an incredibly great idea because otherwise 
your data will become corrupted in transit. That's an issue I'm dealing 
with in terms of programming a simple flow control system for an 
interface with my Tern board. For me, since we are using semi-medium 
sized buffers, I needed to use COM port buffers 4 times the actual size 
of my data being sent, because of corruption issues. That just was 
required because of the algorithms in use.
   Another way to determine sizes, search for limits.h under 
/usr/ssys/i386 (I think that was the location, as I don't have my 
machine in front of me). Another way to do it under linux would be 
/usr/src/limits.h for sure.

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