I would say friends, but this is the first post so there is most likely no one reading at this point, oh well.
Today I want to teach a little about USB to Serial Cords, specifically a USB to DB9 though I think it would apply to any type of serial cables.
Why is it important?
This is a fair question, but it is pretty important as we will see in future robotics projects. Serial communications between a computer and a device is pretty useful and fast. In my future projects I intend to control the robot arm I a building with serial communications. Also, almost every device you use has a serial component to it. Do you know what USB stands for? Universal SERIAL Bus, which means that serial communication is pretty much everywhere. I won’t be doing communications directly with the USB but we will use UDART communication methods.
Serial communications can be broken down pretty simply. First we need to know what a bit is. A bit is either a zero or a one. This is the basis of modern computing. Eight bits in a row is called a byte. This creates a binary number, between 00000000 and 11111111. In decimal this becomes 0-255. You can calculate this range from 2^8= 256. Keep in mind one of those numbers must be zero and you get the range 0-255. The byte is the base unit we will be using to send messages back and forth over the serial port.
In this lecture I will take you through some of the problems with USB to serial communications and then I will show communications using both USB to TTL and USB to Serial cords between a TReX robot controller. The TReX requires a Rx and Tx connection to the computer, and it important to understand that USB does NOT directly map to this as show in figure 1:
Figure1: USB pin-out diagram
See the DATA- (D-) and DATA+ (D+) channels are actually coupled. Why is this important? It is super important because of what Tx and Rx is. Tx is the transmission line and Rx is the receiving line, and these are not coupled and completely separate. This means we will need some kind of circuit in between USB and the TReX controller in order to get communications. Have no fear, that is what the USB to Serial cord is for.
Pros and Cons of a USB to Serial RS232 Cable
So it is not just that simple. In order to convert the USB signal into the serial signal you needs specific chips. A while ago most computers had a DB9 port on them and lots of older joysticks and other things used DB9 ports as their main way of connecting. This has fallen out of fashion with USB ports. This means many of the chip sets that do this conversion are really old, are often not compatible with newer operating systems, and sometimes just suck. Professionals that use these cords to fix customers computers or other electronics often keep three or four on them in case one of them quits working.
Before I learned all this I went to best buy and bought one. I spent an entire Saturday struggling to figure out why it would not work. In the end I learned the chipset on that type of cable was one of the lowest quality chip sets, so I would not suggest anyone buy them. The best guide to selecting a USB to Serial RS232 cable can be found here. Keep in mind that article is trying to sell you something so I will give you the quick and dirty of trying to find one, and a link to the one I am using on amazon. 1) Make sure the chip set is either FTDI or Silabs, I went with FTDI, 2) Check that it is compatible with any operating system you are going to use, for me that was windows 10 and Linux. The one I have is here. Keep in mind that you also have to make connections from the end of the DB9 to the TReX or whatever you are using, I found the soldering adapter at my local Radio Shack for just a few bucks.
Figure 2: Soldering adapter for DB9 cable
Is there another way that doesn’t involve a DB9?
Why yes there is and this is maybe the better way of connecting to the TReX using serial. Those crazy engineers have designed a USB to TTL connector. Why is this good? Because it has the same pins as the USB to serial but you can plug into the way you would plug into a higher quality motor with encoder. You need to look at the chip type and stuff same as before you just search for a different product. The one I got is here.
Software to Send Data
Now many of you might be familiar with using pySerial to send data over serial ports and this is great, I would totally suggest doing that if you are familiar. If you are not, don’t worry I plan on doing a post on pySerial soon. For this post I want to just use some easy to download software that we can use to send bytes back and forth from the TReX. I found and have liked HTerm, but any program would do they are basically the same. HTerm has both a linux version and a Windows 10 version you can download here. It is not complicated to use and everything works as you would expect.
Testing Your Cable with a Loop-Back Test
Due to how troublesome USB to Serial cables are a loop-back test is a good way to make sure they work. As I mentioned above the Tx is the transmitting line and Rx is the receiving line. They are independent normally, but in a loop back test you connect them together. The general idea is that anything you send you should immanently receive back. This is good because it can test that the drivers on your computer and the cable work, and can let you test your send and receive methods. For this demo I am using the USB to TTL cable mentioned above. The pin out is as follows:
Pin 1 GND
Pin 2 CTS
Pin 3 VCC
Pin 4 TXD
Pin 5 RXD
Pin 6 RTS
So I am going to connect pin 4 to pin 5 (note pin 1 is black).
Now I open the device manager on Windows to see which com port this is connected to.
Note that if the drivers are not supported the cable can still show up here. Also keep in mind that if you need to install driver software because it is not available with windows you will have to do that before this step. Furthermore keep in mind that I don’t use mac so I have no idea how this would be different on mac.
Note that for me the cable is attached to COM7. Now in HTerm you are going to want to connect to this com port and to save time later set the baud rate BEFORE connecting to 19200, as this is what the TReX motor controller defaults to out of package. Note I have the received data set to show Ascii characters, decimal (Dec) and binary (Bin). I do that because later it is useful for now you just need Ascii characters ticked. I am transmitting ascii characters now as well. Don’t worry too much about this as you can change those on the fly. Next I also set the newline after … ms receive paus to 500. This means it will make a newline every half second that I don’t receive anything over that port.
Now you can type anything into the the Input control box and if it works you should see it in the received data box. Like so:
If it didn’t work, check your drivers, check that the correct pins are connected and check you have the correct com port. If all of those are fine then in all likelihood you have a cable that doesn’t work with this program/OS. You can try other serial communication programs to be sure but if that doesn’t work you will have to buy a different cable. I’m sorry that totally sucks.
Loop-Back test with DB9
Sometimes you don’t have a cool USB to TTL cable and instead want to use the more classic DB9 cable. This is fine and can be done in much the same way. The only step that differs is how you connect the Tx and Rx pins. Below is a diagram of a standard DB9 set up, but keep in mind that this can be different so be sure you manufacturer hasn’t done something different. If they have and you connect the wrong two pins you can break the chip set.
In this setup you are going to connect pin 2 to pin 3
And following the rest of the above steps I get a HTerm output of:
Communicating with a TReX controller using HTerm
This is the last part of this post, I won’t be controlling any kind of motor I will just show simple communications with the TReX. The TReX has different control modes, analog, RC and serial. At any time the first two can be overwritten by the serial communications, this allows for a computer to control your motors autonomously or for a human to teleoperate it. The TReX also features cool stuff like current feedback, which is the main reason I selected it. With the model I own you can control two bidirectional motors and one unidirectional motor. For the rest of the post I will work with the USB to TTL cable as I think it is a little easier to work with, but I will show any diviation that happen when you use a DB9 cable.
For this you will need the TReX controller, a power supply (anything with a 5V supply you can plug into the TReX) and some jumpers to run from you serial output to the TReX. You should read over the TReX documentation which is here. It is fully comprehensive and really useful. A picture showing how to connect the serial cable into the TReX is show below is straight from that documentation.
Notice that the DB9 and the TTL are wired different. The TTL is wired like the second picture in the diagram, while the DB9 is wired like the first picture (hence the DB9 written above it). DO NOT MIX THIS UP. If you wire the DB9 like it is the TTL you could destroy your TReX. If in doubt wire it like it is the DB9. If you don’t get communications it could be because the voltage is too low and that tells you to wire it like it is the TTL. Below is a picture of my wired setup.
I am using some RC battery I had laying around, it is not even fully charged, so don’t worry too much about the powering. When you are actually controlling motors that is when you need to worry about it a lot more.
Now in HTerm make sure you are connected to the correct com port, and your baud rate is set to 19200. For me that was port COM7. You want to transmit in hexadecimal (HEX) because that is how the commands for TReX are documented, so it makes life a little easier. The command list is documented here. I am going to send the command get signature. This is a good first one to send because the TReX will respond with TReX and then the firmware version number. This will tell you if you need to update the firmware. To send it simply type 81 into the input control box in HTerm when you are in HEX mode and you should get a similar response as I did below:
This tells me that my communications are great and that my firmware version is 1.3. Congratulations you have selected the correct cable, ensured it communicated and used it to send a command to your TReX motor controller.
I hope you guys enjoyed this and please like it, share it and comment below.
Happy Roboting 🙂