Results 1 to 6 of 6

Thread: VB.net text file STX, ETX

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    21

    VB.net text file STX, ETX

    Hi friends, I am working on an application in which I have to send a data of a text file to a PLC machine. Now the convention is that
    send start of file (0x02)
    send data bytes (file data)
    send end of file (0x03)

    Now I dont have any idea that how to send the STX and ETX before and after the file data respectively. The PLC should receive them as 02 and 03 i.e. in their Hex values. If there is any ready function like vbCrLf for STX and ETX or any other way to achieve this then please reply . Thank you

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: VB.net text file STX, ETX

    Code:
            Dim buff() As Byte = New Byte() {2, 3} 'STX, ETX
    
            SerialPort1.Write(buff, 0, buff.Length)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    21

    Re: VB.net text file STX, ETX

    Thanks for ur reply.. but what I want is
    <STX> file data <ETX>
    Your following line :
    SerialPort1.Write(buff, 0, buff.Length)
    writes the entire buff array which is : Dim buff() As Byte = New Byte() {2, 3} 'STX, ETX on serial port. so what shud be the modification to send only STX first then file data and then ETX on the serial port??

    Thanks again
    Vishal ...

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: VB.net text file STX, ETX

    Something like this

    Code:
            'test file data
            Dim ipsum As String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    
            Dim bytesAsList As New List(Of Byte)
    
            bytesAsList.Add(2) 'stx
            bytesAsList.AddRange(System.Text.ASCIIEncoding.ASCII.GetBytes(ipsum)) 'the data
            bytesAsList.Add(3) 'etx
    
            Dim buff() As Byte = bytesAsList.ToArray 'as byte array
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: VB.net text file STX, ETX

    The straight forward way is,

    Code:
    Dim Packet(datalength + 2) As Byte
    Packet(0) = 2
    Packet(1) = firstdatabyte
    'continue adding data bytes through
    Packet(datalength) = lastdatabyte
    Packet(datalength + 1) = 3
    SerialPort.Write(Packet, Packet.Length)
    Generally, the data to the PLC might be binary, not ASCII text (though some PLCs do employ string commands). If your PLC is expecting ASCII data (text) commands, preceeded by STX and terminated by ETX, you can use dbasnett's suggestion. Simpler is just:

    Code:
    Dim myASCIIData As String
    myASCIIData = somestringcommand
    SerialPort.Write(Chr(2) & myASCIIData & Chr(3))
    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: VB.net text file STX, ETX

    Quote Originally Posted by DickGrier View Post
    The straight forward way is,

    Code:
    Dim Packet(datalength + 2) As Byte
    Packet(0) = 2
    Packet(1) = firstdatabyte
    'continue adding data bytes through
    Packet(datalength) = lastdatabyte
    Packet(datalength + 1) = 3
    SerialPort.Write(Packet, Packet.Length)
    Generally, the data to the PLC might be binary, not ASCII text (though some PLCs do employ string commands). If your PLC is expecting ASCII data (text) commands, preceeded by STX and terminated by ETX, you can use dbasnett's suggestion. Simpler is just:

    Code:
    Dim myASCIIData As String
    myASCIIData = somestringcommand
    SerialPort.Write(Chr(2) & myASCIIData & Chr(3))
    Dick
    Sorry for the confusion. I used the ascii data as an example of byte data.

    Code:
            Dim PRNG As New Random
    
            Dim randData(PRNG.Next(32, 65)) As Byte 'create a random sized buffer as a TEST
    
            PRNG.NextBytes(randData) 'fill the TEST buffer with TEST data
    
            Dim someData As New List(Of Byte) 'construct output buffer
    
            someData.Add(2) 'add stx
    
            someData.AddRange(randData) 'add the random data
    
            someData.Add(3) 'add etx
    
            Debug.WriteLine(randData.Length.ToString)
            randData = someData.ToArray 'convert list to array
            Debug.WriteLine(randData.Length.ToString)
    I think the method I would choose would depend on the format of the data, and if it were fixed length or not.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width