Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 1 month ago.
USBSerial send data to VB
Hallo, I am usig LPC11u24 Microcontroller, by using USBSerial communication need to send data to Visual Basic. And Visual Basic will show the data in richtextbox. I can see the output temperature in Putty, But in VB I can't see nothing . Can anyone tell me how to do it ......?
mbed code:
- include <fstream>
- include "mbed.h"
- include "USBSerial.h"
- include <math.h>
- include <Ticker.h>
DigitalOut myled(LED1); DigitalOut myled1(LED2);
USBSerial serial;
AnalogIn analog_Data(A1); For Analog communication int main(){
int B=3975; B value of the thermistor
float temperature, data;
float resistance; while(1){
data = analog_Data.read_u16(); Read Data from Analog port
resistance=(float)(65535-data)*10000/data; get the resistance of the sensor; 16 bit =65535
temperature=1/(log(resistance/10000)/B+1/298.15)-273.15; convert to temperature via datasheet ;
serial.printf("Temperature is : /r/n" , temperature); output is temperature which is a changing value depend on temperature sensor
wait(1); waiting second } }
Visual Basic code:
Imports System.IO Imports System.IO.Ports Imports System.Threading
Public Class Form1
Shared _continue As Boolean Shared _serialPort As SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load SerialPort1.Close()
SerialPort1.PortName = "com3" 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SerialPort1.Open()
RichTextBox1.Text = SerialPort1.ReadLine() Refresh()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SerialPort1.Close()
End Sub
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged End Sub End Class
1 Answer
9 years, 1 month ago.
If it works in putty then the problem is in almost certainly your VB code, you'll probably have more luck asking on a VB board than here. That said I can make a few guesses:
You only seem to read the serial port when you first click the button to open the port. Shouldn't your VB program be reading the port at least once per second or use some sort of interrupt to indicate when new data arrives? You should still get one line each time you click the open button, if you don't it may be that the Open() function flushes the buffer, try only opening the port if it's not already open.
In windows it's normally COM3 not com3, no idea if that makes a difference or not.
Oh and if you use <<code>> and <</code>>
if formats the code neatly. See the editing tips link for details and use the preview button. :-)
Thanks a lot Andy. are there any command to print data except belows :
serial.sprintf("%0.2d\n\r" , tem); serial.fprintf("%0.2d\n\r" , tem); serial.puts("%0.2d\n\r" ,tem); serial.fwrite("%0.2d\n\r" , tem);
serial._puts("%2d",tem); serial._putc(c); Send a character
serial.print("%2d",tem); serial.cout("%2d",tem);
serial.printf("%2d",tem);
posted by 21 Oct 2015puts ([string]) will output a fixed string, putc() is the same for a single character.
printf is the standard c function for outputting a formatted string, that's the one to use it you want to output a number as a text string. Some of your examples are a bit off, %d indicates that the number is a signed integer, %0.2 indicates to output rounded to 2 decimal places, so %0.2d indicates to output an integer to two decimal places.
sprintf is identical to printf but outputs to a variable rather than the serial port. fprintf is the same but to a file. There is also snprintf which is sprintf with protection against buffer overflows.
These are all standard c, Google will give you lots of examples.
posted by 22 Oct 2015