简体   繁体   中英

Trouble with reading Serial Port using VB.net 2008

let me first tell you about the problem I have been facing. I have microcontroller interfaced with serial port of my computer. In my microcontroller I have 2000 sample data and my primary target is to read those data. Now I can read those data in hyper terminal, but when it comes to my application it doesnt show anything. Now I am very new to VB.net so there must be something missing.

Private Sub sp1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp1.DataReceived

    sp1.Open()
    MsgBox(sp1.ReadExisting())

End Sub

this simple code block should show me the data.But it doesnt.

data format coming Microcontroller is in this following formate :

nitialization successful !,1023
Starting sampling process... 1023,1023 1023,1023

0,2 1023,1023 1023,1023 1023,1023 212,686 1023,1023 1023,1023
1,5 1023,1023 1023,1023 1023,1023,659 213,689 1023,1023 1023,1023

now, I dont understand why it doesnt read anything :( please help me out. Thank you

NB: SP1 has a baudrate of 19200,Databits 8 and no parity,COM1 port. I aisnt sure whether I will be needing a buffer or not

You will need to check whether or not you need to turn on flow control.

Most often, the serial port will be using RTS hardware flow control. Setting RtsEnable = True will allow data to flow to your application.

As was mentioned in another comment, you should not attempt to open the serial port object within the data received event handler. The serial port object should already be open when the data received event is fired. That is probably the reason you aren't getting data when you perform the ReadExisting method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM