Issue with reading version off of PSC
theMoshi
Posts: 9
Hey everyone. I'm very new to the propeller servo controller (28830) and am having an issue reading data from the device.
Basically, I'm able to send data to the device (e.g. move servo x to position y, etc.) just fine. However, when I try to read data back, I'm getting random rubbish / nonsensical bytes.
I sure that my setup is correct because I can control all servos. Furthermore, the PSCI software works just as well. It reports the version being "1.0"
The java code I'm using attempts to read the version off of the device (I'm using Libusb and a java wrapper).
The code above outputs:
I've observed that byte 2 and byte 6 are more than often the same.
After the bulk read, is there anything that needs to be done to the data to make it human readable?
Any suggestions will be greatly appreciated.
Basically, I'm able to send data to the device (e.g. move servo x to position y, etc.) just fine. However, when I try to read data back, I'm getting random rubbish / nonsensical bytes.
I sure that my setup is correct because I can control all servos. Furthermore, the PSCI software works just as well. It reports the version being "1.0"
The java code I'm using attempts to read the version off of the device (I'm using Libusb and a java wrapper).
// import ch.ntb.usb.*; Device device = USB.getDevice((short) 0x0403, (short) 0x6001); //public void open(int configuration, int interface_, int altinterface) device.open(1, 0, -1); final int endPointAddress1 = 0x81; final int endPointAddress2 = 0x02; final byte RETURN_CARRIAGE = 0x0D; String commandStr = "!SCVER?"; byte[] command = commandStr.getBytes(); byte[] dataOut = { command[0], command[1], command[2], command[3], command[4], command[5], command[6], RETURN_CARRIAGE }; int bytesWritten = device.writeBulk(endPointAddress2, dataOut, dataOut.length, 5000, false); System.out.println("Bytes written: " + bytesWritten); Thread.sleep(1000); byte[] dataIn = new byte[11]; int bytesRead = device.readBulk(endPointAddress1, dataIn, dataIn.length, 5000, false); System.out.println("Bytes read: " + (bytesRead+1)); for(int i = 0; i < dataIn.length; i++){ System.out.println("Byte " + i + ": " + dataIn[i]); } System.out.println("Version: " + new String(dataIn, "UTF-8")); device.close();
The code above outputs:
Bytes written: 8 Bytes read: 3 Byte 0: 1 Byte 1: 96 Byte 2: -85 Byte 3: 0 Byte 4: -60 Byte 5: 0 Byte 6: -85 Byte 7: 0 Byte 8: -1 Byte 9: -1 Byte 10: -1 Version: `k ? k ???
I've observed that byte 2 and byte 6 are more than often the same.
After the bulk read, is there anything that needs to be done to the data to make it human readable?
Any suggestions will be greatly appreciated.
Comments
1. Inside your for loop you are printing out a byte type. You should cast it to char if you want it to be human readable.
2. Your loop upper bound should be bytesRead, not dataIn.length: the remaining bytes are junk anyway, and you don't care about them.
3. Likewise with your String constructor from the dataIn array.
4. For sending your data you should be able to get that all in one line: (new String"!SCVER?\n").getBytes()
5. You should clear out the receive buffer before you send the command, just to make sure that there isn't any junk hanging around.
I doubt these will fix your problem, but they will get you closer to the solution. Can you post your full code?
The code provided above is mostly it. Here is the entire class.
All usb/device API calls are from LibusbJava:
API: http://libusbjava.sourceforge.net/wp/res/doc/index.html
Ref: http://libusbjava.sourceforge.net/wp/
Edit: I'm not familiar with libusb. It looks like a library to read *any* device that is connected to USB, which is a bit excessive for a serial port. I'm sure it would work, but I'm not so sure that you can just send and receive bytes like you are. Do you have a link to an example of using libusb in this way?
You're correct, Libusb is a bit excessive since its a generic library for communicating with usb devices. Unfortunately, my knowledge of usb technology is immature and this seemed like the route I should take.
What would you suggest is the easiest way to go about working the the PSC? I don't have a basic stamp and therefore my only means of communicating with PSC is through is its usb port directly. Any particular language/tools?
I was able to dig out the following class posted on these forums, but am unable to find the exact thread they belong to. It appears that the author is performing some sort of bit wise operations on the version before returning.
Does this mean anything?
The entire code:
I think Python is the easiest to use to read a serial port. The pyserial library is simple and cross platform without qualifications. I've used it to make a GUI for a Propeller system (http://www.youtube.com/watch?v=viVVwRabck4). Java is supposed to be cross platform, but it has terrible serial port support. The rxtx library isn't very good either (it's not very active for open source, and it doesn't follow the Java IO stream convention).