Question Visual basic 2010 the problem of reading serial port

codervb

New member
Joined
Apr 5, 2012
Messages
3
Programming Experience
1-3
Hi masters,I am using visual studio 2010 and have a problem in vb.net.I can write data to the serial port but can not the read it.Can anyone say to me where I make a mistake?


npq3ab.png
 
Um, the first mistake is that you have no code for reading, or at least you haven't shown us any. We can't tell you what you did wrong if we don't know what you did.
 
jmcilhinney is correct - without code, how are we meant to guess.

However, in this case, could it possibly be that it says "port closed" ??
 
I have two textboxes called Textbox1,Textbox2.I get text from textbox1 and write it to the port.
then I show the data that I have written in textbox2.but I can write the data to port but can not read it.
How can I show the data getting from serial port?

However, in this case, could it possibly be that it says "port closed" ??
No.I am sure port is open
 
sorry.forget to add the code

Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim bfx(255) As Byte
Try
port.Write(TextBox2.Text & vbCr)
TextBox1.Text = port.Read(bfx, 1, port.BytesToRead)




Catch ex1 As Exception


MsgBox(ex1.Message)
End Try
End Sub
 
You are reading the data into the Byte array. If the data represents text then you need to use an appropriate Encoding object to convert that Byte array to a String.
 
In your case, the easiest and most convenient way to handle it is through the System.Text.Encoding class....

TextBox1.Text = System.Text.Encoding.UTF8.GetString(bfx)
 
Last edited:
In your case, the easiest and most convenient way to handle it is through the System.Text.Encoding class....

TextBox1.Text = System.Text.Encoding.UTF8.GetString(bfx)

It's not the most convenient way. It's basically the only way. It's just a matter of which Encoding class to use, which is determined by the encoding used at the other end to convert the original text to binary data in the first place.
 
Back
Top