locked
Visual Basic 2008 Express Edition - Serial Port Communication RRS feed

  • Question

  • Hi,

     

    I a newbie to Visual basic programming and currently using Visual basic 2008 Express Edition. I have an instrument which connects to PC by RS232 serial interface cable. This instrument has RS232 serial commands.

     

    I wish to write a VB program to write a serial port command to the instrument and then save the instrument serial port command reply to a text file.

     

    I have looked through the forums and internet search, but still not able to figure out. I greatly appreciate if someone can advise me sample code to do this?

     

    Thanks alot.

     

    Rgds,

    Helen78

     

     

    Tuesday, November 18, 2008 3:29 AM

Answers

  • For the information of anybody else, this is a copy of the reply I have just send to Helen by e-mail.

     

                --oOo--

     

    There are two ways to do it - the simple, but nasty one, and the more complex.
     
    Since your instrument is polled, you may forget the DataReceived event. What you need to do is to open the port, send a command and call a receive method with timeout to receive the answer. You can do this from the UI thread - the simple way - or from a background thread. If you do it from the UI thread, you will block the thread in the period from you send the command until you have received the reply. Therefore it is important that you set the ReadTimeout so that you don't risk to block the UI thread forever. If you cannot accept the blocking of the UI thread, you need to use multithreading. You can for example send the command from the UI thread and then start a backgroundworker to receive the reply (see the BackgroundWorker class). Events raised from a backgroundworker are raised on the UI thread so it should be a fairly simple matter to make an eventhandler, which empties the buffer when the backgroundworker is finished. I have never used the backgroundworker myself, so you can read the class description as well as I can. Instead of the backgroundworker, you can also make your own thread or get one from the thread pool.
     
    The most important thing when doing any kind og communication is to know the communication protocol (or define one, which can do the job). In your case, it is important to find out whether it is ASCII or binary and how the various telegrams are separated. Then you can decide whether a simple ReadLine command is enough or you need to build a receive loop.
     
    I suggest you start with the simple method to get hole through. Just take the code to open, change and close the port from the sample program of the tutorial. In case of a simple ASCII protocol, a Write command followed by a ReadLine command should then be enough to send the command and receive the answer. Once there is hole through, I suggest that you try to move the read method to another thread.
     
    Tuesday, November 18, 2008 7:01 AM

All replies

  • Check out ( LakeView Reseach )=> RS-232 and RS-485

    Tuesday, November 18, 2008 3:39 AM
  • For the information of anybody else, this is a copy of the reply I have just send to Helen by e-mail.

     

                --oOo--

     

    There are two ways to do it - the simple, but nasty one, and the more complex.
     
    Since your instrument is polled, you may forget the DataReceived event. What you need to do is to open the port, send a command and call a receive method with timeout to receive the answer. You can do this from the UI thread - the simple way - or from a background thread. If you do it from the UI thread, you will block the thread in the period from you send the command until you have received the reply. Therefore it is important that you set the ReadTimeout so that you don't risk to block the UI thread forever. If you cannot accept the blocking of the UI thread, you need to use multithreading. You can for example send the command from the UI thread and then start a backgroundworker to receive the reply (see the BackgroundWorker class). Events raised from a backgroundworker are raised on the UI thread so it should be a fairly simple matter to make an eventhandler, which empties the buffer when the backgroundworker is finished. I have never used the backgroundworker myself, so you can read the class description as well as I can. Instead of the backgroundworker, you can also make your own thread or get one from the thread pool.
     
    The most important thing when doing any kind og communication is to know the communication protocol (or define one, which can do the job). In your case, it is important to find out whether it is ASCII or binary and how the various telegrams are separated. Then you can decide whether a simple ReadLine command is enough or you need to build a receive loop.
     
    I suggest you start with the simple method to get hole through. Just take the code to open, change and close the port from the sample program of the tutorial. In case of a simple ASCII protocol, a Write command followed by a ReadLine command should then be enough to send the command and receive the answer. Once there is hole through, I suggest that you try to move the read method to another thread.
     
    Tuesday, November 18, 2008 7:01 AM
  • I have a similar aplication, and I'm using the serialport.write comand. It works fine with bytes under 128; but I cant send any values over 128 because it sends 63 instead. When I read your reply, I figured its because I have to use a binary protocol. What do I have to change?
    Monday, January 12, 2009 11:18 PM
  • Hi,

    To send a single Byte of data you can use:         (I use Visual Basic 2005)

            Dim Data As Byte
            Data = &H9A            ' for example,
            SendByte(Data)
    .
    .
    .
    .
        Private Sub SendByte(ByVal ByteInput As Byte)        
            Dim OutByte As Byte()                                         
            OutByte = New Byte(0) {ByteInput}                          
            serialPort.Write(OutByte, 0, OutByte.Length)
        End Sub

    To send a series of bytes, simply change OutByte = New Byte(2) { &H01, &H02, &H03,}  ..... e.g. if you sends 3 bytes a time ......

    hope this can help.

    Ray.

    Sunday, October 4, 2009 2:42 AM
  • you must change the ParityReplace the default is 63 it work for me

     

    Saturday, September 4, 2010 10:41 AM
  • I have a similar aplication, and I'm using the serialport.write comand. It works fine with bytes under 128; but I cant send any values over 128 because it sends 63 instead. When I read your reply, I figured its because I have to use a binary protocol. What do I have to change?


    Set the encoding property of the serialport to encoding 28591.

     

    WHAT?  I just noticed this thread is old. 

     

    Anyone using a USB to serial port converter should upgrade to 2010 and .Net 4.0.  .Net 3.5 had many issues with USB serial ports that have been addressed in .Net 4.0.


    Subnet Calculator / Planner      Serial Port      Random
    Saturday, September 4, 2010 12:22 PM