Shop OBEX P1 Docs P2 Docs Learn Events
Message for OBC — Parallax Forums

Message for OBC

Dr_AculaDr_Acula Posts: 5,484
edited 2012-08-18 21:32 in General Discussion
Message for OBC as too big to fit in the PM

This vb.net v 2008.

Declare this right at the beginning of the program

Imports System.IO.Ports
Imports Strings = Microsoft.VisualBasic ' so can use things like left( and right( for strings

and then this is the subroutine. Delay value of 50 seems about right.
    Sub XModemKyeSend(ByRef FName As String, ByVal Delay1 As Integer)
        Dim Filenamepath As String
        Dim a, j, i As Integer
        Dim k As Integer
        Dim LineOfText As String
        Dim SerialString As String
        Dim Counter As Integer
        Dim ErrorString As String
        Dim Packetnumber As Integer
        Dim Checksum As Long
        Dim OnesComplement As Byte
        Dim ByteRead As Byte
        Dim BinaryFileLength As Long
        Dim BinaryFileCounter As Long
        Dim Percent As Single
        Dim ErrorCounter As Byte ' 0 to 10
        Dim CPMFilename As String
        Dim Timeout As Boolean
        ProgressBar1.Maximum = 100
        ProgressBar1.Minimum = 0
        ' timer restart =true for sending via menu. False for auto send
        Filenamepath = FName
        Packetnumber = 1 ' 1 for the first packet (not zero)
        ' Try
        Dim Input As New FileStream(Filenamepath, FileMode.Open, FileAccess.Read)
        Dim br As New BinaryReader(Input)
        OpenSerialPort() ' open the serial port
        Label3.Text = "Error Count"
        Label2.Text = "0%"
        BinaryFileLength = br.BaseStream.Length() - 1
        'i = Len(Filepath) + 1 + 1  ' eg c:\n8vem and a \ = 9 and 1 more for start of name only
        Call Get_CPM_Filename(Filenamepath, CPMFilename)
        CPMFilename = Strings.UCase(CPMFilename)
        LineOfText = "XMODEM R " + CPMFilename + vbCr
        Call StringToPacket(LineOfText) ' 
        Call ClearInputBuffer()  ' clear the buffer into the PC
        For i = 1 To Delay1 ' 50 for kyedos need to wait for kyedos to run the xmodem program
            Sleep(100) ' wait for the xmodem text to come back so the next one is a 21
            Label3.Text = Str$(i)
            Call ClearInputBuffer()  ' clear the buffer into the PC
        Next i
        Call ClearInputBuffer()  ' clear the buffer into the PC
        Do
            SerialString = ""
            If SerialPortFound = True Then
                If SerialPortName.BytesToRead > 0 Then
                    SerialPortName.Read(InPacket, 0, 1) ' get one byte
                    SerialString = Chr(InPacket(0)) ' get a single character/string/byte
                End If
            End If
            If InPacket(0) = 21 Then
                ErrorString = "NAK"
                Exit Do ' got a ^U
            End If
            If SerialString <> "" Then
                'LineOfText = SerialString
                'Call TextLines(LineOfText) ' print it out unless it is a chr21
            End If
            Counter = Counter + 10
            Sleep(10) ' don't change this one - this is waiting for NAK
            Label1.Text = "Waiting for NAK to start" + Strings.Str(Counter) + "ms" ' display in milliseconds
            System.Windows.Forms.Application.DoEvents() ' update the label1 message
            ' add a counter and timeout if nothing
            If Counter > 5000 Then
                ErrorString = "Bypass wait for NAK - xmodem failed"
                Exit Do
            End If
            If ErrorString <> "" Then Exit Do
        Loop
        Label1.Text = ErrorString
        System.Windows.Forms.Application.DoEvents() ' update the label1 message
        Do
            ' act on each byte in the buffer array here
            XOut(0) = 1 'SOH ^A
            XOut(1) = Packetnumber
            OnesComplement = 255 - Packetnumber
            XOut(2) = OnesComplement
            Packetnumber = Packetnumber + 1
            If Packetnumber > 255 Then Packetnumber = Packetnumber - 256
            Checksum = 0
            For i = 0 To 127 ' get 128 bytes from file
                If BinaryFileCounter <= BinaryFileLength Then
                    ByteRead = br.ReadByte ' get a byte
                Else
                    ByteRead = 26 ' pad with char 26 = hex 1A because hyperterm does this ? why but it works
                End If
                XOut(i + 3) = ByteRead
                Checksum = Checksum + ByteRead
                BinaryFileCounter = BinaryFileCounter + 1
            Next
            ' create checksum
            Do
                If Checksum < 256 Then Exit Do ' work out checksum
                Checksum = Checksum - 256
            Loop
            XOut(131) = Checksum
            Call OutputXout()
            System.Windows.Forms.Application.DoEvents() ' update the label
            Timeout = False
            Do
                k = 0
                Do
                    If SerialPortFound = True Then
                        j = SerialPortName.BytesToRead
                    End If
                    If j >= 1 Then
                        If SerialPortFound = True Then
                            SerialPortName.Read(InPacket, 0, 1) ' get one byte
                        End If
                        Exit Do
                    End If
                    k += 1
                    If k > 5000 Then
                        Timeout = True ' 5 second delay for timeout, must be more than the Rx side timeout
                        Exit Do
                    End If
                    Sleep(1) ' 1ms delay
                Loop
                If InPacket(0) = 6 Then Exit Do ' remote got the packet so exit
                If InPacket(0) = 21 Then
                    ' remote didn't get it so try sending again - does up to 10x
                    ErrorCounter = ErrorCounter + 1
                    Call ClearInputBuffer() ' clear the input buffer to the PC
                    Sleep(1000) ' let the remote get itself sorted before resending the packet
                    Call OutputXout() ' resend the packet
                    Label3.Text = "Nak: " + Strings.Str(ErrorCounter)
                    System.Windows.Forms.Application.DoEvents() ' update the label
                End If
                If Timeout = True Then
                    Label3.Text = "Timeout waiting for Ack"
                    ErrorCounter = 20 'force an exit now
                    System.Windows.Forms.Application.DoEvents() ' update the label
                End If
                If ErrorCounter >= 10 Then Exit Do ' more than 10 errors
            Loop
            If ErrorCounter >= 10 Then
                Label1.Text = "10 or more errors - aborting"
                Label2.Text = "0%"
                OutPacket(0) = 24 ' send a ^X cancel
                If SerialPortFound = True Then
                    SerialPortName.Write(OutPacket, 0, 1) ' send 1 byte
                    OutPacket(0) = 3 ' send a ^C
                    SerialPortName.Write(OutPacket, 0, 1) ' send 1 byte
                End If
                Exit Do
            End If
            If BinaryFileCounter > BinaryFileLength Then Exit Do ' finish up
            Label1.Text = "Sent packet: " + Strings.Str(Packetnumber) + " = OK"
            Percent = BinaryFileCounter / BinaryFileLength
            Percent = Percent * 100
            Percent = Int(Percent)
            Label2.Text = Strings.Str(Percent) + "%"
            ProgressBar1.Value = Percent
        Loop
        If ErrorCounter < 10 Then
            OutPacket(0) = 4 ' finish byte
            If SerialPortFound = True Then
                SerialPortName.Write(OutPacket, 0, 1) ' send 1 byte
            End If
            Label2.Text = "100%"
            Label1.Text = "Finished"
            ProgressBar1.Value = 0 'done
            Call StringToPacket(vbCrLf) ' back to kyedos
        End If
        Input.Close()
        Sleep(200) ' wait for next file
        System.Windows.Forms.Application.DoEvents() ' update the labels
        'Catch ex As Exception
        '    Close()
        '    Label1.Text = Filenamepath + " not found"
        '    System.Windows.Forms.Application.DoEvents() ' update the labels
        '    Sleep(1000) ' so time to read message
        'End Try
        SerialPortName.Close() ' close the serial port
    End Sub




Would you be kind enough to share what you are using on the PC end for Xmodem file transfer to Kyedos? Sounds like you've got a snappy little VB program created to make things easy. (Still fighting with Hyperterm here.)


Thanks
Jeff

Comments

  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2012-08-18 20:50
    Could I talk you into emailing a binary? [email]jeffledger at gmail.com[/email]

    I'm having trouble getting this to compile with the tools I have.

    Thanks
    Jeff

    Dr_Acula wrote: »
    Message for OBC as too big to fit in the PM

    This vb.net v 2008.

    Declare this right at the beginning of the program

    Imports System.IO.Ports
    Imports Strings = Microsoft.VisualBasic ' so can use things like left( and right( for strings

    and then this is the subroutine. Delay value of 50 seems about right.
        Sub XModemKyeSend(ByRef FName As String, ByVal Delay1 As Integer)
            Dim Filenamepath As String
            Dim a, j, i As Integer
            Dim k As Integer
            Dim LineOfText As String
            Dim SerialString As String
            Dim Counter As Integer
            Dim ErrorString As String
            Dim Packetnumber As Integer
            Dim Checksum As Long
            Dim OnesComplement As Byte
            Dim ByteRead As Byte
            Dim BinaryFileLength As Long
            Dim BinaryFileCounter As Long
            Dim Percent As Single
            Dim ErrorCounter As Byte ' 0 to 10
            Dim CPMFilename As String
            Dim Timeout As Boolean
            ProgressBar1.Maximum = 100
            ProgressBar1.Minimum = 0
            ' timer restart =true for sending via menu. False for auto send
            Filenamepath = FName
            Packetnumber = 1 ' 1 for the first packet (not zero)
            ' Try
            Dim Input As New FileStream(Filenamepath, FileMode.Open, FileAccess.Read)
            Dim br As New BinaryReader(Input)
            OpenSerialPort() ' open the serial port
            Label3.Text = "Error Count"
            Label2.Text = "0%"
            BinaryFileLength = br.BaseStream.Length() - 1
            'i = Len(Filepath) + 1 + 1  ' eg c:\n8vem and a \ = 9 and 1 more for start of name only
            Call Get_CPM_Filename(Filenamepath, CPMFilename)
            CPMFilename = Strings.UCase(CPMFilename)
            LineOfText = "XMODEM R " + CPMFilename + vbCr
            Call StringToPacket(LineOfText) ' 
            Call ClearInputBuffer()  ' clear the buffer into the PC
            For i = 1 To Delay1 ' 50 for kyedos need to wait for kyedos to run the xmodem program
                Sleep(100) ' wait for the xmodem text to come back so the next one is a 21
                Label3.Text = Str$(i)
                Call ClearInputBuffer()  ' clear the buffer into the PC
            Next i
            Call ClearInputBuffer()  ' clear the buffer into the PC
            Do
                SerialString = ""
                If SerialPortFound = True Then
                    If SerialPortName.BytesToRead > 0 Then
                        SerialPortName.Read(InPacket, 0, 1) ' get one byte
                        SerialString = Chr(InPacket(0)) ' get a single character/string/byte
                    End If
                End If
                If InPacket(0) = 21 Then
                    ErrorString = "NAK"
                    Exit Do ' got a ^U
                End If
                If SerialString <> "" Then
                    'LineOfText = SerialString
                    'Call TextLines(LineOfText) ' print it out unless it is a chr21
                End If
                Counter = Counter + 10
                Sleep(10) ' don't change this one - this is waiting for NAK
                Label1.Text = "Waiting for NAK to start" + Strings.Str(Counter) + "ms" ' display in milliseconds
                System.Windows.Forms.Application.DoEvents() ' update the label1 message
                ' add a counter and timeout if nothing
                If Counter > 5000 Then
                    ErrorString = "Bypass wait for NAK - xmodem failed"
                    Exit Do
                End If
                If ErrorString <> "" Then Exit Do
            Loop
            Label1.Text = ErrorString
            System.Windows.Forms.Application.DoEvents() ' update the label1 message
            Do
                ' act on each byte in the buffer array here
                XOut(0) = 1 'SOH ^A
                XOut(1) = Packetnumber
                OnesComplement = 255 - Packetnumber
                XOut(2) = OnesComplement
                Packetnumber = Packetnumber + 1
                If Packetnumber > 255 Then Packetnumber = Packetnumber - 256
                Checksum = 0
                For i = 0 To 127 ' get 128 bytes from file
                    If BinaryFileCounter <= BinaryFileLength Then
                        ByteRead = br.ReadByte ' get a byte
                    Else
                        ByteRead = 26 ' pad with char 26 = hex 1A because hyperterm does this ? why but it works
                    End If
                    XOut(i + 3) = ByteRead
                    Checksum = Checksum + ByteRead
                    BinaryFileCounter = BinaryFileCounter + 1
                Next
                ' create checksum
                Do
                    If Checksum < 256 Then Exit Do ' work out checksum
                    Checksum = Checksum - 256
                Loop
                XOut(131) = Checksum
                Call OutputXout()
                System.Windows.Forms.Application.DoEvents() ' update the label
                Timeout = False
                Do
                    k = 0
                    Do
                        If SerialPortFound = True Then
                            j = SerialPortName.BytesToRead
                        End If
                        If j >= 1 Then
                            If SerialPortFound = True Then
                                SerialPortName.Read(InPacket, 0, 1) ' get one byte
                            End If
                            Exit Do
                        End If
                        k += 1
                        If k > 5000 Then
                            Timeout = True ' 5 second delay for timeout, must be more than the Rx side timeout
                            Exit Do
                        End If
                        Sleep(1) ' 1ms delay
                    Loop
                    If InPacket(0) = 6 Then Exit Do ' remote got the packet so exit
                    If InPacket(0) = 21 Then
                        ' remote didn't get it so try sending again - does up to 10x
                        ErrorCounter = ErrorCounter + 1
                        Call ClearInputBuffer() ' clear the input buffer to the PC
                        Sleep(1000) ' let the remote get itself sorted before resending the packet
                        Call OutputXout() ' resend the packet
                        Label3.Text = "Nak: " + Strings.Str(ErrorCounter)
                        System.Windows.Forms.Application.DoEvents() ' update the label
                    End If
                    If Timeout = True Then
                        Label3.Text = "Timeout waiting for Ack"
                        ErrorCounter = 20 'force an exit now
                        System.Windows.Forms.Application.DoEvents() ' update the label
                    End If
                    If ErrorCounter >= 10 Then Exit Do ' more than 10 errors
                Loop
                If ErrorCounter >= 10 Then
                    Label1.Text = "10 or more errors - aborting"
                    Label2.Text = "0%"
                    OutPacket(0) = 24 ' send a ^X cancel
                    If SerialPortFound = True Then
                        SerialPortName.Write(OutPacket, 0, 1) ' send 1 byte
                        OutPacket(0) = 3 ' send a ^C
                        SerialPortName.Write(OutPacket, 0, 1) ' send 1 byte
                    End If
                    Exit Do
                End If
                If BinaryFileCounter > BinaryFileLength Then Exit Do ' finish up
                Label1.Text = "Sent packet: " + Strings.Str(Packetnumber) + " = OK"
                Percent = BinaryFileCounter / BinaryFileLength
                Percent = Percent * 100
                Percent = Int(Percent)
                Label2.Text = Strings.Str(Percent) + "%"
                ProgressBar1.Value = Percent
            Loop
            If ErrorCounter < 10 Then
                OutPacket(0) = 4 ' finish byte
                If SerialPortFound = True Then
                    SerialPortName.Write(OutPacket, 0, 1) ' send 1 byte
                End If
                Label2.Text = "100%"
                Label1.Text = "Finished"
                ProgressBar1.Value = 0 'done
                Call StringToPacket(vbCrLf) ' back to kyedos
            End If
            Input.Close()
            Sleep(200) ' wait for next file
            System.Windows.Forms.Application.DoEvents() ' update the labels
            'Catch ex As Exception
            '    Close()
            '    Label1.Text = Filenamepath + " not found"
            '    System.Windows.Forms.Application.DoEvents() ' update the labels
            '    Sleep(1000) ' so time to read message
            'End Try
            SerialPortName.Close() ' close the serial port
        End Sub
    
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-08-18 21:26
    This is a zip of the entire program. It is a huge program - the IDE I wrote for catalina, and it contains other things like making movies for the propeller and the floyd steinberg dithering algorithm. But hidden away in all this is the kyedos download code.

    It may not compile with 2010 - I'm using 2008. But it should be possible to port things over.

    .net files are created in a directory with multiple sub directories but it should unzip with all that directory structure intact and hopefully the .sln file will open it.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2012-08-18 21:32
    Thanks!

    -Jeff
Sign In or Register to comment.