Using the ActiveXperts Serial Port Component with Visual CSharp .NET
ActiveXperts Serial Port Component is a software development kit (SDK) that enables the user to communicate to a device over a serial interface.
Such a device can be: a weight indicator, a modem, a scanner, or any other device that is equiped with a serial port. It can even be another PC, connected via a NULL modem cable.
ActiveXperts Serial Port Component features the following:
- Direct COM port support (like 'COM1')
- TAPI (Windows Telephony Device) support (like 'Standard 56000 bps Modem');
- Support for RS-232/RS422/RS485, up to 256 simultaneous ports;
- Support for all types of Hayes compatible modems;
- Support for serial cable as well as USB cable or Bluetooth connections;
- Support for Virtual COM ports (i.e. COM ports redirected through the network);
- Hardware flow control (RTS/CTS, DTR/DSR) and software flowcontrol (XON/XOFF) support;
- Configurable baudrate/parity/stopbits, full buffered data transfer, text/binary data transfer.
Step 1: Download and install the ActiveXperts Serial Port Component
Download the the ActiveXperts Serial Port Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Step 2: Create a new Visual C# .NET Project
Launch Microsoft Visual Studio (for instance 'Microsoft Visual Studio 2015') from the Start menu. Choose 'New' from the 'File' menu and click on 'Project'. In the 'New Project' dialog, select a Visual Studio template (for instance: 'Console Application'). Select a name for the application (for instance: 'DemoApp') and a name for the solution (for instance: 'DemoSolution'). Also, select the directory where you want to store the project (for instance: 'C:\MyProjects):
Step 3: Refer to the ActiveXperts Serial Port Component Library and create the objects
Now that a new project has been created, you must add a reference to the ActiveXperts Serial Port Component in the project to be able to use the ActiveXperts Serial Port Component object. To do so, choose 'Add Reference...' from the 'Project' menu. In the 'Add Reference' dialog that pops up, select the 'COM' tab and select the 'Serial Port Component 2.2 Type Library' as shown in the following picture:
Click 'OK' to close the 'Add Reference' dialog.
On top of your code, type the following line to use the ActiveXperts Serial Port Component namespace:
using AxSms;
In your Main function, declare and create the following object:
public ComPort m_objComport; m_objComport = new ComPortClass();
Step 4: Send an AT command to a connected Hayes compatible modem
You can now send and/or receive data to and/or from a serial interface.
The following code shows how to query a modem:
using System; using AxSms; namespace AxSerialPort_Demo { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> /// static public ComPort m_objComport; static public void WriteStr(System.String str) { m_objComport.WriteString(str); Console.WriteLine("-> " + str + "\n" ); } static public void ReadStr() { System.String str; str = "notempty"; m_objComport.Sleep(200); while( str != "" ) { str = m_objComport.ReadString(); if( str != "") Console.WriteLine( "<- " + str + "\n"); } } [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // m_objComport = new ComPortClass(); m_objComport.BaudRate = 9600; m_objComport.Device = "COM1"; m_objComport.LogFile = "C:\\ComportLog.txt"; m_objComport.Open(); if( m_objComport.LastError == 0 ) { Console.WriteLine( "Open: SUCCESS\n" ); } else { Console.WriteLine( "Open failed, Error : " + m_objComport.GetErrorDescription( m_objComport.LastError ) + "\n" ); } if( m_objComport.LastError == 0 ) { WriteStr( "atz" ); ReadStr(); WriteStr( "at&f" ); ReadStr(); Console.WriteLine( "Close\n" ); m_objComport.Close(); } m_objComport.Sleep( 2000 ); } } }
You can download the full samples here.
NOTE: Demo Projects are created with Microsoft Visual Studio 2015
The Serial Port Component project ships with a set of Microsoft Visual Studio .NET samples, including samples for Microsoft Visual C# .NET. The projects are created with Microsoft Visual Studio 2015.
Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.