[prev in list] [next in list] [prev in thread] [next in thread] 

List:       libftdi
Subject:    Re: FT232RL Reading DMX
From:       Rui Barreiros <rui.barreiros () audioluz ! net>
Date:       2012-05-31 10:24:34
Message-ID: 4FC746E2.4050307 () audioluz ! net
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hello Thomas,

I did a ftdi_read_data_set_chunksize(ftdi, 513) and commented out the 
purge, still the data I'm receiving is not 'correct' there are 512 
positions (the 1st of the 513 array is the start code which is always 
0x00) which correspond to a DMX channel, and I keep getting the correct 
value but always on the wrong array positions, they keep changing.

Also, the break interrupt seems to be slower than what it should be.

Attached goes my latest test code.

Best regards,


On 05/30/2012 05:57 PM, Thomas Jarosch wrote:
> Hi Rui,
>
> On Wednesday, 30. May 2012 17:09:04 Rui Barreiros wrote:
>>     while(!exitRequested) {
>>       ftdi_poll_modem_status(ftdi,&status);
>>
>>       if(status&  0x1000) { // BREAK_INTERRUPT
>>         f = ftdi_read_data(ftdi, buff, sizeof(buff));
>>         if(f<  0)
>>           sleep(1);
>>         else if(f>  0) {
>>           ftdi_usb_purge_rx_buffer(ftdi);
>>           fprintf(stdout, "Read %d bytes\n", f);
>>           for(i = 0; i<  sizeof(buff); i++) {
>>             fprintf(stdout, "%02X ", ((unsigned char *) buff)[i] );
>>           }
> I'm not sure if the ftdi_usb_purge_rx_buffer() is a good idea:
>
> ftdi_read_data() reads data in "readbuffer_chunksize" blocks,
> usually 4096 bytes. If the "sizeof(buff)" (=513) is smaller than that
> and the chip returns more data, it will be buffered internally.
>
> Next time you call ftdi_read_data(), it might return data
> from the internal read buffer. The problem is that
> ftdi_usb_purge_rx_buffer() clears the internal read buffer,
> so you will experience data loss.
>
>
> Do you really need ftdi_usb_purge_rx_buffer()?
>
> Thomas


-- 

    *Rui Barreiros*
    rui.barreiros@audioluz.net
    Tlm: +351 962 356 020

	Rua Alminhas das Cais, 950
4410-497 Serzedo VNG
Portugal
NIF: 506 107 523
	Tlm: +351 968 015 215
Tlf: +351 227 625 805
Fax: +351 227 534 304
geral@audioluz.net





--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@developer.intra2net.com   
[Attachment #5 (multipart/related)]

[Attachment #7 (text/html)]

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hello Thomas,<br>
    <br>
    I did a ftdi_read_data_set_chunksize(ftdi, 513) and commented out
    the purge, still the data I'm receiving is not 'correct' there are
    512 positions (the 1st of the 513 array is the start code which is
    always 0x00) which correspond to a DMX channel, and I keep getting
    the correct value but always on the wrong array positions, they keep
    changing.<br>
    <br>
    Also, the break interrupt seems to be slower than what it should be.<br>
    <br>
    Attached goes my latest test code.<br>
    <br>
    Best regards,<br>
    <br>
    <br>
    On 05/30/2012 05:57 PM, Thomas Jarosch wrote:
    <blockquote
      cite="mid:201205301857.02650.thomas.jarosch@intra2net.com"
      type="cite">
      <pre wrap="">Hi Rui,

On Wednesday, 30. May 2012 17:09:04 Rui Barreiros wrote:
</pre>
      <blockquote type="cite">
        <pre wrap="">   while(!exitRequested) {
     ftdi_poll_modem_status(ftdi, &amp;status);

     if(status &amp; 0x1000) { // BREAK_INTERRUPT
       f = ftdi_read_data(ftdi, buff, sizeof(buff));
       if(f &lt; 0)
         sleep(1);
       else if(f &gt; 0) {
         ftdi_usb_purge_rx_buffer(ftdi);
         fprintf(stdout, "Read %d bytes\n", f);
         for(i = 0; i &lt; sizeof(buff); i++) {
           fprintf(stdout, "%02X ", ((unsigned char *) buff)[i] );
         }
</pre>
      </blockquote>
      <pre wrap="">
I'm not sure if the ftdi_usb_purge_rx_buffer() is a good idea:

ftdi_read_data() reads data in "readbuffer_chunksize" blocks,
usually 4096 bytes. If the "sizeof(buff)" (=513) is smaller than that
and the chip returns more data, it will be buffered internally.

Next time you call ftdi_read_data(), it might return data
from the internal read buffer. The problem is that
ftdi_usb_purge_rx_buffer() clears the internal read buffer,
so you will experience data loss.


Do you really need ftdi_usb_purge_rx_buffer()?

Thomas
</pre>
    </blockquote>
    <br>
    <br>
    <div class="moz-signature">-- <br>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <title></title>
      <br>
      <table border="0" cellpadding="2" cellspacing="2" width="100%">
        <tbody>
          <tr>
            <td colspan="3" rowspan="1" valign="top">
              <blockquote><b>Rui Barreiros</b><br>
                <a class="moz-txt-link-abbreviated" \
href="mailto:rui.barreiros@audioluz.net">rui.barreiros@audioluz.net</a><br>  Tlm: \
+351 962 356 020<br>  </blockquote>
            </td>
          </tr>
          <tr>
            <td valign="top" width="300"><img alt=""
                src="cid:part1.05030909.06000402@audioluz.net"
                height="65" width="286" align="left"></td>
            <td valign="top">Rua Alminhas das Cais, 950<br>
              4410-497 Serzedo VNG<br>
              Portugal<br>
              NIF: 506 107 523<br>
            </td>
            <td valign="top">Tlm: +351 968 015 215<br>
              Tlf: +351 227 625 805<br>
              Fax: +351 227 534 304<br>
              <a class="moz-txt-link-abbreviated" \
href="mailto:geral@audioluz.net">geral@audioluz.net</a><br>  </td>
          </tr>
        </tbody>
      </table>
      <br>
      <br>
    </div>
  
<br>
<hr><p><b>libftdi</b>&nbsp;-&nbsp;see <a \
href="http://www.intra2net.com/en/developer/libftdi">http://www.intra2net.com/en/developer/libftdi</a> \
for details.<br> To unsubscribe send a mail to <a \
href="mailto:libftdi+unsubscribe@developer.intra2net.com">libftdi+unsubscribe@developer.intra2net.com</a></p>
 <br>
</body>
</html>


["logo_65.png" (image/png)]
["test.c" (text/x-csrc)]

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <ftdi.h>

static int exitRequested = 0;

static void
sigintHandler(int signum)
{
  fprintf(stderr, "Shutdown requested\n");
  exitRequested = 1;
}

int main(int argc, char **argv)
{
  int f = 0, i = 0, tmp = 0;
  unsigned short status = 0;
  struct ftdi_context *ftdi;
  unsigned char buff[513];
  int VID = 0x0403;
  int PID = 0x6001;

  if ((ftdi = ftdi_new()) == 0) {
    fprintf(stderr, "ftdi_new failed\n");
    return EXIT_FAILURE;
  }

  fprintf(stdout, "Opening...\n");
  if(ftdi_usb_open_desc(ftdi, VID, PID, "USB Serial Converter", "MYSERIALNUMBER") < 0) {
    fprintf(stderr, " ftdi_usb_open failed\n");
    fprintf(stderr, "%s\n", ftdi_get_error_string(ftdi));
    exit -1;
  }

  fprintf(stdout, "Reset...\n");
  if(ftdi_usb_reset(ftdi) < 0) {
    fprintf(stderr, " ftdi_usb_reset failed\n");
    fprintf(stderr, "%s\n", ftdi_get_error_string(ftdi));
    exit -1;
  }

  fprintf(stdout, "Setting Line Properties...\n");
  if(ftdi_set_line_property2(ftdi, BITS_8, STOP_BIT_2, NONE, BREAK_ON) < 0) {
    fprintf(stderr, " ftdi_set_line_property failed\n");
    fprintf(stderr, "%s\n", ftdi_get_error_string(ftdi));
    exit -1;
  }
  
  fprintf(stdout, "Setting Baudrate...\n");
  if(ftdi_set_baudrate(ftdi, 250000) < 0) {
    fprintf(stderr, " ftdi_set_baudrate failed\n");
    fprintf(stderr, "%s\n", ftdi_get_error_string(ftdi));
    exit -1;
  }

  fprintf(stdout, "Setting Flow Ctrl...\n");
  if(ftdi_setflowctrl(ftdi, SIO_RESET) < 0) {
    fprintf(stderr, " ftdi_set_setflowctrl failed\n");
    fprintf(stderr, "%s\n", ftdi_get_error_string(ftdi));
    exit -1;
  }

  fprintf(stdout, "Purging Buffers...\n");
  if(ftdi_usb_purge_buffers(ftdi) < 0) {
    fprintf(stderr, " ftdi_usb_purge_buffers failed\n");
    fprintf(stderr, "%s\n", ftdi_get_error_string(ftdi));
    exit -1;
  }

  fprintf(stdout, "Setting RTS...\n");
  if(ftdi_setrts(ftdi, SIO_SET_RTS_HIGH) < 0) {
    fprintf(stderr, " ftdi_setrts failed\n");
    fprintf(stderr, "%s\n", ftdi_get_error_string(ftdi));
    exit -1;
  }

  ftdi_read_data_set_chunksize(ftdi, 513);
  ftdi_read_data_get_chunksize(ftdi, &tmp);
  fprintf(stdout, "Chunksize = %d\n", tmp);
  
  signal(SIGINT, sigintHandler);
  fprintf(stdout, "Starting Loop...\n");
  while(!exitRequested) {
    ftdi_poll_modem_status(ftdi, &status);

    if(status & 0x1000) { // BREAK_INTERRUPT

      f = ftdi_read_data(ftdi, buff, sizeof(buff));
      if(f < 0)
        sleep(1);
      else if(f > 0) {
        fprintf(stdout, "Read %d bytes\n", f);
        for(i = 0; i < sizeof(buff); i++) {
          fprintf(stdout, "%02X ", ((unsigned char *) buff)[i] );
        }
        
        fprintf(stdout, "\n");
        fflush(stdout);
      }
    } // BREAK INTERRUPT

  }

  signal(SIGINT, SIG_DFL);
  ftdi_usb_close(ftdi);
  ftdi_free(ftdi);

  return 0;
}


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic