libftdi Archives

Subject: problems with US232R and serial connection

From: Matthias Janke <janke@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Tue, 10 Feb 2009 12:34:01 +0100
Hi,

I'm having problems with a serial connection. My setup is the folowing:
Device: 19200 8N1 RTS/CTS HW flowcontrol + echo
Serialport: US232R
The protocol says a Command begins with a '#' and ends with a '\r' after the
'\r' I should get an answer in the form of "\nok1\r" while executing my program
which should write '#2\r' I get an error -75 from libusb reading the \r for
echocancelation reading the answer fails too. Now here is my program:

#include <string.h>
#include <stdio.h>

#include <ftdi.h>

#define MAX_CMD_LEN 60

struct stoegra_reference
{
  char status[MAX_CMD_LEN + 1];
};

int stoegra_open_ftdi(struct ftdi_context *ftdic, char *iserial)
{
  if ( ftdi_init(ftdic) < 0 )
    return -1;

  if ( ftdi_usb_open_desc(ftdic, 0x0403, 0x6001, "US232R", iserial) < 0 ) 
    return -2;
  
  if ( ftdi_set_baudrate(ftdic, 19200) < 0 )
    return -3;
   
  if ( ftdi_set_line_property(ftdic, BITS_8, STOP_BIT_1, NONE) < 0 ) 
    return -4;

  if ( ftdi_read_data_set_chunksize(ftdic, 3) < 0 )
    return -5;
 
  if ( ftdi_write_data_set_chunksize(ftdic,1) < 0 )
    return -6;

  if ( ftdi_setflowctrl(ftdic, SIO_RTS_CTS_HS) < 0 )
    return -7;

  if ( ftdi_setrts(ftdic, 0) < 0 )
    return -8;

  if ( ftdi_usb_purge_buffers(ftdic) < 0 )
    return -9;
  
  return 0;
}

int stoegra_close_ftdi(struct ftdi_context *ftdic)
{
  if ( ftdi_usb_purge_buffers(ftdic) < 0 )
    return -1;

  if ( ftdi_usb_reset(ftdic) < 0 )
    return -2;

  if ( ftdi_usb_close(ftdic) < 0 )
    return -3;

  ftdi_deinit(ftdic);
 
  return 0;
}


int stoegra_write_byte(struct ftdi_context *ftdic, char byte, int data_echoed)
{
  int n = 0;
  unsigned char msg[] = {0, 0};
  unsigned char echo[] = {0, 0};

  msg[0]=byte;

  while (n != 1)
    n = ftdi_write_data(ftdic, msg, 1);

  n = 0;
  if (data_echoed)
  {
    while ( n != 1 )
    {
      n = ftdi_read_data(ftdic, echo, 1);
      printf("n:%i\t%02x\n", n, echo[0]);      
      if( n == -75 )
        n = 1;
     }

    if (echo[0] != msg[0])
      return -1;
  }

  return 0;
}

int stoegra_status(struct ftdi_context *ftdic, struct stoegra_reference *sr)
{
  int i, n;
  unsigned char ret[] = {'Q', 0};
  
  for ( i = 0; ret[0] != '\r'; i++ )
  {
    n = 0;
    while ( n != 1 )
    {
      n = ftdi_read_data(ftdic, echo, 1);
      printf("n:%i\t%c\n", n, ret[0]);      
      if (n==-75)
        n=1;
    }
    sr->status[i] = ret[0];
  }
  sr->status[i+1] = 0;

  if(strlen(sr->status) == 2)
  {
    ftdi_usb_purge_rx_buffer(ftdic);
    return -1;
  }

  return 0;
}

int stoegra_cmd_status(struct ftdi_context *ftdic)
{
  char status[] = {0, 0};
  struct stoegra_reference sr;

  if ( stoegra_status(ftdic, &sr) < 0 )
    return -1;

   status[0] = sr.status[3];

  return atoi(status);
}

int main(void)
{
  struct ftdi_context ftdic;

  stoegra_open_ftdi(&ftdic);
  stoegra_write_byte(&ftdic,'#', 1);
  stoegra_write_byte(&ftdic,'2', 1);
  stoegra_write_byte(&ftdic,'\r', 1);
  stoegra_cmd_status(&ftdic);
  return 0;
}

does anyone have an idea why it doesn't work.
Cheers,

Matthias


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread