Results 1 to 7 of 7

Thread: [RESOLVED] keeping console visible with writing console output to text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    [RESOLVED] keeping console visible with writing console output to text file

    Hello,

    I have the following problem with which I would like some help,

    I have a program running which normally outputs to the console. I would now like to save this output to a text file. I have tried the following. I now have the text file created and the output written to it. However I no longer can see the console. Is it possible to make it visible or alternatively would it be possible to display the console output into a textbox (I have had no luck with that) while it is writing to the text file?

    Dim fs As New FileStream("C:\Test.txt", FileMode.Create) 'see comment
    Dim tmp As TextWriter = Console.Out 'see comment
    Dim sw As New StreamWriter(fs) 'see comment



    While (xlStatus <> XLClass.XLstatus.XL_ERR_QUEUE_IS_EMPTY)

    Console.SetOut(sw) 'see comment

    ' if receiveing succeed....
    If (xlStatus = XLClass.XLstatus.XL_SUCCESS) Then

    ' ...print rx data to console
    CANDemo_RxChannel.xlPrintRx(receivedEvent)
    Console.SetOut(tmp) 'see comment
    End If

    End While

    'Comment: These are the bit I’ve added to this section of the original program to have it write to text file but the console is no longer visible

    I would be grateful if I could get any help with this problem.

    thanks,
    Last edited by vb_enthusiast; May 15th, 2013 at 12:55 AM.

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: keeping console visible with writing console output to text file

    Can nobody really help me with this problem...

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: HELP: keeping console visible with writing console output to text file

    Frankly I'm sure I'm not the only one who is totally lost here. If you no longer output to console then there is no console to see, surely? You can't have two destinations for the same stream. But then I can't see why you would be sending to console in the first place? As I say, just baffled!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: HELP: keeping console visible with writing console output to text file

    Thank you for your reply. I am using a piece of kit that comes with its codes. "xlPrintRx" outputs to console as only visual output. In an attempt to save the data I added those lines. However I still would like to see what it being recorded real-time. Hence my question regarding whether or not it is possible to have the console window visible. Since this does not appear to be the case, it is possible to populate a textbox with the recieved data realtime?

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: HELP: keeping console visible with writing console output to text file

    Yes, it's possible. If you capture the data in a variable you can then pass that variable round to as many displays and files as you want. There may be some cross threading problems to solve but we can cross that bridge when and if we come to it.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: HELP: keeping console visible with writing console output to text file

    I've tried to store the output as a string but this causes an error "...cannot be converted to string...". What type of variable would you save the output?

    Thanks

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: [RESOLVED] keeping console visible with writing console output to text file

    I've been doing some research and found my solution. Thought it might be good for me to share it. You can find some more information at the following link:
    http://msdn.microsoft.com/en-us/libr...nsole.out.aspx

    Base on that, this I is what I did to my code:

    Public Shared Sub RX_Thread()

    ' object to be fill with received data
    Dim receivedEvent As XLClass.xl_event = New XLClass.xl_event

    ' result of XL driver func. calls
    Dim xlStatus As XLClass.XLstatus = XLClass.XLstatus.XL_SUCCESS

    ' WaitForSingleObject values
    Dim waitResult As WaitResults

    Dim fs As FileStream
    Dim sw As StreamWriter

    While (True)

    ' wait for hardware events

    waitResult = WaitForSingleObject(CANDemo_RxChannel.eventHandle, 1000)

    ' if event occured...
    If (waitResult <> WaitResults.WAIT_TIMEOUT) Then

    ' init xlStatus first
    xlStatus = XLClass.XLstatus.XL_SUCCESS

    ' while hw queue not empty
    While (xlStatus <> XLClass.XLstatus.XL_ERR_QUEUE_IS_EMPTY)

    ' ...receive data from hardware queue...
    xlStatus = CANDemo_RxChannel.xlReceive(receivedEvent)
    ' if receiveing succeed....
    If (xlStatus = XLClass.XLstatus.XL_SUCCESS) Then
    ' ...print rx data
    CANDemo_RxChannel.xlPrintRx(receivedEvent) 'output to console window

    fs = New FileStream("C:\Test.txt", FileMode.Append)
    sw = New StreamWriter(fs)
    sw.AutoFlush = True
    Console.SetOut(sw) ' redirect console output to file
    CANDemo_RxChannel.xlPrintRx(receivedEvent) 'Data saved into file

    ' Close previous output stream and redirect output to standard output.
    Console.Out.Close()
    sw = New StreamWriter(Console.OpenStandardOutput())
    sw.AutoFlush = True
    Console.SetOut(sw)
    End If

    End While

    End If
    ' no event occured
    End While
    End sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width