Excel 2010 Vba Serial Port Communication
You've configured communication parameters (baud rate, number of data bits, flow control etc) in the data logger and can receive any data without communication errors. Solution: Our software allows you to send your data from RS232 (COM port) to Excel in followings ways.
Introduction to Serial Port Communication in VBA The purpose of this article is to demonstrate how you can perform serial port communication in the VBA (Visual Basic Applications - script editor included in any typical Microsoft Excel distribution) but without using the MSComm control or any other third party add-on or ActiveX. The great advantage of this method, which uses API functions to call the serial port directly, is that you do not really need to install anything on your PC, apart from the Excel itself (which, let’s admit it, already exists on most PCs). Other methods always require the installation of an ActiveX, or at least the registration of an “.ocx” file, like for instance the MSComm control. Even the method of programming in C#, which I have suggested in a different article () needs the installation of the.NET Framework and of the C# compiler itself. It is assumed that the reader would have some basic knowledge of programming (but really basic, not even intermediate) In order to get access to the VBA script editor, you first need to make the corresponding buttons available on the Excel toolbars (they are not there by default). Once you have started your Excel, choose from the top menu: View -> Toolbars -> Visual Basic.
This will make visible another small toolbar, with the following buttons. These buttons will enable you to launch the editor in which you will be able to write the Visual Basic Code and will also allow you to directly create windows-style controls like textboxes, push buttons, labels, radio buttons, checkboxes etc.
In order to make these available on the main toolbar, you need to activate the Control Toolbox button, from the small toolbar activated at the previous step. This will, in turn, make available a number of new other buttons that will allow you to build the mentioned controls. As a goal for this article, I would intend to demonstrate how you open the serial port, how you write a few bytes, how you read some bytes, and then how you close the serial port. In order to do all these, you will need to create four windows-style buttons, each of them performing one of the tasks that I have enumerated before. Click on the Command Button control from the recently activated toolbar, and then draw 4 similar buttons anywhere on the surface of your excel worksheet (it will be a challenge to draw them all of the same size!). The effect of this action will be the opening in the main window of this environment of the actual sheet in which you will write the VB code.
Once you reached this stage, copy paste the code below in there. This Visual Basic Code “defines” the constants, structures and API functions that you will use later to perform the intended operations.
The comments in the code are self explanatory.The article continues after the end of this code, please scroll all the way down. Serial Port Communication Script Option Explicit '------------------------------------------------------------------------------- ' ' This VB module is a collection of routines to perform serial port I/O without ' using the Microsoft Comm Control component. This module uses the Windows API ' to perform the overlapped I/O operations necessary for serial communications. ' ' The routine can handle up to 4 serial ports which are identified with a ' Port ID. ' ' All routines (with the exception of CommRead and CommWrite) return an error ' code or 0 if no error occurs. The routine CommGetError can be used to get ' the complete error message. Foxconn 45cmx lan drivers xp windows 7. First of all, make sure that the “Design Mode” button is activated in the second toolbar that you added throughout this process: Now you can give appropriate names to each of the four buttons you have created before.
In order to do this, right-click on the first of these buttons, and from the pop-up menu, choose the Properties option: A window will open with all the properties of the button object. Change the Caption property to “Initialise”, and then close the window. This will change the label on the first button to this very text: Once you do this for the first button, repeat the operation and change the labels on the other three buttons to “Write”, “Read” and “Close”. Now it is time to start adding code to your buttons.
In order to do this, click on the first button you created, and from the pop-up menu, choose the View Code option: This will open again the scripting environment, adding a new function to the code; it will mark the beginning of the function: Private Sub CommandButton1_Click() End Sub This is the place where you will write the code to initialize the serial port. For this, we will be using the CommOpen function, defined above, which takes as parameters the ID of the COM port you want to open (COM1, COM2 etc) and a few strings that define the baud rate, the parity and the number of data and stop bits. The code for realizing this is: Private Sub CommandButton1_Click() Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4 Dim lngStatus As Long intPortID = 1 ' Open COM port lngStatus = CommOpen(intPortID, 'COM' & CStr(intPortID), _ 'baud=9600 parity=N data=8 stop=1') End Sub It opens COM1, at a baud rate of 9600, no parity, 8 data bits and one stop bit.
Hd saranya sasi hot sex photos. We use cookies to ensure that we give you the best experience on our website.