Excel Serial Communication Examples

Excel Serial Communication Examples Average ratng: 3,2/5 5890 votes

Microsoft Excel VBA Below are archived posts from the MrExcel Message Board about Microsoft Excel VBA. You will find lots of tips and tutorials for using visual basic in Excel. Check out the MrExcel tutorials for more information on how to properly use Excel VBA.

In a previous thread I was complaining about how MS dropped the MSComm API for office 07 and 10. After a lot of searching I finally found a way to communicate through the USB port using Excel and thought I would share. Below is the code I used and I also attached the Excel file and the source code I used to communicate with a PIC32 using a FTDI interface chip.

The Excel file just opens, writes, and reads data over a COM port. The PIC32 just echo's data that was received using the UART interrupt and toggles an LED to show UART activity. The VBA code came from here: I just modified the code slightly for my own purposes. Due to the character restrictions, I will post everything in a few posts. Code: 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. Code: Private Declare Function ClearCommError Lib 'kernel32' (ByVal hFile As Long, lpErrors As Long, lpStat As COMSTAT) As Long ' ' Closes an open communications device or file handle.

Excel

' Private Declare Function CloseHandle Lib 'kernel32' (ByVal hObject As Long) As Long ' ' Creates or opens a communications resource and returns a handle ' that can be used to access the resource. ' Private Declare Function CreateFile Lib 'kernel32' Alias 'CreateFileA' (ByVal lpFileName As String, ByVal dwDesiredAccess As LongByVal dwShareMode As Long, lpSecurityAttributes As AnyByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As LongByVal hTemplateFile As Long) As Long ' ' Directs a specified communications device to perform a function. ' Private Declare Function EscapeCommFunction Lib 'kernel32' (ByVal nCid As Long, ByVal nFunc As Long) As Long ' ' Formats a message string such as an error string returned ' by anoher function. ' Private Declare Function FormatMessage Lib 'kernel32' Alias 'FormatMessageA' (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As LongByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As LongArguments As Long) As Long ' ' Retrieves modem control-register values. ' Private Declare Function GetCommModemStatus Lib 'kernel32' (ByVal hFile As Long, lpModemStat As Long) As Long ' ' Retrieves the current control settings for a specified ' communications device. ' Private Declare Function GetCommState Lib 'kernel32' (ByVal nCid As Long, lpDCB As DCB) As Long ' ' Retrieves the calling thread's last-error code value. ' Private Declare Function GetLastError Lib 'kernel32' As Long ' ' Retrieves the results of an overlapped operation on the ' specified file, named pipe, or communications device.

Excel Serial Communication Example

' Private Declare Function GetOverlappedResult Lib 'kernel32' (ByVal hFile As Long, lpOverlapped As OVERLAPPEDlpNumberOfBytesTransferred As Long, ByVal bWait As Long) As Long ' ' Discards all characters from the output or input buffer of a ' specified communications resource. It can also terminate ' pending read or write operations on the resource.

' Private Declare Function PurgeComm Lib 'kernel32' (ByVal hFile As Long, ByVal dwFlags As Long) As Long ' ' Reads data from a file, starting at the position indicated by the ' file pointer. After the read operation has been completed, the ' file pointer is adjusted by the number of bytes actually read, ' unless the file handle is created with the overlapped attribute. ' If the file handle is created for overlapped input and output ' (I/O), the application must adjust the position of the file pointer ' after the read operation. ' Private Declare Function ReadFile Lib 'kernel32' (ByVal hFile As Long, ByVal lpBuffer As StringByVal nNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As LonglpOverlapped As OVERLAPPED) As Long ' ' Configures a communications device according to the specifications ' in a device-control block (a DCB structure).

The function ' reinitializes all hardware and control settings, but it does not ' empty output or input queues. ' Private Declare Function SetCommState Lib 'kernel32' (ByVal hCommDev As Long, lpDCB As DCB) As Long ' ' Sets the time-out parameters for all read and write operations on a ' specified communications device. ' Private Declare Function SetCommTimeouts Lib 'kernel32' (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long ' ' Initializes the communications parameters for a specified ' communications device. ' Private Declare Function SetupComm Lib 'kernel32' (ByVal hFile As Long, ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As Long ' ' Writes data to a file and is designed for both synchronous and a ' synchronous operation.

Communication

The function starts writing data to the file ' at the position indicated by the file pointer. After the write ' operation has been completed, the file pointer is adjusted by the ' number of bytes actually written, except when the file is opened with ' FILEFLAGOVERLAPPED.

Examples

If the file handle was created for overlapped ' input and output (I/O), the application must adjust the position of ' the file pointer after the write operation is finished. Code: '- ' CommOpen - Opens/Initializes serial port.

' ' ' Parameters: ' intPortID - Port ID used when port was opened. ' strPort - COM port name. (COM1, COM2, COM3, COM4) ' strSettings - Communication settings.

' Example: 'baud=9600 parity=N data=8 stop=1' ' ' Returns: ' Error Code - 0 = No Error. ' '- Public Function CommOpen(intPortID As Integer, strPort As StringstrSettings As String) As Long Dim lngStatus As Long Dim udtCommTimeOuts As COMMTIMEOUTS On Error GoTo RoutineError ' See if port already in use. If udtPorts(intPortID).blnPortOpen Then lngStatus = -1 With udtCommError.lngErrorCode = lngStatus.strFunction = 'CommOpen'.strErrorMessage = 'Port in use.' End With GoTo RoutineExit End If ' Open serial port. UdtPorts(intPortID).lngHandle = CreateFile(strPort, GENERICREAD Or GENERICWRITE, 0, ByVal 0&, OPENEXISTING, FILEATTRIBUTENORMAL, 0) If udtPorts(intPortID).lngHandle = -1 Then lngStatus = SetCommError('CommOpen (CreateFile)') GoTo RoutineExit End If udtPorts(intPortID).blnPortOpen = True ' Setup device buffers (1K each). LngStatus = SetupComm(udtPorts(intPortID).lngHandle, 1024, 1024) If lngStatus = 0 Then lngStatus = SetCommError('CommOpen (SetupComm)') GoTo RoutineExit End If ' Purge buffers.

LngStatus = PurgeComm(udtPorts(intPortID).lngHandle, PURGETXABORT Or PURGERXABORT Or PURGETXCLEAR Or PURGERXCLEAR) If lngStatus = 0 Then lngStatus = SetCommError('CommOpen (PurgeComm)') GoTo RoutineExit End If ' Set serial port timeouts. With udtCommTimeOuts.ReadIntervalTimeout = -1.ReadTotalTimeoutMultiplier = 0.ReadTotalTimeoutConstant = 1000.WriteTotalTimeoutMultiplier = 0.WriteTotalTimeoutMultiplier = 1000 End With lngStatus = SetCommTimeouts(udtPorts(intPortID).lngHandle, udtCommTimeOuts) If lngStatus = 0 Then lngStatus = SetCommError('CommOpen (SetCommTimeouts)') GoTo RoutineExit End If ' Get the current state (DCB). LngStatus = GetCommState(udtPorts(intPortID).lngHandleudtPorts(intPortID).udtDCB) If lngStatus = 0 Then lngStatus = SetCommError('CommOpen (GetCommState)') GoTo RoutineExit End If ' Modify the DCB to reflect the desired settings.

LngStatus = BuildCommDCB(strSettings, udtPorts(intPortID).udtDCB) If lngStatus = 0 Then lngStatus = SetCommError('CommOpen (BuildCommDCB)') GoTo RoutineExit End If ' Set the new state. Code: '- ' CommClose - Close the serial port. ' ' Parameters: ' intPortID - Port ID used when port was opened. ' ' Returns: ' Error Code - 0 = No Error. Code: '- ' CommGetLine - Get the state of selected serial port control lines. ' ' Parameters: ' intPortID - Port ID used when port was opened. ' intLine - Serial port line.

CTS, DSR, RING, RLSD (CD) ' blnState - Returns state of line (Cleared or Set). ' ' Returns: ' Error Code - 0 = No Error.