Friday, April 15, 2005
Serial Communcation using SerialComm library
Download the source code from http://www.codeproject.com/dotnet/DotNetComPorts.asp
The idea behind Serial port.
In order to use a com port, we instatiate an obect of SerialPort.
The class SerialPort defines several events that we can handle via delegates. This is encapsulated in the WithEvents class. The one-argument constructor of SerialPort takes in an object of WithEvents class.
Prior to using reading or writing to a comm port, the port must be opened via the SerialPortObject.Open(1) method. This method takes in a dummy index (but doesn't really do anything to the index).
The default port configuration for SerialPort resides in the SerialCnfg class. But the best way to configure the port is via the Settings dialog box. This dialog box will automatically serialize the values so that we can use it a second time. If no values are provided, SerialCnfg will use its defaulted value (9600 bps, no hardware handshake, no parity).
Steps to Initiaze the SerialPort
(1) Declare an WithEvent and SerialPort objects
That's it!
Send an array of Byte
(1) Call any one of the overloaded members
That's it!
Receive stuff
Receiving stuff is a bit tricky because there are two ways to receive -- via pooling the com port or via delegate. I’d recommend the delegate method.
This is what you do
(1) Check the OnEvent Receive Mode in the Settings dialog box.
THat's it!
The method delegate you hooked up in the serialPortEvents.RxChar like so:
The idea behind Serial port.
In order to use a com port, we instatiate an obect of SerialPort.
The class SerialPort defines several events that we can handle via delegates. This is encapsulated in the WithEvents class. The one-argument constructor of SerialPort takes in an object of WithEvents class.
Prior to using reading or writing to a comm port, the port must be opened via the SerialPortObject.Open(1) method. This method takes in a dummy index (but doesn't really do anything to the index).
The default port configuration for SerialPort resides in the SerialCnfg class. But the best way to configure the port is via the Settings dialog box. This dialog box will automatically serialize the values so that we can use it a second time. If no values are provided, SerialCnfg will use its defaulted value (9600 bps, no hardware handshake, no parity).
Steps to Initiaze the SerialPort
(1) Declare an WithEvent and SerialPort objects
WithEvents serialPortEvents;(2) Hook up the delegates and instantiate the objects
SerialPort com1;
serialPortEvents = new WithEvents();(3) Set up a "Settings" button click event to call the Settings dialog like so
serialPortEvents.Error = new StrnFunc(this.OnError);
serialPortEvents.RxChar = new ByteFunc(this.OnRecvI);
serialPortEvents.CtsSig = new BoolFunc(this.OnCts);
serialPortEvents.DsrSig = new BoolFunc(this.OnDsr);
// Get a connection
com1 = new SerialPort(serialPortEvents);
com1.Open(1);
Settings frm = new Settings(com1.ConfigFileName, com1.Cnfg);com1.ConfigFileName is defaulted to "Port1.cfg" in its constructor.
frm.ShowDialog();
That's it!
Send an array of Byte
(1) Call any one of the overloaded members
public uint Send(byte chr) ;The return value is the amount of bytes sent.
public uint Send(string s);
public uint Send(byte[] buffer);
That's it!
Receive stuff
Receiving stuff is a bit tricky because there are two ways to receive -- via pooling the com port or via delegate. I’d recommend the delegate method.
This is what you do
(1) Check the OnEvent Receive Mode in the Settings dialog box.
THat's it!
The method delegate you hooked up in the serialPortEvents.RxChar like so:
serialPortEvents.RxChar = new ByteFunc(OnRecvI);will be called. THe method, like so
void OnRecvI(byte[] byteReceived)will be called whenever an incoming byte its received.