Is that possible? Checking if the port is open is okay but i don't know why i can't receive data/string after i sent a data.
In main Form:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
connect_btn.Enabled = False
With com1
.PortName = "COM2"
.BaudRate = 9600
.Parity = Parity.None
.StopBits = StopBits.One
.DataBits = 8
.Handshake = Handshake.RequestToSend
.RtsEnable = True
.DtrEnable = True
End With
com1.Open()
AddHandler RFID.DataReceived, SerialDataReceivedEventHandler1
End Sub
Sending Data:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim response As String = TextBox1.Text
If RFID.IsOpen Then
RFID.WriteLine(response)
End If
End Sub
Data Receive:
Private Sub com1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs)
If com1.IsOpen = True Then
read()
End If
End Sub
Friend Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
Dim newReceivedData As String
newReceivedData = RFID.ReadExisting
MessageBox.Show(newReceivedData)
End Sub
When i try to run it, There is no error.
EDITED
Using PuTTY and VSPE work to send a data.
Related
I have a cell counter machine that sends data automatically after test end through a rs232 cable. I tried the following code to receive that data but I get nothing although I changed the BaudRate value many times. What is the problem?
My code:
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class ComReadWrite
Dim myPorts As Array
Dim txtline As String
Dim txtchar As String
Dim txtbyte As String
Dim txtexisting As String
Delegate Sub setTextCallBack(ByVal txt As String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPorts = IO.Ports.SerialPort.GetPortNames()
portnamecombo.Items.AddRange(myPorts)
WriteButton.Enabled = False
CloseButton.Enabled = False
BaudRateBox.Items.Add(110)
BaudRateBox.Items.Add(300)
BaudRateBox.Items.Add(600)
BaudRateBox.Items.Add(1200)
BaudRateBox.Items.Add(2400)
BaudRateBox.Items.Add(4800)
BaudRateBox.Items.Add(9600) 'Populate the baudratebox Combo box to common baud rates used
BaudRateBox.Items.Add(14400)
BaudRateBox.Items.Add(19200)
BaudRateBox.Items.Add(38400)
BaudRateBox.Items.Add(57600)
BaudRateBox.Items.Add(115200)
BaudRateBox.Items.Add(128000)
BaudRateBox.Items.Add(256000)
BaudRateBox.Text = 38400
End Sub
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
SerialPort1.PortName = portnamecombo.Text
SerialPort1.BaudRate = BaudRateBox.Text
SerialPort1.ReadTimeout = 500
SerialPort1.Parity = Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = StopBits.One
SerialPort1.Open()
Start.Enabled = False
WriteButton.Enabled = True
CloseButton.Enabled = True
End Sub
Private Sub WriteButton_Click(sender As Object, e As EventArgs) Handles WriteButton.Click
SerialPort1.Write(Chr(6))
End Sub
Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click
SerialPort1.Close()
Start.Enabled = True
WriteButton.Enabled = False
CloseButton.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Try
RecievedText(SerialPort1.ReadExisting())
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub RecievedText(ByVal txt As String)
If Me.ReadBox.InvokeRequired Then
Dim x As New setTextCallBack(AddressOf RecievedText)
Me.Invoke(x, New Object() {(txt)})
Else
Me.ReadBox.Text &= txt
End If
End Sub
End Class
I'm newbie to VB.net. my case is, i need to display data from serial port to text box. In continuous loop. How do i do that?
I have wrote simple program to display the data. i can display the data once in the textbox, but not continuously.
serialport1 is where i get data from port 1. serialport2 is data from port 2.
SerialPort2.Write("!12,F" & vbCr)
data "!11,F" and "!12,F" is a command to my 2 device.
TextBox2.Text and TextBox5.Text is where i want to continuously display my data.
here is the code;
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Form2
Dim myPort As Array
Dim myport1 As Array
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
'------------------------------------------------
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
myport1 = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(myPort)
ComboBox2.Items.AddRange(myport1)
btnwrite.Enabled = False
End Sub
'------------------------------------------------
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles btninit.Click
SerialPort1.Encoding = System.Text.Encoding.GetEncoding(28591) ' convert to ascii
SerialPort1.PortName = ComboBox1.Text
SerialPort1.BaudRate = TextBox3.Text
SerialPort1.Open()
SerialPort2.Encoding = System.Text.Encoding.GetEncoding(28591) ' convert to ascii
SerialPort2.PortName = ComboBox2.Text
SerialPort2.BaudRate = TextBox3.Text
SerialPort2.Open()
btninit.Enabled = False
btnwrite.Enabled = True
btnwrite2.Enabled = True
btnclose.Enabled = True
End Sub
'------------------------------------------------
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnwrite.Click
SerialPort1.Write("!11,F" & vbCr) 'concatenate with \n
End Sub
Private Sub btnwrite2_Click(sender As Object, e As EventArgs) Handles btnwrite2.Click
SerialPort2.Write("!12,F" & vbCr) 'concatenate with \n
End Sub
Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub SerialPort2_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
ReceivedText1(SerialPort2.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
If Me.TextBox2.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.TextBox2.Text &= [text] 'append text
End If
End Sub
Private Sub ReceivedText1(ByVal [text] As String) 'input from ReadExisting
If Me.TextBox5.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText1)
Me.Invoke(x, New Object() {(text)})
Else
Me.TextBox5.Text &= [text] 'append text
End If
End Sub
Private Sub btnclose_click(sender As System.Object, e As System.EventArgs) Handles btnclose.Click
SerialPort1.Close()
SerialPort2.Close()
btninit.Enabled = True
btnwrite.Enabled = False
btnwrite2.Enabled = False
btnclose.Enabled = False
End Sub
End Class
I am trying to create vb.net program to get data from scales using serial port.
I don't know scales model, just Display model: IND231.
Problem is i get stupid data:
but if turn on Putty i get this:
As you see in putty there is only one line which refresh every sec, true weight is first two 00 (in middle), in my situation this line is not refreshing, but make a huge line and after 5-10 s, my program just crash.
My program code is:
Imports AxSerial
Public Class Form1
Dim Q As Queue(Of String) = New Queue(Of String)
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
For Each s In System.IO.Ports.SerialPort.GetPortNames()
lstPorts.Items.Add(s)
Next s
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnStart.Click
Try
If lstPorts.SelectedIndex = -1 Then
MsgBox("Please select a port")
Exit Sub
Else
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = 1
SerialPort1.RtsEnable = False
SerialPort1.PortName = lstPorts.SelectedItem.ToString
SerialPort1.Open()
Timer1.Start()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object,
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
Q.Enqueue(SerialPort1.ReadExisting())
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Timer1.Tick
SyncLock Q
While Q.Count > 0
txtReceived.Text &= Q.Dequeue
End While
End SyncLock
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnStop.Click
SerialPort1.Close()
Timer1.Stop()
End Sub
End Class
This is IND321 Serial Port Parameters
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = 1
SerialPort1.RtsEnable = False
Im not sure with RtsEnable, i think this is FlowControl.
What i need to correct to get data like putty?
It looks like your scale/serial port is returning data in another format.
Check your scale specs. It might be e72 or some other even/odd/bits/parity combination.
So I am making a port scanner and have a min and max port, but can't get the port scanner to stop scanning when it reaches the maximum port?
I have tried doing an Exit For when port reaches portmax.
Here is the code:
Public Class Form1
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim counter As Integer
Button2.Enabled = False
'set counter explained before to 0
counter = 0
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
Dim host As String
Dim counter As Integer
Dim portmin As Integer = TextBox3.Text
Dim portmax As Integer = TextBox2.Text
'Set the host and port and counter
counter = counter + 1 'counter is for the timer
host = TextBox1.Text
For port As Integer = portmin To portmax
' Next part creates a socket to try and connect
' on with the given user information.
Dim hostadd As System.Net.IPAddress = _
System.Net.Dns.GetHostEntry(host).AddressList(0)
Dim EPhost As New System.Net.IPEndPoint(hostadd, port)
Dim s As New System.Net.Sockets.Socket( _
System.Net.Sockets.AddressFamily.InterNetwork, _
System.Net.Sockets.SocketType.Stream, _
System.Net.Sockets.ProtocolType.Tcp)
Try
s.Connect(EPhost)
Catch
End Try
If Not s.Connected Then
ListBox1.Items.Add("Port " + port.ToString + " is not open")
Else
ListBox1.Items.Add("Port " + port.ToString + " is open")
ListBox2.Items.Add(port.ToString)
End If
Label3.Text = "Open Ports: " + ListBox2.Items.Count.ToString
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
'stop button
Timer1.Stop()
Timer1.Enabled = False
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add("Scanning: " + TextBox1.Text)
ListBox1.Items.Add("-------------------")
Button2.Enabled = True
Button1.Enabled = False
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
End Sub
End Class
I would really appreciate any help
thanks,
Try to stop the Timer while in the scanning.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
'Your code
Timer1.Enabled = True
End Sub
If thats the problem, and looks like it, you should consider using a Try/Catch block:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Timer1.Enabled = False
'Your code
Catch ex As Exception
'Manage the error
Finally
Timer1.Enabled = True
End Try
End Sub
Try this
For port As Integer = portmin To portmax - 1
After searching around I am still having issues with reading data from a serial port in VB.Net/VS2010. I know the Serial Port works, I can write to the port fine but when reading from it, nothing happens. I have only been programming for the last 3 weeks so am still trying to get my head around it all.
The program must run to capture data from a door logger, I will then be outputting the data to a database (not yet implemented - I want to get this part sorted first).
I have tried using several terminal programs as well as another device which outputs data onto the serial line, with nothing displaying in the textbox tbxIn.
Any help would be greatly appreciated.
Code is below:
Imports System.IO.Ports
Imports System.IO.Ports.SerialPort
Public Class Form1
Dim comPort As IO.Ports.SerialPort = Nothing
Dim sComPort As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GetSerialPortNames()
End Sub
Sub GetSerialPortNames()
' Show all available COM ports.
For Each sp As String In My.Computer.Ports.SerialPortNames
lstPorts.Items.Add(sp)
Next
End Sub
Private Sub lstPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPorts.SelectedIndexChanged
sComPort = lstPorts.SelectedItem
Button1.Enabled = True
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Open the serial port using the OpenSerialPort method
Button1.Enabled = False
Button2.Enabled = True
Try
comPort = My.Computer.Ports.OpenSerialPort(sComPort, 9600, IO.Ports.Parity.None, 8, 1)
' comPort.DtrEnable = True
comPort.ReadTimeout = 500
Do
comPort.WriteLine("Go")
Dim sIncomming As String = comPort.ReadLine()
tbxIn.Text = sIncomming & vbCrLf
Loop
Catch ex As TimeoutException
tbxIn.Text &= "Error: Serial Port Read Timeout" & vbCrLf
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
comPort.Close()
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
tbxIn.Text = e.ToString
End Sub
End Class
PRetty sure this will get you what you need. You DON't need the Serial1 component on the designer. Remove that and use this code:
Private comPort As IO.Ports.SerialPort = Nothing
Private sComPort As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
GetSerialPortNames()
End Sub
Sub GetSerialPortNames()
' Show all available COM ports.
For Each sp As String In My.Computer.Ports.SerialPortNames
lstPorts.Items.Add(sp)
Next
End Sub
Private Sub lstPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPorts.SelectedIndexChanged
sComPort = lstPorts.SelectedItem
Button1.Enabled = True
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Open the serial port using the OpenSerialPort method
Button1.Enabled = False
Button2.Enabled = True
Try
comPort = My.Computer.Ports.OpenSerialPort(sComPort, 9600, IO.Ports.Parity.None, 8, 1)
' comPort.DtrEnable = True
'must add handler
AddHandler comPort.DataReceived, AddressOf SerialPort1_DataReceived
comPort.ReadTimeout = 500
Do
comPort.WriteLine("Go")
Dim sIncomming As String = comPort.ReadLine()
tbxIn.Text = sIncomming & vbCrLf
Loop
Catch ex As TimeoutException
tbxIn.Text &= "Error: Serial Port Read Timeout" & vbCrLf
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
comPort.Close()
'remove handler
RemoveHandler comPort.DataReceived, AddressOf SerialPort1_DataReceived
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
tbxIn.Text = e.ToString
End Sub