Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends


I am writing a program in VB.NET 2010 to read data from a device through rs232 port
VB
btnStart.Enabled = False
btnStop.Enabled = True
If SerialPort1.IsOpen Then SerialPort1.Close()
SerialPort1.PortName = cmbPorts.Text
SerialPort1.BaudRate = cmbBaudRate.Text
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits =  IO.Ports.StopBits.One
SerialPort1.DataBits =  8
SerialPort1.Encoding =  System.Text.Encoding.Unicode
SerialPort1.ReadBufferSize = 4096
SerialPort1.Open()


then I have written to read the data as follows

VB
'Declared this delegate at top of form
Delegate Sub SetTextCallback(ByVal [text] As String) 

Private Sub SerialPort1_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            ReceivedText(SerialPort1.ReadExisting())    
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub ReceivedText(ByVal [text] As String)
           If Me.TextBox1.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.TextBox1.Text &= [text]
        End If
    End Sub
Posted
Updated 5-Jul-13 7:11am
v2
Comments
Richard MacCutchan 5-Jul-13 10:55am    
Are you sure that the device is sending you ASCII characters?
JYOTIRMOY SAMANTA 5-Jul-13 12:05pm    
actually the textbox where I display the message shows a series of sqlares like □ □ □ □
Richard MacCutchan 5-Jul-13 13:08pm    
But that means nothing, not all bytes are printable characters, and what you receive may still be valid data. You need to look at the actual values of each byte received and compare it with the expected response.

1 solution

The changes are that the data you are receiving is not straight characters, but binary values. This may be because the device you are connected to is not sending string data but binary values, or it could be because your port configuration is incorrect: baud rate, bits per character, or parity for example.

Try reading the data from the port as bytes, and displaying the hex values:
C#
Dim i As Integer = SerialPort1.BytesToRead
Dim data As Byte() = New Byte(i - 1) {}
i = SerialPort1.Read(data, 0, i)
Dim s As String = String.Join(" ", data.[Select](Function(c) c.ToString("X02")))
That should at least give you an idea what data is being returned.
 
Share this answer
 
Comments
JYOTIRMOY SAMANTA 5-Jul-13 11:54am    
How to determine which settings should I apply? or how to find which configuration is suitable or correct.
OriginalGriff 5-Jul-13 11:58am    
Best thing to do is look at the documentation! There are standard speeds you can try: 9600, 19200, and so forth, but it could take forever to "guess" the right settings, and even then you might get it slightly wrong and it fails in the future (for example, baud rates have a tolerance of 10%, so if you are "close" it may work with the specific unit and PC you have there, but fail completely on a different unit).

Talk to the manufacturers! It is by far the easiest way :laugh:
JYOTIRMOY SAMANTA 5-Jul-13 12:21pm    
Actually I have checked the Device Manager and checked the COM1 port setting it was same as my settings and software supplied by the vendor of that device is showing correct data. but when try with my code it gives error. a series of squares

The device is EM-100 Chemistry Analyzer
OriginalGriff 5-Jul-13 14:01pm    
Any setting sin the Device Manager does not reflect the settings as required by the device *at the other end* - it will just show the current or default settings. There is nothing stopping the manufacturers software changing that when (and only when) it want to talk. What did the manufacturers of the device say it wanted, and what it outputs?
SoMad 5-Jul-13 18:26pm    
As Richard and OriginalGriff have already said, even though you only see 'squares', that does not mean it is an error. You are seeing the data in this way because the values cannot be displayed as characters on the screen. As OriginalGriff said, you will most likely have to contact the manufacturer to figure out how the data is formatted.
In the software supplied by the manufacturer, are you able to see which COM port settings it is using (in a setup screen within the program)?

Soren Madsen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900