Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interfacing PIC16F877A with VB to display ADC Readings

Status
Not open for further replies.

noel_t

Member
In fact, I am working on a project ( Data Acquisition system for Solar Panel) which collects all the data (from the sensors e.g. light, temperature, voltage, current) and after processing by Microcontroller (PIC Microcontroller) will display them on the LCD. It is also required to show/display these Four (4) parameters in the computer screen.
Therefore, as per need of the project, I selected Visual Basic to do it for me.
I have already completed the interfacing part (All the necessary codes-C Language for transmitting data from PIC to the UART-USB), and it is successfully transmitting data to the computer. (Tried by LABVIEW)

What I need is to configure my VB file to receive the data from USB and show it on the specified textbox( which is designed by VB).

Since I have not done the VB design and my friend helped me to do it, I am having some difficulties by working with it. I am getting errors and the measured values which supposed to be displayed in the defined text box can not be showed.

Please help me to eliminate the errors and give me your advice on how I can establish the communication between the VB and Microcontroller.

Here are the errors I am getting.
--------------------------------------------------------------------
I get this error “Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: startIndex”
' Text display routine, which appends the received string to any text in the Received TextBox.
Private Sub Display()
Temperature.Text = New String(stringtoparse, 1, 5)
Light.Text = New String(stringtoparse, 6, 5)
Volt.Text = New String(stringtoparse, 11, 5)
Current.Text = New String(stringtoparse, 16, 5)
------------------------------------------------------------------------
 

Attachments

  • VB.zip
    375.8 KB · Views: 245
Navid.... Are you still having problems... When do you receive this error, After a received data interrupt or when you try to compile? I run it on ISIS, but as the connection is clean, ie internal coms, I receive no problems.. You may need a try / catch for the serial data.

Index non zero means that the calling function has returned -1 (fault)... which one generates the error!! Temperature? or Light? or one of the others.
 
As you said,
if the Temp = 123 and the Light = 456 and volts = 156 and Current = 45

send "T0123L0456V0156C0045" with a following 0x10 & 0x13.

Its set up for FOUR digits so pad with zero's

I can't send these values from my microcontroller to the VB since it is Hi-Tech compiler and has its own syntax and codes. I am using putch command to send data and it is sent successfully, i checked it out with LABVIEW and it is working.
I really can't get your meaning that why i shall modify that part.
As you say stringtoparse contains the readings, so why later on no process is done on that to take the readings and put it into the proper textbox in VB.
Instead of that i can see you introduced Newstring as:
Temperature.Text = New String(stringtoparse, 1, 5)
Light.Text = New String(stringtoparse, 6, 5)
Volt.Text = New String(stringtoparse, 11, 5)
Current.Text = New String(stringtoparse, 16, 5)

By the way, now the error is eliminated but still nothing appears on the textbox. I don't know what happens to the data coming from PIC.
Navid.... Are you still having problems... When do you receive this error, After a received data interrupt or when you try to compile? I run it on ISIS, but as the connection is clean, ie internal coms, I receive no problems.. You may need a try / catch for the serial data.

Index non zero means that the calling function has returned -1 (fault)... which one generates the error!! Temperature? or Light? or one of the others.
 
Should i install MaxiCOM.vshost in order to run/debug the MaxiCOM application for display of mentioned data?
 
error

This is the error i get after Running/Compiling the program in VB2010.
 

Attachments

  • VB error.jpg
    VB error.jpg
    95.3 KB · Views: 210
After changing

After changing
'stringtoparse = COMPort.ReadLine() to stringtoparse = COMPort.ReadExisting()

and

' Text display routine, which appends the received string to any text in the Received TextBox.

Private Sub Display()
Temperature.Text = New String(stringtoparse)
Light.Text = New String(stringtoparse)
Volt.Text = New String(stringtoparse)
Current.Text = New String(stringtoparse)
End Sub

You can see within the snap the important codes and changes
 

Attachments

  • VB2.jpg
    VB2.jpg
    114.5 KB · Views: 259
Ok... COMPort.ReadLine() fetches a complete line of text that should be terminated by CrLf (0x13,0x10) the line

stringtoparse = COMPort.ReadLine()

Copies the input buffer into the string variable "stringtoparse" as you can now "parse" process the input string

the lines... Light.Text = New String(stringtoparse,6,5)

Creates a NEW string from "stringtoparse" from position "6" and is 5 characters long (hopefully this is where you put "Light" when sending from the pic.


Can I see your pic code?
 
I already changed it to Light.Text = New String(stringtoparse,6,5)---------->but still the same problem.
Please check your email to find my pic code.

the lines... Light.Text = New String(stringtoparse,6,5)

Creates a NEW string from "stringtoparse" from position "6" and is 5 characters long (hopefully this is where you put "Light" when sending from the pic.


Can I see your pic code?[/QUOTE]


Ok... COMPort.ReadLine() fetches a complete line of text that should be terminated by CrLf (0x13,0x10) the line

stringtoparse = COMPort.ReadLine()

Copies the input buffer into the string variable "stringtoparse" as you can now "parse" process the input string
 
I already changed it to Light.Text = New String(stringtoparse,6,5)---------->but still the same problem.
Please check your email to find my pic code.
 
I get this error the time want to compile it use MPLAB. Error [1098] ../../common/sprintf.c; 14. conflicting declarations for variable "_sprintf"
 
I get this error the time i want to compile it using MPLAB.

Error [1098] ../../common/sprintf.c; 14. conflicting declarations for variable "_sprintf"
 
Thanks Ian, i have already eliminated the error for compiling the C file using MPLAB, and programmed my PIC again successfully.
However still i am getting nothing using stringtoparse=COMPort.Readline()
and when i am using ReadExisting instead of that, unknown codes are displayed.

I think there is small hint that i am not observing that gives me this error. should i change anything at VB file?Or only i run using VB 2010 every thing should be displayed?

By the way, i did my c code as this:
sprintf(serial_buffer,"T%05dV%05dC%05dL%05d%c%c",light/10,temperature,voltage,current/10,0x10,0x13);

i just changed the orders of light,temperature,voltage,current. as the one you sent to me was different.

I don't know why when i use Readline() in VB2010 nothing appears in the textbox:(((
 

Attachments

  • VB3.jpg
    VB3.jpg
    287.1 KB · Views: 208
  • VB4.jpg
    VB4.jpg
    351.8 KB · Views: 197
  • VB5.jpg
    VB5.jpg
    288.4 KB · Views: 220
Put a breakpoint in VB at the readline() function and check the string that's being sent from the pic.

I know the code works as I have it running here.... I suspect there is a transmission issue... Using the mouse, run the cursor over ANY stringtoparse word and the tool tip text should reveal the contents.... IF!!! the breakpoint is never met... Then there is a transmission issue...Baud, Bits, wiring etc...
 
I put a breakpoint in VB at the readline, but still nothing appeared in the textbox.
I sent you one more time my pic code to be double checked by you. By the way since my baudrate is 9600 at pic side. i also changed it to 9600. according to :
/* Set baud rate. */
SPBRG = 129; //9600 at 20Mhz
// SPBRG = 129; //9600 at 20MHz SPBRG = (Fosc / (16 x Baud rate)) - 1, BRGH = 1 High Speed
 
The point was, did the code stop at the breakpoint? I suspect that there isn't a correct string being received...

One mistake I found (my fault sorry) " 0x10 and 0x13 " WRONG!!!! either 10 and 13 or 0xA and 0xD.... I'm going mad.

I have... VB2010, running the interface....ISIS, running the pic program... com0com, interfacing the two. Its running as expected.
 
The point was, did the code stop at the breakpoint? I suspect that there isn't a correct string being received...
Answer)No the code did not stopped.

One mistake I found (my fault sorry) " 0x10 and 0x13 " WRONG!!!! either 10 and 13 or 0xA and 0xD.... I'm going mad.
Answer) After correction it worked almost, Please see the snap i took just now after correction in the pic programme. It was so effective, because now at least something is displayed. but the values are not displayed correctly. e.g. the first digit of light is not shown.(but the data is coming from pic, because by changing the sensor value it also changes).
If you think comparing the real values from LCD by the values shown in VB can help let me know i will send you the snap of real values as well.
By the way i changed the order of followings since it was not showing in the correct box in VB2010.
' Text display routine, which appends the received string to any text in the Received TextBox.(I hope it is not affecting the objective)

Private Sub Display()

Light.Text = New String(stringtoparse, 6, 5)
Temperature.Text = New String(stringtoparse, 11, 5)
Volt.Text = New String(stringtoparse, 16, 5)
Current.Text = New String(stringtoparse, 1, 5)
End Sub
 

Attachments

  • VB6.jpg
    VB6.jpg
    225.8 KB · Views: 205
Yeaaaaa, The light is now OK!

I am just playing with these values to get something correct.

Light.Text = New String(stringtoparse, 5, 5)
Temperature.Text = New String(stringtoparse, 11, 5)
Volt.Text = New String(stringtoparse, 15, 5)
Current.Text = New String(stringtoparse, 1, 5)

................still changing with VB2010. update you Ian. These things .......haha
 

Attachments

  • VB7.jpg
    VB7.jpg
    212.6 KB · Views: 196
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top