Excel Serial Communication Example

  1. Excel Serial Communication Example Modbus Twin Cat
  2. Serial Communication Basics
  3. Can Serial Communication

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.

' 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. ' 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. 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. 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.

Excel Serial Communication Example

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.

Excel Serial Communication Example Modbus Twin Cat

Hello, I have a temperature monitor that is connected to my computer via RS232 to COM1. (The actual connection is via a COM to USB cable.) Small records of 10 charracters + Carriage-Return are sent per communication. I can receive the data with Hyper Terminal program of XP on COM1. If I could direct the data to EXCEL then I could resume with the data collection. I can try to create a program to write the data in comma separated format in a file but that is not my intention. Is this possible to do without programming? What is the best way to do this?

Serial Communication Basics

Thank you, Harry Geda. I have tried using the above VBA code to read from a serial port and it works very well but with one small issue: If there is no incoming data, or less data than RecLen, then the 'Get' statement locks up until there is more data available. I have also tried changing to 'binary' instead of 'random' for the filemode so I can read one byte at a time but still have the same problem. Is there some way of checking if there is incoming data available before calling 'get' (eg like the 'kbhit' function in C) or alternatively some way of making the 'get' function time out if it doesn't return within a certain time?

QUOTE=Johnw;3548242That code is all you need to answer the specific question asked by the OP, but your situation might be different. I must thank for this info and code - it has get me close to my solution). Excaktly as you i have excel2003 and runs this in makro. I hope to catch info from an attached GPS in COM18 sending NMEAcodes. BUT do you have any idea why the code above just finds blank/empty records? So the code doesnt comes in to the central parts. I have tested to take out my GPS and it works just the same way!

Also when i change the code to a comport that doesn exist it behavse the same! Why doesnt i hook on the signals at all??

Can Serial Communication

Do you have any idea och thoughts it would be lovely! The gps is a bu353 and have a programproduct sent along with it which is working fine and shows the COM18 code in a window rolling over ghe screen. So there is a signal in the port! Ingvar Sweden.