UART 9 bit asynchronous serial communication

I will be working on a project that will be interfacing a Netduino 3 WIFI with an 8051F340, a TTL camera, and a PC via USB to serial adapter. I’ve worked on a few small projects in C# but this will be a much larger undertaking as I will exploring new concepts that I have only novice experience with. I’ve been studying the code given on the Netduino page for UART and will be testing the code. I’d like to get a better understanding of how I can change the code for my purposes. The gist of the projects is that the Netduino I will receive and transfer data bytes via 3 of the com ports. The information will arrive asynchronously and will send in the same manner. Most data will be no more than 5bytes, but the JPG coming back from the camera may be bigger. I understand that the buffer needs to be large enough to handle whatever data is received. The data to and from the 8051 will contain 9 data bits a start and stop bit. I want the event handler to initiate every time data is received, move the data a container object, compare that first byte to a table of if-then statements, then call and method or procedure that will initiate a transmission of data. My first questions are…

I see that a byte array is being use to catch the received data. What container might I use to catch the 9 data bits from the 8051 that would still allow for an array of three or four 9 bit packages? I’m hoping I can use 9 bit UART with the 8051 because less coding on that end.

transmitter.Write(Encoding.UTF8.GetBytes(messageToSend), 0, messageToSend.Length);

Similarly, I see this line seems to initial the transmission. It also seems to encapsulate only 8 bits. How might I initiate transmission with 9 bits?

Also I figure it would be easier to compare the first byte using if-then statements to choose how it want to respond. It there a better way to do this, and are methods the best way to initiate a transmission reply?

Lastly I’m concerned about data collision or loss in the buffer when multiple ports write to the Netduino at once. Would multi-threading help to avoid this?

Sounds like an interesting project.

For the container I would consider converting the array of bytes into an array of shorts / ushorts as this will allow you to capture the 9 bits being transmitted.

For 9-bit transmission, I’ve never attempted that before so not sure of the answer.

For the if statement I’d consider two options, switch statements or a function table. With the function table I’d put the most commonly occurring bytes in the data stream at the top of the table. Mind you, you could use a dictionary in place of a table.

Multithreading on the Netduino currently offers fixed time slices of 20ms. There’s a rather good article on NETMF threading here.

Let us know how you get on as it sounds really interesting.

Hope this helps,

1 Like


I found these two posts enlightening

hopefully this is along the lines of what you are trying to accomplish, specifically the second one, which goes into more detail.

I was able to complete the project and get it working. The experience was very enlightening. I’m considering posting a formal review of the project and sharing the code which is pretty Large.

1 Like

Would be nice to see how you solved the problem.


Sounds like an awesome idea!! Maybe you can post it on Check out