| Author |
Message |
Rocky79

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
Hello, I have spent a lot of time trying to use the serial port command in the toolbox but i was unsucessful. I looked at the samples posted online but i always get errrors. Can anyone explains what are the steps needed to have a sucessful communication (read and write) from and to a serialport.If you can post the code it would be great...Please help
Thank you
Visual Studio Express Editions44
|
| |
|
| |
 |
ahmedilyas

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
|
| |
 |
Rocky79

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
Thanks but i have looked all that up. Can i drag the serialport1 icon from the toolbox and then set it's property in the property window without writing that in code . I still need to open the port and read/write from it. I am looking here for shortcuts. Thanks
|
| |
|
| |
 |
ahmedilyas

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
there are no shortcuts, things have been simplified! :)
you have to write the code, you maybe able to set properties from the Windows Designer however to read/write to the ports you have to write code otherwise how is it going to open/close/read/write to ports This is what a software developer does ;-)
|
| |
|
| |
 |
Rocky79

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
**can you help me figure this out.I am just following what you said. From the beginning. I openned a form in Vb Express and then I dragged the serialPort icon from the toolbox to the form and i defined all it's properties like baud rate, the com port and all that.
next i created a button icon on the form and them i double clicked it so that i would get into the form1.vb[design] ( the code view window) The following code are generated:**
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub End Class
**at this point i need to open the port to read and write** ** The tutorial That you sent me mentions that i need to declare the **data received event handler. ** i am not sure about the code below,where should i included it in my **form1 code and how should i read/write from to the port
Public Event DataReceived As SerialDataReceivedEventHandler
Dim instance As SerialPort Dim handler As SerialDataReceivedEventHandler
AddHandler instance.DataReceived, handle
Thank you much
|
| |
|
| |
 |
ahmedilyas

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
what you have done so far is correct
now to implement the dataRecieved event handler you can do this programmatically or via the Form designer so:
click on the SerialPort object/control you have added on the form, then look at the properties Window. Click on the lightning icon, which indicates Events and double click on "DataRecieved".
This will not create the DataRecieved event in your application for the serial port
To write to the serial port, just enter:
theSerialPort.Write("hi")
or whatever, the serial port class has the Write() method which you can use to send data
to read from the serial port, the DataRecieved event will be fired when there is data being recieved automatically.
does this help
|
| |
|
| |
 |
Rocky79

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
|
|
Hello, Here
is what i have at this point. I am just trying to read from the serial
port . Am i missing anything all i get is a blank messagebox. | |
Public Class Form1 Dim StoreReceived As String
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived StoreReceived = SerialPort1.ReadExisting
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(StoreReceived)
End Sub
End Class
|
|
| |
|
| |
 |
ahmedilyas

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
you are getting a blank message box because the variable "StoreRecieved" has nothing there.
|
| |
|
| |
 |
Rocky79

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
StoreReceived should store whatever string value captured from the StoreReceived = SerialPort1.ReadExisting. Also do i need to open/close the connection or it's done when i use the readExisting command. Please explain Thanks
|
| |
|
| |
 |
ahmedilyas

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
yes you do need to open and close the connection when you want to access the serial port
there may not be any data being recieved hence why you will get a blank message box or even the fact that you have not opened a connection and data will not be recieved
|
| |
|
| |
 |
Rocky79

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
Ok can you help me get this thing working by just filling in the missing code if there is.Here is my updated code with the open serialport
Public Class Form1 Dim StoreReceived As String
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
SerialPort1.Open() StoreReceived = SerialPort1.ReadChar
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(StoreReceived)
End Sub
End Class
|
| |
|
| |
 |
ahmedilyas

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
thats incorrect.
place a button on the form. Name it "open connection"
double click this button
type:
SerialPort1.Open()
on the form, place another button, name this "close connection"
double click this button
type:
SerialPort1.Close()
in your dataRecieved event handler, type:
MessageBox.Show(StoreRecieved)
|
| |
|
| |
 |
Rocky79

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
Yes! it did work. Thank you for being patient with me. At this point i am getting a string of numbers from the serial port, i am going to store all of them in an array. But the problem is that i don't know how many characters i will be receiving in that case how can i creat an array with unknown size Second when i open the serial port. How can i check if i am still receiving data Here is what i have so far..Thank you again.
Public Class Form1 Dim StoreReceived(5) As String Dim i As Integer
Private Sub btn_OpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OpenPort.Click
SerialPort1.Open() If SerialPort1.IsOpen Then MessageBox.Show("Serial Port Is Open") End If
End Sub
Private Sub btn_ShowReceivedData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ShowReceivedData.Click For i = 0 To 5
StoreReceived(i) = SerialPort1.ReadByte
TextBox1.Text = StoreReceived(i)
Next
End Sub
Private Sub btn_ClosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ClosePort.Click
SerialPort1.Close()
End Sub
End Class
|
| |
|
| |
 |
ahmedilyas

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
Well you can use an arraylist and just "add" the data read, so globally you would have:
Dim theDataItemsRecieved as new ArrayList()
then in your DataRecieved event, just "add" the data recieved:
Me.theDataItemsRecieved.Add(SerialPort1.ReadExisting())
you dont need to "check" if you are recieving data, the DataRecieved event will automatically get fired/executed when there is data to be read from the port.
|
| |
|
| |
 |
Rocky79

|
Posted: Visual Basic Express Edition, SerialPort 101 for a beginner |
Top |
So from what i understand is that you created an instance of class arraylist() and you called it the dataItemsReceived . 3 questions:
Now when you say Me.theDataItemsRecieved.Add(SerialPort1.ReadExisting()) you are essentially storing whatever is read from the serial port into the dataItemsReceived object
Also don't you need a loop to for the Me.theDataItemsRecieved.Add(SerialPort1.ReadExisting()) to store all the serial port stream
and last question how can you display the content of the theDataItemsRecieved array i got an error when i used the for loop to do that since theDataItemsRecieved is an object and it's missing the dot operator
|
| |
|
| |
 |
| |