// Example: Server usage // Uncomment to run server
Console.WriteLine("=== Serial File Transfer Server Example ===\n");
using var server = new SerialFileTransferServer.SerialFileTransferServer("COM4", "C:\SerialRoot", 115200);
server.LogMessage += (sender, e) => { Console.WriteLine($"[{e.Timestamp:HH:mm:ss}] {e.Message}"); };
server.FileReceived += (sender, e) => { Console.WriteLine($"[Received] {e.FilePath} ({e.FileSize} bytes)"); };
server.FileSent += (sender, e) => { Console.WriteLine($"[Sent] {e.FilePath} ({e.FileSize} bytes)"); };
server.ErrorOccurred += (sender, e) => { Console.WriteLine($"[Error] {e.GetException().Message}"); };
server.FileSent += (sender, e) => { Console.WriteLine($"[Sent] {e.FilePath} ({e.FileSize} bytes)"); };
server.FileReceived += (sender, e) => { Console.WriteLine($"[Received] {e.FilePath} ({e.FileSize} bytes)"); };
// Example: Client usage Console.WriteLine("=== Serial File Transfer Client Example ===\n");
using var client = new SerialFileTransferClient.SerialFileTransferClient("COM3", 115200);
// Subscribe to events client.TransferStart += (sender, e) => { Console.WriteLine($"[Start] {e.FileName}: {e.Direction} ({e.TotalBytes} bytes)"); };
client.TransferProgress += (sender, e) => { Console.WriteLine($"[Progress] {e.FileName}: {e.ProgressPercentage:F1}% ({e.BytesTransferred}/{e.TotalBytes} bytes)"); };
client.TransferCompleted += (sender, e) => { Console.WriteLine($"[Complete] {e.FileName}: {e.Direction} completed ({e.TotalBytes} bytes)"); };
client.FolderTransferStart += (sender, e) => { Console.WriteLine($"[Folder Start] {e.FolderPath}: {e.Direction} (Subfolders: {e.IncludeSubfolders})"); };
client.FolderTransferCompleted += (sender, e) => { Console.WriteLine($"[Folder Complete] {e.FolderPath}: {e.FilesTransferred} files, {e.FoldersCreated} folders, {e.TotalBytes} bytes"); };
client.LogMessage += (sender, e) => { Console.WriteLine($"[{e.Timestamp:HH:mm:ss}] {e.Message}"); };
client.ErrorOccurred += (sender, e) => { Console.WriteLine($"[Error] {e.GetException().Message}"); };
try { client.Open();
// Compare file before upload
Console.WriteLine("\n--- Comparing file for upload ---");
var uploadComparison = await client.CompareFileForUploadAsync("C:\\temp\\document.txt", "/test/document.txt");
Console.WriteLine($"Result: {uploadComparison.Result}");
Console.WriteLine($"Reason: {uploadComparison.Reason}");
Console.WriteLine($"Should transfer: {uploadComparison.ShouldTransfer}");
// Upload file with comparison
Console.WriteLine("\n--- Uploading file (skip if identical) ---");
await client.UploadFileAsync("C:\\temp\\document.txt", "/test/document.txt", skipIfIdentical: true);
// Compare file before download
Console.WriteLine("\n--- Comparing file for download ---");
var downloadComparison = await client.CompareFileForDownloadAsync("/test/document.txt", "C:\\temp\\downloaded.txt");
Console.WriteLine($"Result: {downloadComparison.Result}");
Console.WriteLine($"Reason: {downloadComparison.Reason}");
Console.WriteLine($"Should transfer: {downloadComparison.ShouldTransfer}");
// Download file with comparison
Console.WriteLine("\n--- Downloading file (skip if identical) ---");
await client.DownloadFileAsync("/test/document.txt", "C:\\temp\\downloaded.txt", skipIfIdentical: true);
// Upload folder with comparison (sync-like behavior)
Console.WriteLine("\n--- Uploading folder (with comparison) ---");
var uploadResult = await client.UploadFolderAsync(
"C:\\temp\\Projects",
"/backup/Projects",
includeSubfolders: true,
skipIfIdentical: true);
Console.WriteLine($"\nUpload Result:");
Console.WriteLine($" Files uploaded: {uploadResult.FilesUploaded}");
Console.WriteLine($" Files skipped: {uploadResult.FilesSkipped}");
Console.WriteLine($" Folders created: {uploadResult.FoldersCreated}");
Console.WriteLine($" Total bytes: {uploadResult.TotalBytesTransferred}");
Console.WriteLine($" Duration: {uploadResult.Duration.TotalSeconds:F2} seconds");
// Download folder with comparison
Console.WriteLine("\n--- Downloading folder (with comparison) ---");
var downloadResult = await client.DownloadFolderAsync(
"/backup/Projects",
"C:\\temp\\Downloaded\\Projects",
includeSubfolders: true,
skipIfIdentical: true);
Console.WriteLine($"\nDownload Result:");
Console.WriteLine($" Files downloaded: {downloadResult.FilesDownloaded}");
Console.WriteLine($" Files skipped: {downloadResult.FilesSkipped}");
Console.WriteLine($" Folders created: {downloadResult.FoldersCreated}");
Console.WriteLine($" Total bytes: {downloadResult.TotalBytesTransferred}");
Console.WriteLine($" Duration: {downloadResult.Duration.TotalSeconds:F2} seconds");
// Upload folder example
Console.WriteLine("\n--- Uploading folder ---");
var result = await client.UploadFolderAsync("C:\\Folder", "/remote");
Console.WriteLine($"Uploaded {result.FilesUploaded} files:");
foreach (var file in result.UploadedFiles)
{
Console.WriteLine($" {file.LocalPath} -> {file.RemotePath} ({file.FileSize} bytes) at {file.UploadedAt}");
}
Console.WriteLine("\n=== All operations completed ===");
} catch (Exception ex) { Console.WriteLine($"\nFatal error: {ex.Message}"); }