SetDeviceFileData

Function

Objective

The function is used to transfer a file from the PC to the device. It mainly used to transfer the updade file. The updade file name is emfw.cfg.

Parameter description

[in]: The handle that is returned when the connection is successful.

[in]: The name of the file transferred to the device, for example, a emfw.cfg file.

[in]: The data buffer used to transfer a file.

[in] Length of the transferred data.

[in]: The default value is null; it is used for extension.

Returned value

When the returned value is 0 or a positive value, it indicates that the operation is successful. When the returned value is a negative value, it indicates that the operation fails. Attached table 5 lists the information about the error codes.

Example

file_name = ”emfw.cfg” buff_len = len(file_name) pfile_name = create_string_buffer(file_name) pbuffer = create_string_buffer(buff_len) ret = self.commpro.SetDeviceFileData(self.hcommpro, pfile_name, pbuffer, buff_len, "")

int ret = 0; string filename = "emfw.cfg "; FileStream fsFile = File.OpenRead(this.openFileDialog1.FileName); string buffersize = (int)fsFile.Length; byte[] buffer = new byte[buffersize]; string options = ""; ret = SetDeviceFileData(h, filename, ref buffer[0], buffersize, options);

 

Ejemplo

C#

        [DllImport("plcommpro.dll", EntryPoint = "SetDeviceFileData")]
public static extern int SetDeviceFileData(IntPtr h, string filename, ref byte buffer, int buffersize, string options);
private void btnSetDevFile_Click(object sender, EventArgs e)
        {
string filename = "";
int buffersize = 0, ret = 0;
//byte[] buffer = new byter[buffersize];
string options = "";
            FileStream fsFile = File.OpenRead(filename);
            buffersize = (int)fsFile.Length;
byte[] buffer = new byte[buffersize];
if (fsFile.Read(buffer, 0, buffersize) != buffersize)
            {
                MessageBox.Show("Read file error!");
            }
else
            {
if (IntPtr.Zero != h)
                {
                    ret = SetDeviceFileData(h, filename, ref buffer[0], buffersize, options);
if (ret >= 0)
                    {
                        MessageBox.Show("Uploaded file succeed!");
return;
                    }
else
                    {
                        MessageBox.Show("Uploaded file failed!error code is " + ret);
//ret = PullLastError();
return;
                    }
                }
else
                {
                    MessageBox.Show("Handle has disconnect!");
return;
                }
            }
        }

VB

<DllImport("plcommpro.dll", EntryPoint := "SetDeviceFileData")> _
Public Shared Function SetDeviceFileData(h As IntPtr, filename As String, ByRef buffer As Byte, buffersize As Integer, options As String) As Integer
End Function
Private Sub btnSetDevFile_Click(sender As Object, e As EventArgs)
Dim filename As String = ""
Dim buffersize As Integer = 0, ret As Integer = 0
'byte[] buffer = new byter[buffersize];
Dim options As String = ""
Dim fsFile As FileStream = File.OpenRead(filename)
    buffersize = CInt(fsFile.Length)
Dim buffer As Byte() = New Byte(buffersize - 1) {}
If fsFile.Read(buffer, 0, buffersize) <> buffersize Then
        MessageBox.Show("Read file error!")
Else
If IntPtr.Zero <> h Then
            ret = SetDeviceFileData(h, filename, buffer(0), buffersize, options)
If ret >= 0 Then
                MessageBox.Show("Uploaded file succeed!")
Return
Else
                MessageBox.Show("Uploaded file failed!error code is " + ret)
'ret = PullLastError();
Return
End If
Else
            MessageBox.Show("Handle has disconnect!")
Return
End If
End If
End Sub