Writes specified number of bytes from array to serial port.
| 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);
|
DataBuffer
[in] SafeArray of bytes to write.
BytesToWrite
[in, optional] Number of bytes to write to serial port.
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.
The method may throw exception. Use GetLastError method to get the error code.
'--------------------------------------------------------------------
' 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);
}