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

How to send data Serially through COM port

100+
P: 108
Hi guys,

Just want to ask how can I send data serially? I have here a sample code but I use sockets and tcpclient class but I'm wondering how to do it using serial cable.Any idea?

Expand|Select|Wrap|Line Numbers
  1. Private Sub SendData(ByVal IpAdd As String, ByVal sData As String)
  2.  
  3.         Dim myIpAdd As System.Net.IPAddress
  4.         Dim myPort As Integer = 4003
  5.  
  6.         'Convert data String to Byte()
  7.         Dim pBytes() As Byte = Encoding.Default.GetBytes(sData)
  8.         'Debug:
  9.         'Dim pBytes() As Byte = Encoding.ASCII.GetBytes(sData)
  10.  
  11.         'create a socket
  12.         Dim isocket As Socket
  13.         isocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  14.  
  15.         'convert string ip to IPAddress 
  16.         myIpAdd = IPAddress.Parse(IpAdd)
  17.  
  18.         'Debug:
  19.         'Dim temp As String = myIpAdd.ToString()
  20.         'Dim ret = IPAddress.TryParse(IpAdd, temp)
  21.  
  22.         'connect to remote host
  23.         Try
  24.             isocket.Connect(myIpAdd, myPort)
  25.             If isocket.Connected() Then
  26.  
  27.                 'Send Data
  28.                 isocket.Send(pBytes)
  29.  
  30.                 'Debug
  31.                 'MsgBox("Sent... ")
  32.  
  33.                 'close connection
  34.                 isocket.Close()
  35.  
  36.             End If
  37.  
  38.         Catch se As SocketException
  39.             MsgBox("Socket Exception")
  40.  
  41.         Catch ex As Exception
  42.             MsgBox("UnExpected exception")
  43.  
  44.         End Try
  45.  
  46.     End Sub
Thanks
Nov 22 '07 #1
Share this Question
Share on Google+
3 Replies


Shashi Sadasivan
Expert 100+
P: 1,435
Hi,
I have never tried this, BUt got some good looking links
link1 link2

hope it helps
Nov 22 '07 #2

100+
P: 108
Hi,
I have never tried this, BUt got some good looking links
link1 link2

hope it helps

thanks for this link...Anyway, did you experience sending data using sockets and tthe data is distorted? I just saw it today when I delete MsgBox which inform me that data is sent/failed. I think I need to add delay before sending the next one? Do you know the how to add delay or sleep in .net?
Nov 22 '07 #3

Shashi Sadasivan
Expert 100+
P: 1,435
thanks for this link...Anyway, did you experience sending data using sockets and tthe data is distorted? I just saw it today when I delete MsgBox which inform me that data is sent/failed. I think I need to add delay before sending the next one? Do you know the how to add delay or sleep in .net?
Thread.Sleep(time) will put the current operation to sleep
Nov 22 '07 #4

Post your reply

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