C# and .NET programming – introduction 1 Introduction A course in C# and .NET programming Associated book: C# and .NET Programming for Engineers, John Allwork, Publisher: Elektor, 2009, ISBN: 978-0-905705-81-1 1 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 2 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 2 Introduction, Development environment User Interface, controls – Properties and events Dialogs and forms C# language – basics C# language – arrays and strings C# language -program flow Object Oriented programming – Methods Object Oriented programming – Classes File handling Multimedia & Graphs © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 3 Debugging Run-time placement Threads Internet communication Databases – Introduction and displaying Databases – Creating Databases – Accessing from code Plotting DLL and API Hardware interfacing - USB interface 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 3 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 4 Software – Visual C# 2008 Download from Microsoft http://www.microsoft.com/express/download/default.aspx Free but slightly limited version Also install SQL 2008 – used for databases Register the product 4 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 5 Development environment We will create Windows applications 5 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 6 Design environment: Solution explorer Menus Form Code editor Toolbar messages 6 Properties/events window © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 7 The Form – Most important - place controls – the UI. Display by clicking Form1.cs [Design] tab Form Textbox Button Label Listbox 7 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 8 The Toolbox – Grouped by task Contains controls Common controls are: Buttons, Textboxes, Labels, Radio buttons etc. 8 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 9 The Properties / Events window Properties Each control has properties – e.g. Name Position (top and left) Size (height and width) Text Description of property 9 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 10 The Properties / Events window Events Events – happen to controls e.g: Button click KeyPress MouseMove MouseDown Others – Form load 10 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 11 The Code Editor – where you enter your code Double-click object to enter code Some added for you – do not delete 11 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 12 Your First C# Program 12 Run C#, start a new Project, > Windows Application and call it ‘Hello world’ Save the project. Select File>Save All. Display the form (click form1.cs[Design] tab). Add button (drag and drop) from Toolbox to form © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 13 Change the button’s text display (a property). Display the properties window, Scroll to the Text property, type in ‘Hello world’ 13 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 14 Place TextBox and label to form Change label’s caption property to ‘My First C# Program’. Form looks like: 14 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 15 Run program – not much happens. Close it. Double-click button to add code for button click Add code: 15 textBox1.Text="Hello world"; © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 16 Run program, click button. ‘Hello World’ is displayed – Your first C# program ! Note use dot notation to access property C# is case sensitive 16 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – introduction 17 Summary Free software from Microsoft Development environment Form, Code editor, Toolbox, properties/event window Drag/drop controls (buttons) to form Double-click to add code First program 17 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 18 Topics • • • • • Adding methods to class Static classes – available to all objects Overriding default methods Inheritance Protected declaration 18 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 19 Methods Add method Move Move Point one place in X and Y direction Code: public void Move( ) // declare public { _x++; // move X by one _y++; // move Y by one } // end move Use: myPoint.Move( ); 19 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 20 Method overloading Add second Move method – pass distance to move public void Move(int Xdistance, int Ydistance) { _x = _x + Xdistance; _y = _y + Ydistance; } Use both: myPoint.Move(12,34); myPoint.Move( ); 20 // pass X and Y // one unit in X and Y © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 21 IntelliSense knows about both: 21 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 22 Static Classes - don’t have to be instantiated. ‘Distance from Origin’ example of this – available to all objects Code: class Calculate // pass x,y return distance { public static double DistanceToOrigin(int x, int y) { return Math.Sqrt(x * x + y * y); } } Use: distance = Calculate.DistanceToOrigin (myPoint.X, myPoint.Y); 22 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 23 More useful ToString method ? - Override default ToString method Add code: public override string ToString( ) { return "My Point Object is at : " + _x + “," + _y; } Use: MessageBox.Show(MyPoint.ToString( )); Displays: ‘My Point Object is at : 123,456’ 23 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 24 Inheritance Take a class and extend Seen this when we create our Form: public partial class Form1 : Form Let’s create Circle Class from our Point Class Can add radius and area Code: public Circle : Point 24 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 25 Add new class Circle: (Project > Add class) Call it Circle, code: class Circle : Point { } Can now create a circle: Circle smallcircle = new Circle( ); Because we are using existing code, it’s more reliable 25 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 26 Extend – define radius Constructors: class Circle : Point { private double _radius; // internal – private public Circle( ) { } public Circle(int xValue, int yValue, double radius) { _x = xValue; // _x and _y now declared protected in Point class // still private to outside world _y = yValue; _radius = radius; } 26 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 27 // add property - radius – use get and set public double radius { get { return _radius; } set { if (value >= 0) _radius = value; } } 27 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 28 Extend further Add method – Area: // method Area public double area( ) { return Math.PI * _radius * _radius; } 28 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 29 Override ToString method: public override string ToString() { return "Circle at x,"+_x+" y,"+_y+ "radius,"+_radius; } 29 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 30 Use: Circle smlCircle = new Circle( ); Circle largeCircle = new Circle(12, 34, 56); smlCircle.X = 98; smlCircle.Y = 87; smlCircle.Radius = 10; MessageBox.Show(smlCircle.ToString( )); MessageBox.Show(largeCircle.ToString( )); MessageBox.Show (smlCircle.area( ).ToString( )); 30 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – OOP2 - 31 Summary: • • • • • Adding methods Static classes – available to all objects Overriding default methods Inheritance – extend class Protected declaration 31 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 32 Topics: • Serial port Parallel port API DLLs USB • USB Module • • • 32 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 33 Serial Port control Non-visual control. Properties: • BaudRate: 9600, • DataBits: 8, • Parity: None, • PortName: COM1, • StopBits: One. Main event: DataReceived Occurs when data is received from the port 33 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 34 Needs: using System.IO.Ports; Set properties serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), "None"); serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "One"); Open device serialPort1.Open(); 34 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 35 Send and receive data serialPort1.WriteLine(textBox1.Text); listBox1.Items.Add(serialPort1.ReadLine()); Or use DataReceived event 35 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 36 e.g: private void serialPort1_DataReceived (object sender, SerialDataReceivedEventArgs e) { listBox1.Items.Add(serialPort1.ReadLine()); } 36 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 37 Parallel interface. One way of getting digital I/O. Data register: Bits 0-7 data Status Register: Bits: 0-2 not used, 3-Error, 4-Select, 5paper out, 6-acknowledge, 7 busy. Control Register: Bits: 0 strobe, 1-Auto-feed, 2-initialise, 3-select, 4-IRQ enable, 5-7 not used Base address (data register) is at 0x378 Status and control at 0x379 and 0x37A. Eight outputs Only status register bits are guaranteed inputs 37 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 38 Accessing the parallel port Use inpout32.dll - Lake View Research (www.lvr.com). Provides direct read and write of the I/O [DllImport("inpout32.dll", EntryPoint = "Out32")] public static extern void Output(int adress, int value); [DllImport("inpout32.dll", EntryPoint = "Inp32")] public static extern int Input(int address); Use: Output(port, data); temp = Input(port); 38 // writes data to port // read port, puts data in temp © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 39 USB interfacing Most popular way of interfacing to the PC. Complete design involves: • Hardware / USB interface • PC drivers • Understanding protocol and hardware limitations Difficult 39 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 40 The USB interface - 1 USB 2.0 three modes of operation: • High speed (480 Mbits/s), • Full speed (12 Mbits/s) and • Low speed (1.5 Mbits/s). Device indicates its speed by pulling D+ or D- data line high. Power can be taken from USB bus – but strict limitations 40 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 41 The USB interface – 2 The host controls the bus - initiates & controls all messages Up to 127 devices on the bus - a device may not run at its full speed. USB Connectors: The A-type is exclusively for a host B-types are for connection to slaves. Smaller B-type for small devices such as mobile phones and digital cameras. 41 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 42 USB interfacing Many manufacturers make USB / I/O modules One is from DLP design: DLP-245PB-G 42 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 43 The module features - 1: USB 1.0 and 2.0 compatible – communication at up to 2Mbits/s 18 digital I/O lines (6 as A/D inputs) Programmable Microchip 16F877A PIC Pre-programmed code to interface to USB • • • • 43 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 44 The module features - 2: • Code provides access to: I/O (analogue and digital) EEPROM and external digital temperature sensors • Access to the PIC data bus for further expansion. • No in-depth knowledge of USB hardware or software is required • 40-pin DIL pin-out: further expansion is easy. 44 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 45 Using the module Install drivers and DLL – can then use from C# Can read and write directly to I/O Need to understand protocol 45 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 46 46 © 2009 Elektor International Media C# Programming for Engineers, J Allwork C# and .NET programming – Hardware 47 Summary • Serial port Parallel port API DLLs USB • USB Module • • • 47 © 2009 Elektor International Media C# Programming for Engineers, J Allwork