** Hi WillI think you might**
Hi Will
I think you might overestimate my C# skills, but I’ll try to answer some of your questions.
The source files for C# has the file extension .cs and yes they are the source files that the compiler uses to build the executable code. In C# they are called class files and you can add as many as you need to your project. I normally split up my program in to different class files. For example I could have a file called COM_LCD.cs that holds the class for the LCD handling, or a GPS.cs for doing all the work on a GPS stream.
So if we look at the COM_LCD.cs file
using System;
using System.Threading;
using Microsoft.SPOT;
using System.Text;
using System.IO.Ports;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
namespace SauID
{
class COM_LCD
{
static PWM Buzz;
private SerialPort LCDhandle;
const byte DISP_ON = 0xC; //Turn visible LCD on
const byte DISP_OFF = 0X08;
const byte CLR_DISP = 0x01; //Clear display
const byte CUR_HOME = 2; //Move cursor home and clear screen memory
const byte SET_CURSOR = 0x80; //SET_CURSOR + X : Sets cursor position to X
const byte MOVE_CURSOR_LEFT = 0x10;
const byte MOVE_CURSOR_RIGHT = 0x14;
const byte SCROLL_RIGHT = 0x1C;
const byte SCROLL_LEFT = 0x18;
const byte CURS_UNDERLINE_ON = 0x0E;
const byte CURS_UNDERLINE_OFF = 0x0C;
const byte CURS_BOX_ON = 0x0D;
const byte CURS_BOX_OFF = 0x0C;
const byte BRIGHTNESS_OFF = 128;
const byte BRIGHTNESS_40 = 140;
const byte BRIGHTNESS_73 = 150;
const byte BRIGHTNESS_ON = 157;
private void Command(byte cmd)
{
SendByte(0xFE);
SendByte(cmd);
}
private void SendByte(byte value)
{
byte[] buffer = new byte[1];
buffer[0] = value;
LCDhandle.Write(buffer, 0, buffer.Length);
}
public void ClearScreen()
{
Command(CLR_DISP);
}
public void Biip()
{
Buzz.Set(true);
Thread.Sleep(400);
Buzz.Set(false);
}
public void Error()
{
for (int i = 0; i < 5; i++)
{
Buzz.Set(true);
Thread.Sleep(200);
Buzz.Set(false);
Thread.Sleep(200);
}
}
public void ScrollLeft()
{
Command(SCROLL_LEFT);
}
public void SetCursor(byte row, byte col)
{
Command((byte)(SET_CURSOR | row << 6 | col));
}
public void BacklightOn()
{
Command(BRIGHTNESS_ON);
}
public void Write(string Tekst)
{
string LCDTekst = Tekst + " ";
LCDTekst = LCDTekst.Substring(0, 16);
byte[] buffer = Encoding.UTF8.GetBytes(LCDTekst);
LCDhandle.Write(buffer, 0, buffer.Length);
Thread.Sleep(10);
}
public COM_LCD(string comPort, int baudrate)
{
Buzz = new PWM((PWM.Pin)FEZ_Pin.PWM.Di10);
LCDhandle = new SerialPort(comPort, baudrate);
LCDhandle.Open();
Command(DISP_ON);
Command(CLR_DISP);
}
}
}
You will see the following things
The using statements includes the .net libraries that is needed for the class
Then you have the namespace that should be the same as your main program
Now the class definition starts with the class COM_LCD statement. All classes must have at least one constructor and that’s the ‘public COM_LCD(string comPort, int baudrate)’ this will build your object and needs two input parameters, the COM port and the baudrate.
All the public functions like ‘public void ClearScreen()’ will be available to the user and recognized by intellicense. So in your main program you could have a line like myLCD.ClearScreen();
All private functions are internal functions for this class like ‘private void Command(byte cmd)’ This will not show up when you use the object and will not show up in intellicense.
I can understand that you are a bit confused about this function / method stuff, but think of it this way. A function is a subroutine in the program or class that you’re working on. So if you are in the Main() part of your program you can call functions within that block. If you have an LCD object then you can call a function in that object like myLCD.ClearScreen(); but this is a function within that class and then it is referred to as a method.
So in your Main() program loop you could have a statement like
runMotor(20,60);
And further down (below the main block) you should add your function
static void runMotor(int LeftMotor, int RightMotor)
{
… do something
}
Notice the ‘static’ that says that this function should be available everywhere within the ‘program’ class. ‘void’ indicates that we are not expecting any result back, and you have your parameters…
I don’t know if this helps. But there are tons of C# tutorials out there and most of them apply when it comes down to the basics of C#
Keep up the good work and keep spreading the word