By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,481 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,481 IT Pros & Developers. It's quick & easy.

how to receive data from a hardware device through serial port.

P: 1
hii
am able to send hex data to the hardware device but not able to receive the data it is sending here is my code


Expand|Select|Wrap|Line Numbers
  1. Public Function getMessage() As Integer
  2.         Try
  3.             Dim hostEntry As IPHostEntry = Nothing
  4.             Dim s As Socket = Nothing
  5.  
  6.  
  7.             Dim i As Integer
  8.             Dim str, str1 As String
  9.             str = String.Empty
  10.             'serialport.Open()
  11.  
  12.             If ServerConnection = True Then
  13.                 While True
  14.                     If ServerConnection = True Then
  15.                         str1 = String.Empty
  16.  
  17.  
  18.                         Dim byteToRead As Byte = serialport.ReadByte()
  19.                         Buffer.Add(byteToRead)
  20.  
  21.                         If Buffer.Count < 7 Then
  22.                             'If Buffer.Count < 7 And Buffer.Item(0) = &H1 And Buffer.Item(2) = &H4 And Buffer.Item(3) = &H50 And Buffer.Item(4) = &H0 And Buffer.Item(5) = &H10 And Buffer.Item(7) = &HFF Then
  23.                             receivesuccess = True
  24.  
  25.                             For iInside As Integer = 0 To 7
  26.                                 str = Hex(Buffer.Item(iInside))
  27.                                 If str.Length = 1 Then
  28.                                     str = "0" + str
  29.                                 End If
  30.                                 str1 += str + "  "
  31.                             Next
  32.                             RR.Insert(0, str1)
  33.                             'End If
  34.  
  35.                         End If
  36.                     End If
  37.                 End While
  38.  
  39.             End If
  40. End Function
  41.  

this is send message in hex format and this is working and it is writing data to the serial port and am able to display it.



Expand|Select|Wrap|Line Numbers
  1. Public Function SendMessage(ByVal NoOfData As Integer) As Integer
  2.         Try
  3.             serialport.Close()
  4.             serialport.Open()
  5.             'serialport.PortName = "COM3"
  6.  
  7.             If serialport.IsOpen Then
  8.                 serialport.Write(MassageToSend, 0, NoOfData)
  9.                 'serialport.Read(ReceivedResponse, 0, 24)
  10.                 Dim str, str1 As String
  11.  
  12.                 str = ""
  13.                 For ctr As Byte = 0 To CByte(NoOfData - 1)
  14.                     str1 = Hex(ServerComm.MassageToSend(ctr))
  15.                     If str1.Length = 1 Then
  16.                         str1 = "" + "0" + str1 + ""
  17.                     End If
  18.                     str = str + Space(7) & str1 + ""
  19.  
  20.                 Next
  21.  
  22.                 str = ">>" + str
  23.                 Threading.Thread.Sleep(100)
  24.                 'str = System.Text.Encoding.Default(str + vbCrLf)
  25.                 With SunRose.tbText
  26.                     .Font = New Font("Garamond", 12.0!, FontStyle.Regular)
  27.                     SunRose.Tx.FillColor = Color.White
  28.                     SunRose.Tx.FillColor = Color.Red
  29.                     .SelectionColor = Color.Red
  30.                     .AppendText(str & vbCrLf)
  31.                     .ScrollToCaret()
  32.  
  33.  
  34.                     Dim file As String = Application.StartupPath() + "\Backup\Excel\" + "HEXdata" + System.DateTime.Now.ToString("dd" & "MM" & "" & "yyyy") + ".csv"
  35.  
  36.                     Dim str_send = ""
  37.  
  38.                     str_send += str + " - " + System.DateTime.Now.ToString("dd") + "/" + System.DateTime.Now.ToString("MM") + "/" + System.DateTime.Now.ToString("yyyy") + "  " + System.DateTime.Now.ToString("HH") + ":" + System.DateTime.Now.ToString("mm") + ":" + System.DateTime.Now.ToString("ss") + vbCrLf
  39.  
  40.                 End With
  41.                 Threading.Thread.Sleep(800)
  42. end sub
but in getmessage() as soon as debuger is executing readbyte() it is not moving and nothing is geting displayed so plz help me with code and sugestions
May 17 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.