ReadArray Method

Reads specified number of bytes from serial port to array.

Syntax

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

Parameters

DataBuffer

[out] Array of received bytes

BytesToRead

[in, optional] Number of bytes to read from serial port

Return

Number of bytes read from serial port.

Errors

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

Code Example

'--------------------------------------------------------------------
' Visual Basic 6 - Read array
'--------------------------------------------------------------------
Private Sub FTSPCControl1_OnReceive(ByVal Count As Long)
On Error GoTo ErrorHandler
	Dim buf(1) As Byte
	ReadCnt = FTSPCControl1.ReadArray(buf, 1)

	If ReadCnt > 0 Then
		For i = 0 To ReadCnt - 1
			Text_Terminal.Text = Text_Terminal.Text + Chr(buf(i))
		Next i
	End If
Exit Sub

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


/////////////////////////////////////////////////////////////////////
// C++ Example - Read array
/////////////////////////////////////////////////////////////////////
void CSPCDemoDlg::OnReceiveFtspccontrol1(unsigned long Count)
{
	SAFEARRAY* psa;
	psa = SafeArrayCreateVector(VT_UI1, 1, Count);
	VARIANT V;
	V.vt = VT_ARRAY|VT_UI1;
	V.parray = psa;
	LONG ReadCnt;

	try 
	{
		ReadCnt = m_SPCControl1.ReadArray(&V, Count);
	}
	catch(COleDispatchException* E)
	{
		MessageBox(E->m_strDescription, mbCaption,
			MB_OK | MB_ICONERROR);
	}

	m_edit_Terminal.GetWindowTextW(tmp_edit_text);
	SafeArrayLock(V.parray);
	for (int i = 0; i < ReadCnt; i++)
	{
		PCHAR P = (PCHAR)V.parray->pvData + i;

		//Todo: add your code

	}
	SafeArrayUnlock(V.parray);
	SafeArrayDestroy(psa);
}