WriteArray Method

Writes specified number of bytes from array to serial port.

Syntax

Visual Basic
Function WriteArray(DataBuffer As array of Byte, BytesToWrite As Long) As Long 
Visual C++ (MFC)
long WriteArray(VARIANT DataBuffer, long BytesToWrite);
Visual C++ (#import)
LONG WriteArray(VARIANT DataBuffer, LONG BytesToWrite);

Parameters

DataBuffer

[in] SafeArray of bytes to write.

BytesToWrite

[in, optional] Number of bytes to write to serial port.

Return

Number of bytes written to serial port. It can write fewer bytes than requested; the return value must be noted, and the reminder of the operation should be retried when possible.

Errors

The method may throw exception. Use GetLastError method to get the error code.

Code Example

'--------------------------------------------------------------------
' Visual Basic 6 - Write array
'--------------------------------------------------------------------
On Error GoTo ErrorHandler
	Dim DataToWrite(5) As Byte
	For i = 1 To 5
		DataToWrite(i) = i
	Next i
	Call FTSPCControl1.WriteArray(DataToWrite, 5)
Exit Sub

ErrorHandler:
	Dim err_code As Integer
	err_code = FTSPCControl1.GetLastError
	MsgBox "ErrorCode = " & CStr(err_code) & " - " & Err.Description, _
	vbCritical, "Error!"


/////////////////////////////////////////////////////////////////////
// C++ Example - Write array
/////////////////////////////////////////////////////////////////////
char *WriteBuffer = "Data to write";
VARIANT V;
V.vt = (VT_ARRAY|VT_UI1);
SAFEARRAY* psa;
psa = SafeArrayCreateVector(VT_UI1, 1, strlen(WriteBuffer));

if (psa != NULL)
{
	SafeArrayLock(psa);
	memcpy(psa->pvData, PVOID(WriteBuffer), strlen(WriteBuffer));
	SafeArrayUnlock(psa);
	V.parray = psa;
}

try
{
	m_SPCControl1.WriteArray(V, sizeof(WriteBuffer));
}
catch(COleDispatchException* E)
{
	MessageBox(E->m_strDescription, mbCaption, MB_OK | MB_ICONERROR);
}

if (psa != NULL)
{
	SafeArrayDestroy(psa);
}

See Also

Write