Ora ti posto tutto il codice completo . Chiaramente non guardare gli eventuali errori non gestiti . Ho fatto in modo che alla fine della lettura del file venga generato un pacchetto di dimensione 0 e numero di byte 0 . In ricezione scrivo fino a quando non mi giunge questo pacchetto : guarda e fammi sapere .
Codice:
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_NEW = 1
Const CREATE_ALWAYS = 2
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Private Declare Function WriteFile Lib "kernel32" (ByVal hfile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hfile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'La costante sotto la puoi cambiare per aumentare il numero masismo di byte
Const BYTE_PER_MSG = 16
Private Sub SendMsg(num As Long, buf() As Byte)
Dim tmp(4) As Byte, I As Long
Dim v As Variant
tmp(1) = (num And &HFF000000) / &H1000000
tmp(2) = (num And &HFF0000) / &H10000
tmp(3) = (num And &HFF00) / &H100
tmp(4) = num And &HFF
v = tmp
MSComm1.Output = v
'Non so se buf vada ridimensionato comunque lo faccio uguale
v = buf
MSComm1.Output = v
End Sub
Private Sub Command1_Click()
Dim buffer(BYTE_PER_MSG) As Byte, f As Long, read As Long
CommonDialog1.ShowOpen
f = CreateFile(CommonDialog1.FileName, GENERIC_READ, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, 0, 0)
If f = -1 Then
MsgBox "Errore nell'apertura del file !!!"
Else
Do
ReadFile f, buffer(1), BYTE_PER_MSG, read, ByVal 0&
'Ora richiamo la Sub che invia i dati
SendMsg read, buffer
Loop Until read < BYTE_PER_MSG
SendMsg 0, buffer
CloseHandle f
End If
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
Private Sub MSComm1_OnComm()
If MSComm1.CommEvent = 2 Then
CommonDialog1.ShowSave
Dim hfile As Long
hfile = CreateFile(CommonDialog1.FileName, GENERIC_WRITE, FILE_SHARE_READ, ByVal 0&, CREATE_ALWAYS, 0, 0)
Dim tmp(4) As Byte
MSComm1.InputLen = 4
MSComm.InputMode = comInputModeBinary
tmp = MSComm1.Input
Dim num As Long
num = Int(tmp(1)) * &H1000000
num = num + Int(tmp(2)) * &H10000
num = num + Int(tmp(3)) * &H100
num = num + Int(tmp(4))
If num <> 0 Then
MSComm1.InputLen = num
If MSComm1.InputCounter < num Then
Do
While MSComm1.InBufferCount >= num
Dim buf() As Byte
'quindi ora siamo sicuri che ci siano i byte da leggere
buf = MSComm1.Input 'ed hai letto il buffer
WriteFile f, buf(1), num, read, ByVal 0&
Else
CloseHandle f
End If
End If
End Sub