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
Re: VB.net text file STX, ETX
Code:
Dim buff() As Byte = New Byte() {2, 3} 'STX, ETX
SerialPort1.Write(buff, 0, buff.Length)
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 ...:)
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
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
Re: VB.net text file STX, ETX
Quote:
Originally Posted by
DickGrier
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.