Allow sending files with control bytes

This commit is contained in:
Livio Merola
2026-07-24 10:13:17 +02:00
parent 0167a9f335
commit 8da94c535e
6 changed files with 18 additions and 6 deletions
+15 -3
View File
@@ -681,7 +681,7 @@ Public Class Form1
Dim fileBytes = Await File.ReadAllBytesAsync(txtFileInvio.Text, token)
fileBytes = RemoveUtf8BomIfPresent(fileBytes)
LogFirstBytes(fileBytes)
ValidateCncTextBytes(fileBytes)
LogNonStandardCncBytes(fileBytes)
Dim blockPaced = chkInvioBlocchi.Checked
Dim terminator = If(blockPaced, Encoding.ASCII.GetBytes("%" & vbCrLf), GetTerminatorBytes())
@@ -1036,15 +1036,27 @@ Public Class Form1
End If
End Sub
Private Shared Sub ValidateCncTextBytes(data As Byte())
Private Sub LogNonStandardCncBytes(data As Byte())
Dim firstInvalidPosition As Integer = -1
Dim firstInvalidValue As Byte = 0
Dim invalidCount As Integer
For i = 0 To data.Length - 1
Dim value = data(i)
Dim isAllowedControl = value = &H9 OrElse value = &HA OrElse value = &HD
Dim isPrintableAscii = value >= &H20 AndAlso value <= &H7E
If Not isAllowedControl AndAlso Not isPrintableAscii Then
Throw New InvalidDataException($"Il file non sembra testo CNC ASCII/7 bit: byte non valido 0x{value:X2} alla posizione {i}.")
invalidCount += 1
If firstInvalidPosition < 0 Then
firstInvalidPosition = i
firstInvalidValue = value
End If
End If
Next
If invalidCount > 0 Then
Log($"Avviso: trovati {invalidCount} byte di controllo/non ASCII. Primo valore 0x{firstInvalidValue:X2} alla posizione {firstInvalidPosition}. Invio comunque.")
End If
End Sub
Private Sub SetBusy(isBusy As Boolean)