Serial port problem: Output is scrambled.

Serial port problem: Output is scrambled.

Post by Mikeywild » Mon, 02 Oct 2000 12:45:55

To all:

I'm writing C code to control a radio transceiver.  The radio's interface
requires a serial (RS232) port, 4800 bps, no parity, 8 data bits, 2 stop bits.
My code is sending data to the serial port (I can see the LED indicators on my
RS232 break-out box flash).  However, the radio does not respond.  I plugged
the cable into a Wyse data terminal and set the Wyse to the above
specifications.  When I run the program, the break-out box LEDs flash and I see
scrambled output on the terminal.  This seems to indicate a problem with the
serial settings.  I double checked my code and wrote in some commands to view
the termios flags after I performed the tcsetattr command to put the port
(/dev/ttyS1) in raw mode.  The following was reported:

input flag c_iflag = 0x0000
output flag c_oflag = 0x0000
control flag c_cflag = 0x0cfc
local flag c_lflag = 0x0a30
line discipline = 0x00
input speed = 0x08049ca4 = 134519972
output speed = 0xbffffce4 =3221224676

The control flag (0x0cfc) is 0000 1100 1111 1100 which indicates a speed of
4800 bps, CS8, 2 stop bits, no parity and HUPCL and CLOCAL flags set.  The
local flag (0x0a30) is 0000 1010 0011 0000.  The ECHOE flag is not set so all
other ECHOE flags are ignored.   This is what I want.  I am using unsigned
characters so the bytes will transfer correctly.  What am I missing?  The
following is the shortened version of my code.

Thanks in advance for any help.

Mike Wilder

/* sertest2 program
   created 9-29-00 by Mike Wilder
   tests basic serial port operation of opening, writing to serial port
   tests sending a basic command to the Yaesu FT-480 radio.

#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
   int tfd;     /* terminal (serial port) file descriptor */
   int i;
   struct termios oldtty, tty;

   char port[]="/dev/ttyS1";
   char speed[80];
   unsigned char null = 0x00;
   unsigned char step_code = 0x8e;
   unsigned char hg_code = 0x0d;
   unsigned char ham_code = 0x00;
   unsigned char gen_code = 0x01;
   unsigned char mtr_code = 0xf7;
   unsigned char command[6];

   tfd = open(port, O_RDWR, O_SYNC);
   if (tfd < 0 ) {
      return -1;

   tcgetattr(tfd, &oldtty); /* save original port settings */
   tcgetattr(tfd, &tty);
   tty.c_cflag |= CSTOPB;
   tty.c_oflag &= ~ONLCR;   /* not necessary? Sets c_oflag = 0 */
   cfsetospeed(&tty, B4800);
   cfsetispeed(&tty, B4800);
   tcsetattr(tfd, TCSANOW, &tty);
   if (cfgetospeed(&tty) != B4800)
      printf("output speed is not 4800 bps.\n");

/*  Create the 5 byte code sequence to send the radio */  
      command[i] = null;
   /* tcsendbreak(tfd, 1); */
   write(tfd, command, 5);

   tcsetattr(tfd, TCSANOW, &oldtty);

   return 0;