FullDuplexSerial encoding
nisbus
Posts: 46
Hi,
I've written a small program in C# that reads from the USB port of the Propeller protoboard (FTDI).
I'm running the HelloFullDuplexSerial demo from the Propeller tool and I can see that it's sending out "This is a test message" at regular intervals.
My C# application is picking up on the bytes received but I'm having troubles decoding the bytes into string on the PC side.
I always get a list of ? when I use ASCIIencoding do decode the message and similar results with unicode encoding.
Does anyone know what kind of decoding can be done on the PC side to translate the bytes coming from the propeller to a string??
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks,
nisbus
I've written a small program in C# that reads from the USB port of the Propeller protoboard (FTDI).
I'm running the HelloFullDuplexSerial demo from the Propeller tool and I can see that it's sending out "This is a test message" at regular intervals.
My C# application is picking up on the bytes received but I'm having troubles decoding the bytes into string on the PC side.
I always get a list of ? when I use ASCIIencoding do decode the message and similar results with unicode encoding.
Does anyone know what kind of decoding can be done on the PC side to translate the bytes coming from the propeller to a string??
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks,
nisbus
Comments
byte[noparse]/noparse buf = new byte[noparse][[/noparse]serialPort1.ReadBufferSize];
serialPort1.Read(buf, 0, serialPort1.ReadBufferSize);
you should look at the buf array and see what the actual data is. The first character in the array should be the decimal # 85 or 01010101 in binary which would equal the letter 'T'.
Jesse
188 180 156 25
I'm using this FTDI dll to read from the propeller.
What am I doing wrong?
Is there something else I should be using to read from the port?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks,
nisbus
A serial port sniffer program is extremely useful - you load it first then load the program that will use the port, eg the terminal program. See my link below - if you go to the propeller page near the bottom just above the last picture is the link.
Also near that is a link to a huge vb.net program with examples of serial port code (xmodem file transfers etc) using vb.net which is similar to C. There are unicode issues there - eg outputting strings in vb.net only sends 7 bits instead of 8 bits. Not simple to debug that one. I read it into byte arrays first. Then there is the issue of scanning the port continuously or writing code that runs an interrupt when a byte appears. I never got the latter working so I ended up just polling the port every 30 milliseconds or so.
Can you post your C code?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.smarthome.viviti.com/propeller
I'm getting the same resulting byte sequence as from the FTDI driver.
This is the code for the serial port reading:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks,
nisbus
I've had all sorts of problems with serial ports over the years. Do you have a second USB to serial adaptor? If so, can you fire up a terminal program, send data out one serial port and read it in another port with your program? Just swap pins 2 and 3 on the two male plugs with a female to female adaptor.
Having said that, there may be something simpler. Despite the propeller being able to run at 115k for downloads, I've never managed to get it to output a byte reliably at anything > then 38k. I don't understand why as the hardware is the same so maybe it is something in the software driver. So perhaps first start by dropping the baud rate back to something really safe and slow (1200) on the propeller side and change the C code accordingly.
Have you got that serial port sniffer program - that will help a lot too as it will tell you if the problem is the propeller not sending properly (subtle timing errors from the xtal?) or the PC ncode not receiving properly.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.smarthome.viviti.com/propeller
I don't have a second serial port adapter so I can't test that.
I'm downloading a serial port sniffer now.
Thanks for your help.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks,
nisbus
If PST works as expected check:
The data from the prop is the correct polarity - reverse polarity on prop using invert tx (mode bit 1)
Data from FDS is always 8 bits, no parity so make sure the port on the PC is set for 8 bits, no parity, one stop bit, and matching baud rate.
Try setting the prop baud rate to 9600.
Post Edited (Dave Hein) : 3/28/2010 1:33:47 PM GMT
Jesse
Suggestions:
I use BytesToRead because rarely do I exceed the serial port buffer size.
SerialPort.Read(buffer, offset, count) always returns the number of bytes read. Use the number of bytes to enumerate the buffer argument.
Remove the encoding, at least for now. The serial port default should be fine. Take a look at the serial port help file for more information.
Create another handler or method in the port_DataReceived() handler. This handler or method should frame up the received data. You cannot be sure that the data received event will contain a single "This is a test message<cr>". It could be 1 byte or 100 bytes. This handler or method will look for each <cr> (0x13) in a public buffer ("This is a test message<cr>This is a test message<cr>This is a test") then fire off an event or invoke a delegate. Pass a single "This is a test message<cr>" argument each time you fine a match in the buffer. Don't forget to remove the "This is a test message<cr>" from your buffer once you find the match and invoke the delegate. Always append to the end of the buffer on each SerialPort.Read(buffer, offset, count). That way you don't miss anything.
Hope this helps. Let me know if you need anything.
Post Edited (Mike G) : 3/28/2010 3:27:41 PM GMT
I set the BaudRate to 1200 on the PC side and setting it to 9600 on the Prop side gives me the correct data [noparse]:)[/noparse]
It doesn't seem to matter what I set as BaudRate on the PC side.
I now get "This is a test message!" on my PC !!
All I need to do now is implement some framing on the PC side to catch only whole messages.
Thanks so much for all your time.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks,
nisbus
That does not make sense at all. In the demo I provided, the PC and Prop were both set to 57600. I loaded HelloFullDuplexSerial demo and ran the C# code as is.
I had two constructors on the PC side and one was setting the BaudRate while the other wasn't. I was using the one that wasn't [noparse]:)[/noparse]
If I set both to 57600 I get correct data as well.
So after all this was about setting the matching baud rate.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks,
nisbus
This was about how to use the SerialPort.Read() method.
After you frame up the messages, the next thing you'll need to do is figure out how to update the UI on the main thread
Post Edited (Mike G) : 3/28/2010 5:34:23 PM GMT
I have an ObservableCollection<string> to hold the data that is bound to a listbox:
In the constructor I catch the CurrentDispatcher
and on the event that gets messages I do this:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks,
nisbus