As part of the project we need to have a visual c# program read a sensor connected to the MBed. The sensor we decided to use was the Adafruit TCS34725 Colour Sensor, as we already have the colour sensor working on the mbed all we needed to do was to set up the program to read the serial in line and respond to the command sent from the c# application when it wants to receive the data.
The basic c++ source for the MBed looks like this;
#include "mbed.h" Serial pc(USBTX, USBRX); int main() { while(1) { if (pc.readable()){ char Data_in = pc.getc(); switch(Data_in){ case 'R' : /*Do Something*/ break; } } } }
using the case statement to decode the input allows us to add more functionality to the source if we want to read other sensors, we will just create more functions to read the sensor and return the values in a way for us to decode them in the c# application. The Data returned from the MBed is in the form [Red<space>Green<space>Blue<space>Clear<newline>].
The application is simple;
and the source for reading the sensor, when "Read Senor" button is clicked;
private void BTN_Read_Click(object sender, EventArgs e) { string Data_In = null; try { serial.WriteLine("R"); Data_In = serial.ReadLine(); } catch (TimeoutException) { MessageBox.Show("ERROR: Timeout"); } catch (InvalidOperationException) { MessageBox.Show("ERROR: Open Serial Port"); } string[] in_Data = Data_In.Split(' '); int W_col = int.Parse(in_Data[3]); float R_col = (float.Parse(in_Data[0]) * 255) / W_col; float G_col = (float.Parse(in_Data[1]) * 255) / W_col; float B_col = (float.Parse(in_Data[2]) * 255) / W_col; LBL_Red.Text = in_Data[0]; LBL_Green.Text = in_Data[1]; LBL_Blue.Text = in_Data[2]; LBL_Clear.Text = in_Data[3]; int R = (int)R_col; int G = (int)G_col; int B = (int)B_col; LBL_Col_red.Text = R.ToString(); LBL_Col_Green.Text = G.ToString(); LBL_Col_Blue.Text = B.ToString(); Color sample_Colour = new Color(); sample_Colour = Color.FromArgb(R, G, B); TXT_Colour.BackColor = sample_Colour; }
in the try catch block we attempt to send the command to the MBed, two catch exceptions were added; the first is for the COM port read timeout, the second for if the COM Port is closed.
The line
string[] in_Data = Data_In.Split(" ");
is used to take the input and split it into an array, using the <space> in between the values. The data in the array can then be manipulated.
The labels in the Returned group box will be updated with the raw value from the serial data.
To make the application a little more interesting a textbox is updated with the colour from the sensor. As the max value of the sensor is 2^16 (65,536) and the FromARGB method only takes a byte (255) as an input we need to multiply the data by 255 and divide by the clear value.
FromARGB then produces a new colour object which we set to the backcolor property of the text box.
and the result is
The colours seam to be a tad off, think it will probably be due to fluctuations in the sensor. Might be worth while averaging the sensor reading and returning the average, but for now i think it's close enough to complete this week's deliverable.
Top Comments