Create a pause in stream.read?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markclinn
    New Member
    • Feb 2007
    • 7

    #1

    Create a pause in stream.read?

    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.

    Code:
    Public Class Form1
    Inherits System.Windows.Forms.Form
    
    'Setup up a new socket
    Public tClient As New System.Net.Sockets.TcpClient()
    
    ' Get a client stream for reading and writing.
    Public stream As NetworkStream
    Public str_Output_message As String
    Public IP_Address As String = xxx.xxx.xxx.xxx
    Public Port_Number As Integer = xxxxx
    
    Public Sub i_Comm_Initilization()
    
    tClient.ReceiveBufferSize = (256)
    tClient.ReceiveTimeout = 3000
    tClient.SendTimeout = 1000
    'Create a TcpClient. 
    tClient.Connect(IP_Address, Port_Number)
    
    ' Get a client stream for reading and writing.
    stream = tClient.GetStream()
    
    'get the information from the device
    i_Read_Device_xmitB()
    
    'Delete the recordstore and close the connection to the field device
    i_delete_recordstore()
    i_Close_Connection()
    
    End Sub
    
    Public Sub i_Read_Device_xmitB()
    
    'Set the Output message
    str_Output_message = "b"
    
    Dim byt_receive_buffer(tClient.ReceiveBufferSize) As Byte
    
    ' Translate the message to be transmitted into ASCII and store it as a Byte array.
    byt_array_data = Encoding.ASCII.GetBytes(str_Output_message)
    
    str_returndata = ""
    
    If stream.CanWrite And stream.CanRead Then
    
    ' Do a simple write.
    ' Send the message to the connected TcpServer. 
    stream.Write(byt_array_data, 0, byt_array_data.Length)
    
    stream.Read(bytes, 0, CInt(tClient.ReceiveBufferSize))
    
    ' Output the data received from the host to the console.
    str_returndata = Encoding.ASCII.GetString(bytes)
    
    stream.Write(byt_array_data, 0, byt_array_data.Length)
    
    'Call the Data Breakout Routine
    i_Data_Breakout()
    
    End Sub
    Last edited by Killer42; Feb 13 '07, 02:05 AM. Reason: Please use CODE tags.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    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.

    Comment

    Working...