Anybody have some example code for sending a text file and receiving one, through the serial port.
Thanks
serial port
These are two prodedures to send and to receive any file type.
Of course, MVCOM is used.
Please try...
Of course, MVCOM is used.
Please try...
Code: Select all
Procedure ComReceiveFile(DirPath.s)
ChunkSize.l = 512
DataSize.l = 0
Stx.b = 2
Bot.b = 1
Eot.b = 4
Etx.b = 4
CarData.b = 0
FileName.s = ""
NbData.l = 0
Chaine.s = ""
Status.l = 0
Pointer.l = -1
FlagEnd.l = #False
If HCom > 0
*Buffer = AllocateMemory(ChunkSize)
While FlagEnd = #False
If ComInputBufferCount(HCom) > 0
ComRead(HCom,@CarData,1)
Select Status
Case 0 ;Nothing append
If CarData = Stx
Status = 1 ;Next data will be the filename
EndIf
Case 1 ;Receive the filename
If CarData = Bot
Status = 2 ;Next Data will be the length of data to receive
Else
Filename + Chr(Cardata)
EndIf
Case 2 ;Receive the data length
If CarData = Eot
NbData = Val(Chaine)
If NbData > 0 ;NextData will be the file data
Status = 3
Else
Status = 4
EndIf
Else
Chaine + Chr(CarData)
EndIf
Case 3 ;Receive File Data
If NbData > 0
Pointer + 1
PokeB(*Buffer + Pointer,CarData)
If Pointer = NbData - 1
Status = 4
EndIf
EndIf
Case 4 ;Receive the Etx
If CarData = Etx
If NbData > 0
CompletPath.s = DirPath + FileName
If OpenFile(1, CompletPath)
FileSeek(1,Lof(1))
WriteData(1,*Buffer,NbData)
CloseFile(1)
EndIf
Else
FlagEnd = #True
EndIf
EndIf
EndSelect
EndIf
Wend
FreeMemory(*Buffer)
EndIf
EndProcedure
Procedure ComSendFile(FileName.s)
DataSize.l = 0
Stx.b = 2
Bot.b = 1
Eot.b = 4
Etx.b = 4
SendOk.l = #False
ChunkSize.l = 512
If FileSize(FileName) > 0
If HCom > 0
If ReadFile(0,FileName) <> 0
*Buffer = AllocateMemory(ChunkSize)
DataSize = ChunkSize
While SendOk = #False
If DataSize = ChunkSize
If Lof(0)-Loc(0) < ChunkSize
DataSize = Lof(0)-Loc(0)
EndIf
Else
DataSize = 0
EndIf
If DataSize > 0
ReadData(0,*Buffer, DataSize)
Chaine.s = Chr(Stx) + GetFilePart(FileName) + Chr(Bot) + Str(DataSize) + Chr(Eot)
ComWrite(Hcom, @Chaine,Len(Chaine))
ComWrite(Hcom, *Buffer,DataSize)
Chaine = Chr(Etx)
ComWrite(Hcom, @Chaine, 1)
Else
Chaine.s = Chr(Stx) + GetFilePart(FileName) + Chr(Bot) + Str(DataSize) + Chr(Eot) + Chr(Etx)
ComWrite(Hcom, @Chaine,Len(Chaine))
SendOk = #True
EndIf
Wend
CloseFile(0)
FreeMemory(*Buffer)
EndIf
EndIf
EndIf
EndProcedure
Marc from PARIS - MVCOM Library