Chitresh Suman

Chitresh Suman

  • NA
  • 275
  • 935

ESD (Electronic signature device) communication issue using socket

Dec 10 2020 1:06 AM
device not responding i.e I am unable to receive any response
  1. public void GetStatus()  
  2. {  
  3. try  
  4. {  
  5. string server = "*********";  
  6. string message;  
  7. Int32 port = ****;  
  8. TcpClient client = new TcpClient(server, port);  
  9. if (client.Connected)  
  10. {  
  11. ushort Head1 = 0x1a;  
  12. ushort Head2 = 0x5d;  
  13. ushort Command = 0x01;  
  14. ushort l = 0x00;  
  15. ushort l2 = 0x00;  
  16. ushort l3 = 0x00;  
  17. ushort l4 = 0x5f;  
  18. //byte[] vOut = BitConverter.GetBytes(l);  
  19. //byte[] vOut1 = BitConverter.GetBytes(l2);  
  20. //byte[] vOut2 = BitConverter.GetBytes(l3);  
  21. //byte[] vOut3 = BitConverter.GetBytes(l4);  
  22. //if (BitConverter.IsLittleEndian) Array.Reverse(vOut);  
  23. //var lvout = ToHex(vOut);  
  24. //if (BitConverter.IsLittleEndian) Array.Reverse(vOut1);  
  25. //var lvout1 = ToHex(vOut1);  
  26. //if (BitConverter.IsLittleEndian) Array.Reverse(vOut2);  
  27. //var lvout2 = ToHex(vOut2);  
  28. //if (BitConverter.IsLittleEndian) Array.Reverse(vOut3);  
  29. //var lvout3 = ToHex(vOut3);  
  30. string len = l.ToString() + l2.ToString() + l3.ToString() + l4.ToString();  
  31. StringBuilder str3 = new StringBuilder();  
  32. foreach (ushort item in table)  
  33. {  
  34. str3.Append(item);  
  35. }  
  36. // message = str3.ToString();  
  37. message = Head1.ToString() + Head2.ToString() + Command.ToString() + len + str3.ToString();  
  38. Byte[] crcbytes = Encoding.ASCII.GetBytes(message);  
  39. //var teststr = ToHex(crcbytes);  
  40. var calcCRC = ToHex(GetCrc(crcbytes));  
  41. ushort crc = 0x1a;  
  42. ushort crc1 = 0x0aa;  
  43. message = message + crc + crc1;  
  44. Byte[] data = Encoding.ASCII.GetBytes(ConvertToHex(message));  
  45. var final = ConvertToHex(message);  
  46. NetworkStream stream = client.GetStream();  
  47. // Send the message to the connected TcpServer.  
  48. stream.Write(data, 0, data.Length);  
  49. Console.WriteLine("Sent: {0}", message);  
  50. // Receive the TcpServer.response.  
  51. if (stream.CanRead)  
  52. {  
  53. byte[] myReadBuffer = new byte[1024];  
  54. StringBuilder myCompleteMessage = new StringBuilder();  
  55. int numberOfBytesRead = 0;  
  56. // Incoming message may be larger than the buffer size.  
  57. do  
  58. {  
  59. numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);  
  60. myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));  
  61. }  
  62. while (stream.DataAvailable);  
  63. // Print out the received message to the console.  
  64. Console.WriteLine("You received the following message : " +  
  65. myCompleteMessage);  
  66. }  
  67. else  
  68. {  
  69. Console.WriteLine("Sorry. You cannot read from this NetworkStream.");  
  70. }  
  71. stream.Close();  
  72. client.Close();  
  73. }  
  74. }  
  75. catch (Exception)  
  76. {  
  77. throw;  
  78. }  
  79. }  

Answers (1)