LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2011, 07:58 PM   #1
fenomax
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
Serial communication problem RS-232 - USB(FTDI-chip)


Some definitions!

Sensor frequency 100Hz, each sample it sends 36bytes, so it sends 3600 bytes per second!

I'm trying to build a piece of software to read the data send by sensor at Linux(UBUNTU). Between the PC and Sensor has the RS-232 - USB converter FTDI.

Sometimes I get the correct mesuarement, other times seems I'm get junkdata at the buffer of FTDI!

The idea of function to read is below:

while(count<=5000){

FT_read_data();//I read 36 bytes per time
wait(the time to complete 10ms, frequency of sensor);
count++;
}

I'm wondering what happens if not arrive 36 bytes in the buffer and the comand FT_read_data execute, it could be the cause of the junkdata.

*FT_read_data() is a command of FTDI's library!

If anyone has a code to read a data from a sensor using FTDI! Please post to see how it works!
 
Old 11-25-2011, 05:55 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Here is a good place to read about serial programming in linux using C language.

I personally use another rs232-usb converter -- PL2303 (ordinary old Nokia data cable actually ) to control my 3D printer. But many people use FTDI's absolutely the same way as I am, so there are probably no big differences.

It can be a pain to implement and debug serial communication in C so I suggest you to debug your device using higher-level language, like python, first.

For example:
1) Install `python-serial' package
Code:
sudo apt-get install python-serial
2) To check my device's current position I would type (to file test.py)
Code:
#test.py
import serial
s = serial.Serial('/dev/ttyUSB0', baudrate=19200)
s.write('M114\n') # M114 -- get current position
print s.readline()
Code:
$ python test.py 
ok X:0.000,Y:0.000,Z:0.000,E:0.000,F:50
$
'/dev/ttyUSB0' is a device file associated with my usb-serial converter.

read/write operations are buffered so you do not need any delay:
Code:
 
while 1:
    print s.readline()
should work (if your packets end with '\n').

This way you can assure yourself that sensor works properly.

Hope this helps.

Last edited by firstfire; 11-25-2011 at 07:35 AM.
 
Old 11-25-2011, 02:50 PM   #3
fenomax
LQ Newbie
 
Registered: Nov 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Instead of use the FTDI hardware interface, I connected my sensor directly at serial port!

I'm starting to have a good results, I meaning i'm reading the right data!

The new problem is that!

How can I know if has arrived new data at serial port? Because my sensor sends at 100hz(10ms), but if i wait(10ms) may the data not arrived yet, so how can i know if the amount of data has already arrived?


I've created that function

While(){

read()
wait(10ms)

}
 
Old 11-26-2011, 06:57 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Firstly, your Serial<-->USB dongle probably can/should get installed with drivers to allow you access the serial data using conventional serial port semantics. It should provide you with a device like /dev/ttyUSB0. Once you get that set up, the following links should get you most of the way to your goal.

Serial-Howto
Serial Programming Howto
Serial Programming Guide for POSIX OS's

For matters of timing, look closely at the settings of VMIN and VTIME. Used in combination, they can be used to create timeout-based delimiting of input data. More conventionally, specific characters can be used to delimit input records. Is your serial data coming in as human-readable text, or as binary data? If text, most instruments do provide some sort of record delimiter, such as carriage returns and/or linefeeds. Canonical mode in the serial driver can exploit this to delimit records.

Note that if you are really using wait() to delay between readings, you are using it for entirely the wrong purpose. Perhaps you really mean to sleep() or usleep().

--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FTDI USB/TTL serial adapter. TIOCM_DTR bit rtspitz Linux - Hardware 1 07-19-2011 09:36 PM
FTDI USB-to-Serial /dev naming RogueWarrior65 Linux - Hardware 5 09-23-2010 05:05 PM
Experience with 8 bit serial AND parity with HL-420 USB-RS-232 converter? cordata Linux - Hardware 4 06-22-2009 09:47 AM
USB serial driver(ftdi): there is error in receive data plumnut Linux - Newbie 0 03-23-2009 07:30 AM
ftdi serial to usb converter rightsidedown Linux - Hardware 1 04-29-2008 05:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration