tx-oliveira Posted March 26, 2015 at 11:20 AM Report Share #580242 Posted March 26, 2015 at 11:20 AM Viva, Fiz um ciclo for, que vai do 1 ao 128 e queria fazer uma funçao quando era 2,3 ou 10 que fosse a uma funçao e converte-se (No caso do 2 (10 e 82), 3(10 e 83) e 10 (10 e 90)) Que funçao poderei fazer ? Alguem ja fez algo do genero ? Cumprimentos Link to comment Share on other sites More sharing options...
ribeiro55 Posted March 26, 2015 at 11:54 AM Report Share #580246 Posted March 26, 2015 at 11:54 AM Tens de te explicar um pouco melhor. Converter o que? Para o quê? Era 2, 3 ou 10, o quê? O último número? Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
tx-oliveira Posted March 26, 2015 at 12:11 PM Author Report Share #580247 Posted March 26, 2015 at 12:11 PM Faço um ciclo for ii = 1 to 128 Mas quando é 2,3 e 10 tem de saltar fora o 2, 3 e 10 e entrar o 10,82;10,83... Dim Dato As [string] Dato = "" Dim data As [byte]() = System.Text.Encoding.Default.GetBytes(Dato) Dim somma As Integer Dim comando As Integer Dim seg As Integer Dim Ii As Integer For Ii = 1 To 128 Ii = Ii somma = 4 + 8 + 61 + 10 + Ii comando = Ii If Ii = 2 Then comando = Ii = 16 & seg = 130 End If If Ii = 3 Then comando = Ii = 16 & seg = 131 End If If Ii = 10 Then comando = Ii = 16 & seg = 144 End If comando = comando & Chr(seg) Dato = Chr(2) & Chr(4) & Chr(8) & Chr(0) & Chr(0) & Chr(61) & Chr(10) & Chr(0) & comando & Chr(0) & Chr(somma) & Chr(3) Link to comment Share on other sites More sharing options...
ribeiro55 Posted March 26, 2015 at 12:52 PM Report Share #580250 Posted March 26, 2015 at 12:52 PM Continua críptico para mim 🙂 Para além disso, o teu código tem alguns problemas. Um deles, por exemplo, é o facto da variável seg nunca assumir nenhum valor a não ser o da inicialização (0 por omissão). Não estarás a querer atribuir valores, em vez do teste lógico? Tens para aí uma confusão de datatypes tremenda. O que queres fazer com "comando = Ii = 16" ? Assim como está, estás a dar True (1) à variável comando quando o Ii é igual a 16 e False (0) nos outros casos. Explica lá melhor o que queres dizer com "entra 10,82 ...." Deixa lá arriscar uma implementação, para termos algo onde pegar: Dim Dato As String = String.Empty Const C As String = Chr(2) & Chr(4) & Chr(8) & Chr(0) & Chr(0) & Chr(61) & Chr(10) & Chr(0) For i As Integer = 1 To 128 Dim soma As Integer = 4 + 8 + 61 + 10 + i Dim seg As Integer Dim comando As Integer = i Select Case i Case 2 : comando = 16 : seg = 130 Case 3 : comando = 16 : seg = 131 Case 10 : comando = 16 : seg = 144 End Select Dim tmp As String = comando.ToString() & Chr(seg) Dato &= C & tmp & Chr(0) & Chr(soma) & Chr(3) Next Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
tx-oliveira Posted March 26, 2015 at 01:10 PM Author Report Share #580251 Posted March 26, 2015 at 01:10 PM (edited) Eu estou a fazer uma programa de inicio entre VB-Socket. (central de antifurto) Agora tenho de fazer é enviar esse "dato" que é Dato = Chr(2) 'inicio & Chr(4)'cumprimento dos dados & Chr(8) 'flag & Chr(0) & Chr(0) 'connec & Dados: Chr(61) & Chr(10) & Chr(0) & comando & Chr(0) & Chr(somma) 'soma& Chr(3) ' fine Eu mando um comando neste caso 61-10-0-Carater 'Caracter pretendido (1 - ingresso 1; 2- ingresso 2 etc) , neste caso quero listar os caracteres de 1 a 128. Podia fazer o ciclo for de 1-128 e colocar a variavel do ciclo for no caracter, mas este socket tem uma regra especifica para o 2,3 e 10 que le o 10-82, 10-83 e 10-90 Se fizer 1 a 1 ele funciona. Try Dim Dato As [string] Dato = "" Dim data As [byte]() = System.Text.Encoding.Default.GetBytes(Dato) Dim somma As Integer Dim comando As Integer Dim Ii As Integer For Ii = 1 To 128 Ii = Ii somma = 4 + 8 + 61 + 10 + Ii comando = Ii If Ii = 2 Then comando = Chr(16) & Chr(130) End If If Ii = 3 Then comando = Chr(16) & Chr(131) End If If Ii = 10 Then comando = Chr(16) & Chr(144) End If ' comando = comando & Chr(seg) Dato = Chr(2) & Chr(4) & Chr(8) & Chr(0) & Chr(0) & Chr(61) & Chr(10) & Chr(0) & comando & Chr(0) & Chr(somma) & Chr(3) ' Translate the passed message into ASCII and store it as a Byte array. ' Get a client stream for reading and writing. Dim stream As NetworkStream = client.GetStream() 'Stream stream = client.GetStream(); ' Send the message to the connected TcpServer. stream.Write(data, 0, data.Length) ' Receive the TcpServer.response. ' Buffer to store the response bytes. data = New [byte](256) {} ' ' String to store the response ASCII representation. Dim responseData As [string] = [string].Empty ' ' Read the first batch of the TcpServer response bytes. Dim bytes As Int32 = stream.Read(data, 0, data.Length) responseData = System.Text.Encoding.Default.GetString(data, 0, bytes) ' Input Bytes. Dim array() As Byte = {data(6), data(7), data(8), data(9), data(10), data(11), data(12), data(13), data(14), data(15), data(16), data(17), data(18), data(19), data(20), data(21)} ' Use GetString. Dim value As String = System.Text.ASCIIEncoding.ASCII.GetString(array) 'Console.WriteLine(value) TextBox1.Text = value ListBox1.Items.Add(value) Next Catch exu As ArgumentNullException Console.WriteLine("ArgumentNullException: {0}", e) Catch exu As SocketException Console.WriteLine("SocketException: {0}", e) End Try Edited March 26, 2015 at 01:43 PM by apocsantos geshi Link to comment Share on other sites More sharing options...
ribeiro55 Posted March 26, 2015 at 05:38 PM Report Share #580270 Posted March 26, 2015 at 05:38 PM Desculpa. Não consigo perceber o teu problema. Mas se tens lá o ciclo, e tens lá as condições, qual é o problema? Um problema que vejo logo à cabeça é o facto de estares a concatenar dois caracteres para dentro de um Integer. Isso até devia estoirar. Usa outra variável, do tipo String, para guardares a concatenação, e um IIf para trocares a variável quando aplicável: somma = 4 + 8 + 61 + 10 + Ii comando = Ii Dim tmp As String = String.Empty If Ii = 2 Then tmp = Chr(16) & Chr(130) End If If Ii = 3 Then tmp = Chr(16) & Chr(131) End If If Ii = 10 Then tmp = Chr(16) & Chr(144) End If Dato = Chr(2) & Chr(4) & Chr(8) & Chr(0) & Chr(0) & Chr(61) & Chr(10) & Chr(0) & IIf(tmp <> String.Empty, tmp, comando) & Chr(0) & Chr(somma) & Chr(3) Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips" Link to comment Share on other sites More sharing options...
tx-oliveira Posted March 27, 2015 at 02:36 PM Author Report Share #580337 Posted March 27, 2015 at 02:36 PM Obrigado, ja consegui resolver o problema Dim Dato As [string] Dim i As Integer For i = 1 To 10 Dato = Chr(61) & Chr(10) & Chr(0) & Chr(i) ' Translate the passed message into ASCII and store it as a Byte array. Dim data As [byte]() = System.Text.Encoding.Default.GetBytes(ConvDato(4, Dato)) ' Get a client stream for reading and writing. Dim stream As NetworkStream = client.GetStream() 'Stream stream = client.GetStream(); ' Send the message to the connected TcpServer. stream.Write(data, 0, data.Length) ' Receive the TcpServer.response. ' Buffer to store the response bytes. data = New [byte](256) {} ' ' String to store the response ASCII representation. Dim responseData As [string] = [string].Empty ' ' Read the first batch of the TcpServer response bytes. Dim bytes As Int32 = stream.Read(data, 0, data.Length) responseData = System.Text.Encoding.Default.GetString(data, 0, bytes) ' Input Bytes. Dim array() As Byte = {data(6), data(7), data(8), data(9), data(10), data(11), data(12), data(13), data(14), data(15), data(16), data(17), data(18), data(19), data(20), data(21)} ' Use GetString. Dim value As String = System.Text.ASCIIEncoding.ASCII.GetString(array) ListBox1.Items.Add(value) If i = 1 Then lbL1.Text = value End If If i = 2 Then lbL2.Text = value End If If i = 5 Then lbdip5.Text = value End If If i = 6 Then lbdip6.Text = value End If If i = 7 Then lbdip7.Text = value End If If i = 8 Then lbdip8.Text = value End If If i = 9 Then lbdip9.Text = value End If If i = 10 Then lbdip10.Text = value End If Next a tal funçao: Public Function ConvDato(ByVal iLunghezza As Integer, ByVal stStringa As String) As String Dim stStringaFinale As String Dim iCheck As Integer Dim iI1 As Integer 'Dim iprova As Integer 'Dim stprova As String stStringaFinale = Chr(2) & Conv2310(Chr(iLunghezza)) & Chr(8) & Chr(0) & Chr(0) iCheck = iLunghezza + 8 For iI1 = 1 To iLunghezza stStringaFinale = stStringaFinale & Conv2310(Mid(stStringa, iI1, 1)) 'stprova = Mid(stStringa, iI1, 1) 'iprova = Asc(Mid(stStringa, iI1, 1)) iCheck = iCheck + Asc(Mid(stStringa, iI1, 1)) Next stStringaFinale = stStringaFinale & Chr(0) & Chr(iCheck) & Chr(3) ConvDato = stStringaFinale Return ConvDato End Function Public Function Conv2310(ByVal StCar As String) As String Conv2310 = StCar If Asc(StCar) = 2 Then Conv2310 = Chr(16) & Chr(130) ElseIf Asc(StCar) = 3 Then Conv2310 = Chr(16) & Chr(131) ElseIf Asc(StCar) = 16 Then Conv2310 = Chr(16) & Chr(144) End If End Function Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now