locked
Data Input from the Serial Port

    Question

  • Hello,

    Im not really great at programming and I need to know how to write code to read data from the serial port.  I have a microcontroller connected to the serial port and I need the C++ program to read the input and to perform a certain task according to the information.  Does anyone know what functions are needed to do this?
    Tuesday, July 15, 2008 2:00 PM

Answers

  • Hi HEDGE18,

    Could you clarify your scenario? Do you need to write the VB.NET or C++ code to access the serial port? If you will use the VB.NET language to read/write the serial port, you can take a look at the the serialport class about this issue. Read this good tutorial about serial COM port communication for your reference.

    Best regards,
    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, July 17, 2008 1:21 PM
    Moderator
  • Please post one new thread to discuss it. 
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, December 22, 2008 8:41 AM
    Moderator

All replies

  • Hi HEDGE18,

    Could you clarify your scenario? Do you need to write the VB.NET or C++ code to access the serial port? If you will use the VB.NET language to read/write the serial port, you can take a look at the the serialport class about this issue. Read this good tutorial about serial COM port communication for your reference.

    Best regards,
    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, July 17, 2008 1:21 PM
    Moderator
  • Hi Riquel,

    What i'm trying to ask for is similar to HEDGE18's question. For my school project, I'm doing a battery management system and what it mainly does is that the microcontroller reads voltages and currents from 3 battery packs. I am required to use vb.net to write a program to display those datas thru the rs232 interface, as well as able to turn the 3 batteries on or off individually using the program depending on the load's requirement.
    I'm relatively new at vb programming and all that i did was just the graphics part. i've tried looking all over searchengines for help, but most of the programs are done using vb6 and not vb.net. Is it possible to take multiple readings from the microcontroller through the rs232 interface using vb.net?

    Thanks for your help.
    Friday, December 19, 2008 9:12 AM
  • Please post one new thread to discuss it. 
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, December 22, 2008 8:41 AM
    Moderator
  • Hi Hedge and ProgSmart :),
                   As a mechatronics engineer., I can tell that you came to the right place. Let me just demonstrate few things you should know before building your project (that you'll do for sure):

    1. You need to determine the type of Microcontroller you shall use (PIC,ATMEL...etc). I personally use PIC.
    2. It is advisable to use Microcontroller who has onboard USTART (Universal Synchronous Asynchronous Receiver Transmittier) to make serial data transfer easy (although you can achieve that using soft USART, but Hardware USTART is the best choice). I used PIC 16F876 with PICBASIC Compiler (since we all talk here VB, then BASIC must be our native language).
    3. You may take a look at our Marvelous Beth Massi explanation to reading from serial port here:             Reading from Serial Port By Beth Massi
    4. Eventually, I can post you the Schematic for my temerparature module (I love doing temperature monitoring using PC with All ports like parallel, serial and USB, so call me temperature freak)    

    Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology
    Monday, December 22, 2008 9:12 AM
  • Hi Waleed,

    Thanks a million a lot for your help =). From what I know, I will be using a PIC 16 microcontroller, can't remember the model tho. The circuit design is already out but the PCB board is still under fabrication and so i can't test if the design actually works. I'm still stuck at the programming part, but the article by Beth Massi was quite helpful. Thanks for showing it to me.

    I have 3 questions and hope that you'll have the answers to them.
    1. When i use the command SerialPort1.Open() does it mean that I will definitely receive data from the microcontroller and be able to use the Serialport1_DataReceived function? Or will I receive data only after i send a command to prompt for a reading? 
    2. As I am expecting multiple readings, when I receive data, will i receive a whole chunk of text, or will i get the readings straight away? (Hope i don't have to learn how to use multithreading. Looks pretty complicated.)
    3. Since you are using PC to monitor the temperatures, do you use vb to create the graphics of your program?
    Monday, December 22, 2008 6:48 PM
  • Hi progSmart,
        Herewith are the answers to your questions:
    • Before opening the port, make sure that you have selected the physical port that your data acquisition card is connected to. You can retrieve serial ports available on your PC using this code:
           
    Private Sub Form1_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load 
            'Retrieve available serial port on PC 
            Dim Ports As String() = My.Computer.Ports.SerialPortNames.ToList.ToArray 
     
            'Adding all retrieved serial port to Combobox (named :cboSerialPoarts) 
            cboSerialPorts.DataSource = Ports 
    End Sub 

         Then, after selecting the port name and get it assigned to SerialPort1.PortName, you are now connected to serial port. If your controller sends continuously the data without requesting it, then you don't have to send anything. However, It is considered a bad practice.

         You may send a character "Like A" and in your Microcontroller code, this character could be the command to send your data. So, the bottom line is based on your PIC code, you'll be able to know what to do to retrieve your data.

    •     You should also note that ReceievedBytesThreshold is very important since DataReceived event won't trigger until the ReadBuffer has data more than or equal to The threshold you used.
    •     Based on your baudrate, you can determene how fast you retrieve your data. It is advised that you get the data first, then process it to retrieve a good chunk of it. That's why I recommend using commands to send to PIC and then PIC knows that it is time to send new data.
    •      I utilized a thirdparty component to plot my graph, it comes from Dundas and here is their website:  Dundas Company
        
        I hope this gives you an Idea. Again, I'll be more than happy to help you in this regard.

    Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology
    Tuesday, December 23, 2008 8:23 AM
  • Hi Waleed,

    Thanks for your help once again, I'm back with some new questions for you. Lets say the battery's original voltage is around 50V, I want to use a dialog box to warn when there is a over voltage, so how do i write the command using IF?

    something like this?
    Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            If Volt1 is __________ Then 
            Dim theForm As New Dialog1  
            theForm.ShowDialog()  
    End Sub 

    And I heard from my teacher that the microcontroller will be sending 1 byte of data to the serial port. this 1 byte of data will contain all the readings for the 3 batteries. I am going to assign the data that I'm collecting to those variables that I have made in the module. Can it be done? Or are there any other alternatives?
    Module Module1  
        Public Volt1 As Double 
        Public Curr1 As Double 
        Public Volt2 As Double 
        Public Curr2 As Double 
        Public Volt3 As Double 
        Public Curr3 As Double 
    End Module 

    Last question, how do i set the values to 2 decimal places?

    Thanks for your help. Sorry for the blue text. Somehow it turned blue and I have no idea know how to chage them back. lol.
    Tuesday, December 30, 2008 12:48 PM
  • dbasnett's serial port link

    the OP said "And I heard from my teacher that the microcontroller will be sending 1 byte of data to the serial port. this 1 byte of data will contain all the readings for the 3 batteries."


    8 bits = 1 byte
    3 batteries
    battery 1 = 3 bits
    battery 2 = 3 bits
    battery 3 = 2 bits

    or is it something else?


    Saturday, January 03, 2009 5:01 PM
  • OP never followed up per Riquel's request.  Subsequent replies are in response to attemped hyjacking by progidiot.  Riquel's response satisfies the OP's question, and hence this thread.

    Progidiot's own thread on the subject of the microcontroller can be found here:
    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e84b5f38-7883-4abe-87e2-516c6adc6f96
    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Sunday, January 04, 2009 11:05 PM
    Moderator
  • Hi ProgSmart and all valuable devs on this thread,
        I do agree that this thread is started by HEDGE18, however, Both Prog and HEDGE seem to have a common passion towards serial port interfacing (it is my humble opinion).

      dbasnett  has a very important inquiry in regard to "data format" (and has great article too). In my opinion ( and you should ask your teacher for further illustration), Each Battery voltage will be acquired via Analog Channel (PIC16877 has 8 analog channels multiplexed with 8 Digital I/O's). The A/D resolution is 10-bits but could be down to 8-bits. This means that you may get 3 bytes having three readings of batteries.

      Another thing to consider, the Max. Reference Voltage is 5V. How can you digitize 50V? This may require using Operational Amplifier ( to work as attenuator so that 50V will be attenuated to 5V). You may check taht with your instructor. By the way, how are you going to meaure the current?

     Rounding number might be achieved by the following:
    'Analog Voltage Value 
    Dim Voltage As Decimal = 3.41454 
     
    'Rounding to 2 Decimal points 
    Dim Y as Decimal=Decimal.Round(x, 3, MidpointRounding.AwayFromZero) 

    I hope this answers your questions Prog and did some help to HEDGE18

                   
    Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology
    Monday, January 05, 2009 1:18 PM
  • hello,
     
    well, i just thought that HEDGE18's question is almost identical to mine, so instead of making multiple threads about the same issue, why not just use it? since most of the forums in the internet discourages double posts, i just thought that here will be the same.

    calling it attempted hijacking sounds pretty serious thou. i don't know what damages i've made, but if i caused any inconveniences, i apologise.
    • Edited by progidiot Monday, January 05, 2009 2:09 PM
    Monday, January 05, 2009 1:56 PM
  • Hi Prog,
        I don't think HEDGE18 himself complained about that. We are all here to make use of each others knowledge. Reed were trying to focus on the original problem posted by HEDGE18. You, I and everyone is welcome here to post any relevant topics just like you mentioned as long as it is relevant.

    Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology
    Monday, January 05, 2009 2:00 PM
  • Hi Waleed,

    We are measuring the voltage and current, but have isolated the high voltage from the low voltage side. For the current, we added Hull effect current sensors to the circuit. It gives a voltage output with respect to the current it detects. With that, if you want to look at the current and voltage waveform of a circuit through a cathode ray oscilloscope, it is much easier.
     
    I think my program is working now. Thanks a lot for all the help that you've given me. This goes to Reed, jo0ls and dbasnet too.
    Monday, January 05, 2009 2:14 PM
  • I am new to VB2010. i am now require to get sample code by using NI-6009 DAQ Card to measure voltage(Vrms) and Curent (Irms).From that i can calculate power.But the problem i don't know how to create a new analog input task to start the program??? 
    Wednesday, April 25, 2012 6:37 AM
  • I am new to VB2010. i am now require to get sample code by using NI-6009 DAQ Card to measure voltage(Vrms) and Curent (Irms).From that i can calculate power.But the problem i don't know how to create a new analog input task to start the program??? 

    Hello Sara,

    Have you successfully created a task on  NI MAX for measuring v and I?

    Ni has daqmx APIs for calling your task. I promise to write you an by the weekend. Just please do post on new thread if you have a new question to keep the forum clean. Have a good day.


    Waleed El-Badry, Mechatronics Department, College of Engineering, Misr University for Science and Technology View my MCP Certifications

    Thursday, April 26, 2012 9:09 AM
  • Hi Waleed...thanks for answer

    Actually, i just modified an example program from VB example..from that example i try to measure Vrms and it is success.but the problem is i dont know how to measure Irms because the example from VB in another folder.So, how to combine between Vrms and Irms that i can calculate apparent power.plss help me as soon as possible...or you have another example code to measure Vrms and Irms with using NI-6009 DAQ Card in VB2010..

    Saturday, April 28, 2012 9:06 AM
  • Hi Waleed.....

    I am also need to display waveform for current, voltage and power..i have a file that want to attach for you. in this file contain a code program that i have modified and i want to show to you for help me..Honestly, i want you to help me...can i have your email??TQ

    Saturday, April 28, 2012 9:33 AM
  • Hi Waleed.....

    I am also need to display waveform for current, voltage and power..i have a file that want to attach for you. in this file contain a code program that i have modified and i want to show to you for help me..Honestly, i want you to help me...can i have your email??TQ

    Dear Amira

    Sure. My e-mail is wbadry@must.edu.eg


    Waleed El-Badry, Mechatronics Department, College of Engineering, Misr University for Science and Technology View my MCP Certifications


    Saturday, April 28, 2012 1:28 PM
  • Hi Waleed....

    I had sent the file to you..TQ

    Sunday, April 29, 2012 1:29 AM