By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

Create a pause in stream.read?

P: 7
I have a device in the field that I access by the stream method.
I open the Stream and do the following:
1. stream.write a character to the device.
2. stream.read the information from the device.

The problem is that when I am debugging and step through the code there is a long enough pause that I recieve the entire string. If I just run it, I only get the 1st character. I have attached the code below.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2. Inherits System.Windows.Forms.Form
  3.  
  4. 'Setup up a new socket
  5. Public tClient As New System.Net.Sockets.TcpClient()
  6.  
  7. ' Get a client stream for reading and writing.
  8. Public stream As NetworkStream
  9. Public str_Output_message As String
  10. Public IP_Address As String = xxx.xxx.xxx.xxx
  11. Public Port_Number As Integer = xxxxx
  12.  
  13. Public Sub i_Comm_Initilization()
  14.  
  15. tClient.ReceiveBufferSize = (256)
  16. tClient.ReceiveTimeout = 3000
  17. tClient.SendTimeout = 1000
  18. 'Create a TcpClient. 
  19. tClient.Connect(IP_Address, Port_Number)
  20.  
  21. ' Get a client stream for reading and writing.
  22. stream = tClient.GetStream()
  23.  
  24. 'get the information from the device
  25. i_Read_Device_xmitB()
  26.  
  27. 'Delete the recordstore and close the connection to the field device
  28. i_delete_recordstore()
  29. i_Close_Connection()
  30.  
  31. End Sub
  32.  
  33. Public Sub i_Read_Device_xmitB()
  34.  
  35. 'Set the Output message
  36. str_Output_message = "b"
  37.  
  38. Dim byt_receive_buffer(tClient.ReceiveBufferSize) As Byte
  39.  
  40. ' Translate the message to be transmitted into ASCII and store it as a Byte array.
  41. byt_array_data = Encoding.ASCII.GetBytes(str_Output_message)
  42.  
  43. str_returndata = ""
  44.  
  45. If stream.CanWrite And stream.CanRead Then
  46.  
  47. ' Do a simple write.
  48. ' Send the message to the connected TcpServer. 
  49. stream.Write(byt_array_data, 0, byt_array_data.Length)
  50.  
  51. stream.Read(bytes, 0, CInt(tClient.ReceiveBufferSize))
  52.  
  53. ' Output the data received from the host to the console.
  54. str_returndata = Encoding.ASCII.GetString(bytes)
  55.  
  56. stream.Write(byt_array_data, 0, byt_array_data.Length)
  57.  
  58. 'Call the Data Breakout Routine
  59. i_Data_Breakout()
  60.  
  61. End Sub
Feb 12 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 5K+
P: 8,434
I'd suggest a quick search of TheScripts (see search box at top right) for the Sleep API call. It has been discussed at least once or twice, and allows you to wait for a specified number of milliseconds without tying up the cpu.
Feb 13 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.