hrastro pon 4.2.2019 10:11

Imam problem kako napisati phraser u Visual Basicu. Zbilja sam tutlek glede programiranja i sve mi se svodi  na traženje primjera na webu i prilagođavanje. 

 

Problem;

Šaljem drugom uređaju podatak putem TCP, 1B021D03003132331B03.

Kada taj isti podatak šaljm s npr Herculesom,  obavezno moram staviti kvačicu na HEX (vjerojatno da šalje čisti RAW podatak?). Gornji podatak sadržava zaglavlje, kontrolne znakove + glavni podatak tj. 123 (podatak je podcrtan tj 1 heksidecimalno je 31, 2 hex 32, 3 hex je 33.. tablicu sam stavio u privitak).

 

U VB  taj podatak formatiram na slijedeći način,

 

Dim sendBytes As [Byte]() = {&H1B, &H02, &H1D, &H03, &H00, &H31, &H32, &H33, &H1B, &H3} 

 

To funkcionira. 

 

Međutim, 123 nije fiksni podatak nego će biti troznamenkasti podatak koji upisuje korisnik putem forme. 

 

Dim podaci As String
podaci = podatak.Text

 

Ne znam kako napisati phraser koji će "podaci" uglaviti umjesto podatka &H31, &H32, &H33,?

 

Cijeli program; 

 

Imports System.Net.Sockets
Imports System.Text

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sadrzaj As String
sadrzaj = podatak.Text

Dim ipAddress As String
ipAddress = IpAdresa.Text
Dim podaci As String
podaci = podatak.Text
Dim port As Integer
port = BrPorta.Text
WriteData(podaci, ipAddress)
End Sub


Public Sub WriteData(ByVal data As String, ByRef IP As String)
Try
Console.WriteLine("Saljem """ & data & """ to " & IP)
Dim client As New TcpClient()
Dim port As Integer
port = BrPorta.Text
client.Connect(IP, port)

Dim stream As NetworkStream = client.GetStream()
Dim sendBytes As Byte() = Encoding.ASCII.GetBytes(data)
'Dim sendBytes As [Byte]() = {&H1B, &H2, &H1D, &H3, &H0, &H3, &H32, &H33, &H1B, &H3} 'OVO RADI

stream.Write(sendBytes, 0, sendBytes.Length)

stream.Close(5000) 'Wait 5 seconds to ensure that the data is sent.
client.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

 

 

 

 

hrastro pon 4.2.2019 11:44

Snašao sam se. 

 

Public Sub WriteData(ByVal data As String, ByRef IP As String)
Try
Console.WriteLine("Saljem """ & data & """ to " & IP)
Dim client As New TcpClient()
Dim port As Integer
port = BrPorta.Text
client.Connect(IP, port)

Dim stream As NetworkStream = client.GetStream()
'Dim sendBytes As Byte() = Encoding.ASCII.GetBytes(data)
'Dim sendBytes As [Byte]() = {&H1B, &H2, &H1D, &H3, &H0, &H3, &H32, &H33, &H1B, &H3} 'OVO RADI

Dim testString1 As String = data
Dim charArray() As Char = testString1.ToCharArray

Dim sendBytes As [Byte]() = {&H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0}
sendBytes(0) = &H1B
sendBytes(1) = &H2
sendBytes(2) = &H1D
sendBytes(3) = &H3
sendBytes(4) = &H0

sendBytes(5) = Microsoft.VisualBasic.AscW(charArray(0))
sendBytes(6) = Microsoft.VisualBasic.AscW(charArray(1))
sendBytes(7) = Microsoft.VisualBasic.AscW(charArray(2))

sendBytes(8) = &H1B
sendBytes(9) = &H3

stream.Write(sendBytes, 0, sendBytes.Length)

stream.Close(5000) 'Wait 5 seconds to ensure that the data is sent.
client.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub