serial port

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kwstas86
    New Member
    • May 2009
    • 2

    serial port

    Hello I have a project which i need to build an interface in vc++.Net to communicate with a serial port with a microcontroller .I am a little bit amateur in programming.I search the web but i haven't find sth for this in this language.Is there anyone who knows sth?I just need how to open a serial,establis h the serial,close serial,send data to serial and read from serial.
    Because I have find some projects with serial in other languages if someone know how to make a project where i have the program in vc++.NET and the program for serial in another language.

    Thank you...
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Have you used the SerialPort component supplied by Visual Studio?
    You can drag/drop that onto your form from the Toolbox and go from there pretty painlessly.
    Properties exist for port, baud etc.
    Events exist for DataReceived, ErrorReceived, PinChange

    Comment

    • kwstas86
      New Member
      • May 2009
      • 2

      #3
      Yes I have done this one.But the thing that want to do is everytime i will open the interface to connect direct to the serial without pressing any button.So for this one i have to write some code as public i guess.And then when i press one button i want to send one 8bit number to the serial.From the otherside i will have maybe some textboxes to watch some values from my circuit-like temperature.
      So i need some samples of code to open automatically the serial connection with the circuit and some examples for how to send and read data.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        i will open the interface to connect direct to the serial without pressing any button.
        This implies that you first need to load your saved settings from someplace. That's up to you: Config file, registry, whatever.

        Create a method for loading your settings. LoadSettings(); for example.
        Create a method for opening your serial port. StartSerial(); for example

        From the .Load event of the custom control you are making call the LoadSettings() method followed by the StartSerial() method.

        Code:
                private void TempSensor_Load(object sender, EventArgs e)
                {
                    LoadSettings();
                    StartSerial();
                }
        For testing I suggest you also add a Start button that calls the same methods. This way you can start/stop it at will and not just when the control is loaded.

        some examples for how to send and read data.
        Before we just give you all the code... do some reading and some experimenting. If you read up on the SerialPort component (MSDN Library for example) you will see there are methods for .Read() .ReadLine() .Write() and .WriteLine()

        Comment

        • prasad599
          New Member
          • Feb 2009
          • 24

          #5
          There is one way to do it. Use the MSComm Control i.e Microsoft Communication control. Add this component from the list of com components.
          Right click the toolbox of the .NET IDE then click on Choose items.Then from com components add Microsoft Communications control.

          Drag and drop the control from toolBox

          The default name for the control comes as AxMscomm1

          Set the port number for the control either in design view or runtime.
          Then open the port using PortOpen property.
          Use the 'Output' property to send the data
          e.g Dim data(10) as byte

          AxMscomm1.outpu t = data


          Also create an event handler for the 'OnComm' event of the AxMscomm1
          This event is triggered when the data is received at the serial port.

          the code for the event should look like this:
          Variable receivedData is declared as string.

          Private Sub mscomm_OnComm(B yVal sender As Object, ByVal e As System.EventArg s) Handles AxMscomm1.OnCom m

          Dim commConst As MSCommLib.OnCom mConstants

          Select Case AxMscomm1.CommE vent
          Case commConst.comEv Receive
          receivedData = receivedData & AxMscomm1.Input
          End Select
          End Sub


          When you send some data to the connected hardware , you can check the response in a timer , by checking the receiveddata contents.

          Some of the properties of the control that needs to be changed in the design time.

          Right click on the control and then click on properties then click on Buffers
          Set RThreshold =1 and SThreshold =1

          Well the above code is in VB.NET

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            If you are using anything newer then .NET1.1 you should NOT use the mscomm dll, but instead use the SerialPort class built into .NET

            Comment

            Working...