• Home
  • Help
  • Search
  • Login
  • Register
Pages: [1]
Author Topic: RS485 to USB - How to?  (Read 7498 times)
SonicBoom
Newbie
*

Karma: 0
Posts: 11



View Profile
« on: March 04, 2010, 06:47:00 AM »

Hallo!

I'm a quasi-newbie in Linux technical questions, so sorry for any inaccuracy! Sorry for my english, too!

I bought a Sheevaplug and I need to use it to acquire data from some sensors connected to the Sheeva's USB plug via a RS485_to_USB converter. The chip installed on the converter is a ftdi ft232bl, and the converter itself is this one: http://www.dghcorp.com/usb/ .

When I connect the converter to the USB using dmesg I got this

usb 1-1: new full speed USB device using ehci_marvell and address 2
usb 1-1: configuration #1 chosen from 1 choice

Actually I don't know:
1. if the 2.6.22.18 kernel has some drivers for this kind of converter (I found some drivers for the 2.4.x kernels, but none for the 2.6.x ones);
2. which is the correct device name in /dev/ to use: I thought I was able to find a /dev/usb/ttyUSBx device, but I'm just finding some ttys and usbdev1.x [tty, tty0, tty1, ...., tty63, ttyS0, ..., ttyS3, ttyp0, ..., ttypf, usbdev1.1, usbdev1.1_ep00, usbdev1.1_ep81, usbdev1.2, usbdev1.2_ep00, usbdev1.2_ep02, usbdev1.2_ep81].

I really need some help, so I hope someone out there can give me some advices (some GOOD advices!  Smiley )!!!

Thank you in advance!
Logged

MarkF
Full Member
***

Karma: 7
Posts: 144


View Profile
« Reply #1 on: March 04, 2010, 06:51:30 AM »

Until someone else chimes in with a real answer, I'll offer this:

Unplug the device and list the /dev directory.
Plug the device in and list the /dev directory.

If there is a difference, this is the name of the device you just plugged in. Smiley
Logged

Mark

SonicBoom
Newbie
*

Karma: 0
Posts: 11



View Profile
« Reply #2 on: March 04, 2010, 07:03:25 AM »

Very good!

usbdev1.2etc. has suddenly disappeared and, after reconnecting the converter, dmesg says

usb 1-1: USB disconnect, address 2
usb 1-1: new full speed USB device using ehci_marvell and address 3
usb 1-1: configuration #1 chosen from 1 choice

and in /dev/ I was able to find

usbdev1.3  usbdev1.3_ep00  usbdev1.3_ep02  usbdev1.3_ep81

Does this mean that I can communicate with the converter?
Logged

SonicBoom
Newbie
*

Karma: 0
Posts: 11



View Profile
« Reply #3 on: March 05, 2010, 02:57:49 AM »

Nobody can help me?

Anyway, I tried a different approach: i connected the converter http://www.dghcorp.com/usb/ on a laptop PC provided with Ubuntu 9.10

Code:
uname -srvmo
Linux 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686 GNU/Linux

dmesg gave

Quote
[ 1509.896135] usb 2-2: new full speed USB device using uhci_hcd and address 3
[ 1510.092662] usb 2-2: configuration #1 chosen from 1 choice
[ 1510.099264] ftdi_sio 2-2:1.0: FTDI USB Serial Device converter detected
[ 1510.099325] usb 2-2: Detected FT232BM
[ 1510.099329] usb 2-2: Number of endpoints 2
[ 1510.099334] usb 2-2: Endpoint 1 MaxPacketSize 64
[ 1510.099339] usb 2-2: Endpoint 2 MaxPacketSize 64
[ 1510.099343] usb 2-2: Setting MaxPacketSize 64
[ 1510.101350] usb 2-2: FTDI USB Serial Device converter now attached to ttyUSB0

lsusb

Quote
Bus 002 Device 003: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC

while in /dev/ I found three devices:
  • /dev/serial/by-id/usb-FTDI_USB_TO_RS-422_485_ADAPTER_FTDYJ9MY-if00-port0
  • /dev/serial/by-path/pci-0000:00:1d.0-usb-0:2:1.0-port0
  • /dev/ttyUSB0

Finally, lsmod has detected a new module

Quote
usbserial              36264  1 ftdi_sio

I still didn't wrote any line of java code to communicate with the sensors, but I'm very confident that on the laptop I will be able to use them.
So my question is: is there a way to load the correct ftdi_sio driver in the Sheevaplug? How? Can someone out there help me providing a step-by-step (very, very verbose!) guide? I would really appreciate your efforts!
Logged

MarkF
Full Member
***

Karma: 7
Posts: 144


View Profile
« Reply #4 on: March 05, 2010, 12:50:07 PM »

My "best" advice would be to upgrade the SW on your plug to something more recent which would make "finding" drivers easier.  Having written that, I realize not everyone wants to do this so your only other choices (that I see right now) are to either, as you said, find the ftdi_sio driver for the kernel you are using OR build the driver from source yourself.

RS-485 has an address associated with each attached device, right?  I have not looked at the IO control interfaces in the Linux serial driver.  Do you know if the standard drivers support the 485 protocol?  Sorry for the questions; but, I'd hate for you to get all of the drivers you need and still not be able to talk to the hardware in a manner you want.
Logged

Mark

dlh
Newbie
*

Karma: 0
Posts: 3


View Profile
« Reply #5 on: March 06, 2010, 09:17:23 PM »

I would hope  that a 422/485 interface would very much look like a standard serial port - it is basically a standard UART with a different  hardware interface (to the world). IE a 232 device has ground, transmit and receive pins (plus a bunch of optional signal control pins) The 485 interface has a differential (+/- pair) for receive and transmit. This allows one to send high speed signals over long distances. Long ago I set up a snow making system at Whistler with more than a mile of wire between the control computer and the hydrants & sensors we were controlling.

So assuming you get a driver loaded, it will probably look like a serial port. The big issue with 485 software, is that you may be operating in a mode where the transmit and receive pairs are tied together onto a single pair so that you see whatever you transmit. In this case, you need to turn off the transmitter after sending a message out to your instrumentation in order to get a reply back. In transmit mode the controlling transmitter is driving the line levels and only one transmitter can do that at a time. The trick here is determining when you've finished sending. Even with a 1 character hardware send buffer you may get a buffer free interrupt before the last bit is out. If you turn the line around too soon, you'll clobber the last few bits (or bytes) of the message you just sent. Two solutions without an interrupt that tells you that the transmit buffer is truly empty)
    1 - wait a bit until the buffer is surly free
    2 - look at the data you've just sent until you see it all (since the receive pair is tied to transmit)
    This latter method will work with a standard 232 serial port driver.

I typically used an external 232 - 485 converter which has hardware built in to turn the line around as needed.
Logged

SonicBoom
Newbie
*

Karma: 0
Posts: 11



View Profile
« Reply #6 on: March 08, 2010, 04:13:57 AM »

Thank you for your replies!

I'm not sure I understand what dlh was telling, anyway it seems that the very first step is to have a properly working driver.
I tried to install the 1.0 Installer, but I was not lucky. Maybe I'm missing some points, but when I start the runme.exe with "nand" option, I get some informations and then everything seems to suddenly stop. I'm going to try it again, probably I will be able to have a ftdi_sio driver correctly loaded. By the way: why /proc/modules/ is empty?

Stay tuned, I'll let you know how things are going on!
Logged

CarlMLE
Newbie
*

Karma: 0
Posts: 10


View Profile WWW
« Reply #7 on: March 11, 2010, 04:55:59 AM »

Hi SonicBoom,

I am actually doing exactly the same thing as you, I am using a sheeva with a USB to 485 FTDI232BL

Bus 001 Device 005: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC

I used the installer to set up ubuntu on an SD card, did an apt-get update/upgrade and then installed an up to date kernel ( Currently using Linux ubuntu 2.6.33)

When you attach the USB 485 it will appear in /dev as ttyUSBx (Where x is the next availible number).
I use a udev rule filter to symlink the /dev/ttyUSBx to /dev/usbserial so my C application can check and use /dev/usbserial instead of trying to find out which ttyUSBx is the 485 device

You can then treat it like a normal serial port, my USB to 485 automatically does the direction line switching not sure that's a function of the chip or my board so be aware of that.

Installing a new kernal was easy, I just did :
wget http://sheeva.with-linux.com/sheeva/README-2.6.33
chmod 777 README-2.6.33
. README-2.6.33 --nandkernel

That downloads a script, makes it executable then runs it tell the script we want to flash the kernal in Nand.
« Last Edit: March 11, 2010, 04:58:41 AM by CarlMLE » Logged

ML Electronics - www.ml-electronics.co.uk

CarlMLE
Newbie
*

Karma: 0
Posts: 10


View Profile WWW
« Reply #8 on: March 11, 2010, 05:04:48 AM »

Just noticed you have said in one of your posts you can see a "/dev/ttyUSB0" that means linux has done its stuff and you have a serial port to write to.
Logged

ML Electronics - www.ml-electronics.co.uk

SonicBoom
Newbie
*

Karma: 0
Posts: 11



View Profile
« Reply #9 on: March 11, 2010, 12:22:35 PM »

Thank you again for all of your efforts in helping me! I appreciate very much!

I was figuring that the problem was related with the old kernel shipped with the Sheevaplug - and I was right!
I just installed the Installer-1.0 and (after some pain!) I was able to correctly see a /dev/ttyUSB0 device.

I have installed openjdk to install my Java programs and everything is going on fine.
I'm going to use an USB HDSPA modem to upload data to a remote server, and it works very well.

Next step will be to control the RS485 port via a Java program. I'm currently using librxtx-java that seems to work fine.

@CarlMLE: I'm not able to write a complex C program, so I really need Java code (that is much simpler for me). Anyway probably you could help me again: my old acquisition software is written in basic and it mainly just open the com port, write a string on it to ask data, listen to get data (written in a string with a command like this

Quote
str$ = input$(#comport, lof(#comport))

and closes the com port. I'm trying to replicate the same behavior in my new Java program, but on the read the program hangs waiting for data. Do you have some advices, since it's my very first attempt to control a com port through a Java program? Thank you in advance!
Logged

CarlMLE
Newbie
*

Karma: 0
Posts: 10


View Profile WWW
« Reply #10 on: March 12, 2010, 02:12:34 AM »

Our projects are very similar, mine also uses a HDSPA usb modem to upload data, another tip, I had to update the kernel to add in PPP support so I could dial the modem, with out PPP support in the kernel I could not get a connection.

Im not sure I can help you with Java, it's not a language I have used with Linux. I quick google suggests you should be using the javax.comm api : http://java.sun.com/products/javacomm/

This page seems to have an example : http://www.captain.at/howto-java-serial-port-javax-comm-rxtx.php



Logged

ML Electronics - www.ml-electronics.co.uk

SonicBoom
Newbie
*

Karma: 0
Posts: 11



View Profile
« Reply #11 on: March 15, 2010, 06:40:39 AM »

So ok.
I managed the RS485 converter to work.
I'm actually writing a Java program to acquire data from my serial-connected sensors using rxtx lib.

The good news is that I can send coomands on the rs485 converter (a green blinking LED tells that the port is receiving something).
The bad news is that no data come back from the converter!

I had a basic program (written to work under a Windows system) that was working. The important part is:

Quote
open "COM9:9600,n,8,1,cs,ds,rs" for random as #dgh

for tryW = 1 to 10
        print #dgh, "$aRB"
        GOSUB  [delay]
        H$ = input$(#dgh,lof(#dgh))
        if left$(H$,1)="*" then exit for
next tryW
aa = val(mid$(H$,2,9))/100
bb = val(mid$(H$,13,9))/100
cc = val(mid$(H$,24,9))/100
dd = val(mid$(H$,35,9))/100

and similar for...next loops for the other sensors.

I'm trying to replicate this behaviour through my Java code:

Quote
String  defaultPort = "COM10";
portList = CommPortIdentifier.getPortIdentifiers();
serialPort = (SerialPort)portId.open("DataReading", 2000);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
serialPort.notifyOnOutputEmpty(true);

int available = 0, k = 0;
boolean check = true;      
   
for (int i=0; i<10; i++) {
   while (check) {
      outputStream.write("$aRB".getBytes());
      Thread.sleep(k++);
      available = inputStream.available();
      if (available > 0) {
         System.out.println(k);
         check = false;
      }
      if (k == 100)
         check = false;
      System.out.print(".");
   }
   check = true;
   k = 0;
   System.out.println("");
   System.out.println("Available data: " + available);
   if (available > 0) {
      int prova = inputStream.read(byteArray, 0, available);
      System.out.println("Read: " + prova);
   } else {
      System.out.println("No data found!");
   }
}
serialPort.close();
System.exit(1);

A lot of things are messing in this pseudo-code I wrote, but the point is that the ouputStream.write seems to do correctly his dirty jpb, while the instruction "inputStream.available()" constantly returns 0, even when it should find some data.

My only thought is related with the "open" command in basic:

Quote
open "COM9:9600,n,8,1,cs,ds,rs" for random as #dgh

Should I use the same parameters (cs, ds, rs) in my Java program too? How?
Or the problem is buried much deeper, probably in the way RS485 protocol should be used? Maybe I'm wrong, but in the basic code I was using nothing seems to be too hard to understand and implement in a similar Java program. The result, however, is that the basic code is working, while my Java code not!

Please, help me!
Logged

SonicBoom
Newbie
*

Karma: 0
Posts: 11



View Profile
« Reply #12 on: April 13, 2010, 03:52:29 AM »

@Everybody

Found and solved the problem! Maybe it will be useful to someone else.
It was very simple!

While in basic the "print #" statement prints a string ending with a newline character, the Java .write statement doesn't! It was a simple matter to add a "\r" at the end of the string to send on the rs485 converter the correct command.

Now the system is working properly.
Contact me if you need some help!
Logged

Pages: [1]
Print
Jump to: