I just adquire a cash drawer, connected with a rj11 port to my computer. When i ask about to how to open in my application, they said me to just send a sequence characters at less than 1200 bps to port com5 , beacuse the rj11 is configured to respond to that. How can i send it? I already try this but nothing happens
Using COM As System.IO.Ports.SerialPort =
My.Computer.Ports.OpenSerialPort("COM5")
COM.WriteLine("Enviodeprueba")
End Using
If you know those are the correct baud rate and port number, set those for the serial port first:
Dim COM As System.IO.Ports.SerialPort = New System.IO.Ports.SerialPort
With COM
.PortName = "COM5"
.BaudRate = "1100" 'as you stated it has to be less than 1200
.DataBits = 8
'and whatever other properties you want to set for the port here
End With
Then you have to open the port:
COM.Open()
THEN you should be able to send info to it:
COM.WriteLine("Enviodeprueba")
And be sure to close the port when you are done with it:
COM.Close()
This is the MSDN article about WriteLine for a port.
Related
I currently have a program that connected to the PLC of machine and each have a dedicated serial port that monitors a dedicated device.
I used port COM3 for monitoring L (Left) side and port COM5 for monitoring R (Right) side of PLC.
All works fine, with no conflicts.
But when I want to set COM3 for monitoring both of sides (L & R), I got an error info that says "Access to the port 'COM3' is denied.". I know this happened because port COM3 is already used by L (Left) sides.
I want to use if the else, like;
if Me.cPlc(i).PortOpen() Then
// Take the value and close
Me.cPlc(i).PortClose();
End if
But I don't know how to do that. Does anyone know how to handle this problem ?
Thank you very much.
Here is the piece of code that is giving me trouble:
'Open each port
Dim length As Integer = Me.cPlc.Count - 1
If length > -1 Then
For i As Integer = 0 To length
Me.cPlc(i).PortOpen() // The error occurs on this line.
Next
Do
'Read the device value from the PLC at the end of each port
For i As Integer = 0 To length
Me.cPlc(i).Scan()
Next
Loop Until Me.BgwPlc.CancellationPending = True
'Close each port
For i As Integer = 0 To length
Me.cPlc(i).PortClose()
Next
End If
I am developing a piece of scientific software in VB.net that I intend to distribute. Part of the application will involve communication with a server (and other clients). For all intents and purposes the communication will be like a chat application.
I have developed the software using sockets, and it works fine when testing at home on my local network. Below is basically how I send messages:
Dim serverIPString as String = "192.168.1.5"
Dim serverPort as long = 65500
dim messageString as String = "Hello"
Dim Client As New System.Net.Sockets.TcpClient(serverIPString, serverPort)
Dim Writer As System.IO.StreamWriter(Client.GetStream())
Writer.Write(messageString)
Writer.Flush()
Writer.Close()
And this is how I listen:
Public Class TCPIPListener
Dim initalised As Boolean = False
Dim port As Long
Dim IP As System.Net.IPAddress
Dim ourListener As TcpListener
Dim ourClient As TcpClient
Dim ourMessage As String
'''''''''''
'' ctors ''
'''''''''''
Public Sub New()
initalised = False
End Sub
'Takes IP and port and starts listeing
Public Sub New(inIP As String, inPort As Long, inExchange As messageExchange)
'Try to set the IP
Try
IP = System.Net.IPAddress.Parse(inIP)
Catch ex As Exception
Exit Sub
End Try
'Set the port
port = inPort
initalised = startListener()
End Sub
'''''''''''''''
'' Listening ''
'''''''''''''''
Private Sub Listening()
ourListener.Start()
End Sub
'starts listener
Public Function startListener() As Boolean
ourListener = New TcpListener(IP, port)
ourClient = New TcpClient()
Dim ListenerThread As New Thread(New ThreadStart(AddressOf Listening))
ListenerThread.Start()
initalised = True
Return True
End Function
'Called from a timer on the form
Public Function tick() As Boolean
If Not initalised Then
Return False
End If
If ourListener.Pending = True Then
ourMessage = ""
ourClient = ourListener.AcceptTcpClient()
Dim Reader As New System.IO.StreamReader(ourClient.GetStream())
While Reader.Peek > -1
ourMessage = ourMessage + Convert.ToChar(Reader.Read()).ToString
End While
messagebox(ourMessage)
End If
Return True
End Function
End Class
Using this approach, every client will be listening for messages sent from the server, and any messages sent will go to the server and be directed to the relevant client - it's a little inefficient but necessary for the way my software works.
The problem is that I am having real trouble getting this to work across the internet. If I send a message to a local IP address it works fine, even from the debugger. If I use a external IP address I get the following:
No connection could be made because the target machine actively refused it XXX.XXX.XXX.XXX:65500"
I have turned on port forwarding with my router and set up the correct port (65500 here) to forward to my PC running the lister (192.168.1.5) for both TCP and UDP. This website it tells me this port is open, and those either side are not.
If I right click on the exe and run as administrator, my antivirus pops up and says it is doing something suspicious when I start the listener (which I guess if anything is encouraging). If I then add my application to the list of exceptions and run it again, then check to see if the port is listening (using this) I find my application listening on the correct port (if I stop the application it is gone). However the client application still gives the same error above. If I use a random IP address rather than the one of the listener I get a different error:
{"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond XXX.XXX.XXX.XXX:65500"}
I suspect I am not taking the correct approach to this. I am fine with most aspects of VB, but have not really tackled this sort of problem before. When it comes to distributing software I really don't want my customers to have to mess around with deep settings to get this application to work. Any help will be immensely useful to me!
Another problem I guess I will face in the future is if multiple clients have the same external IP but different internal IPs (I intend to distribute to universities). Under these circumstances I guess port forwarding will not work.
Thanks very much in advance (and apologies for the long post)!
If you set your server to listen for connections on 192.168.1.5 then it will only allow connections from that specific IP address. The address you give a TcpListener is the IP address which you will accept connections from.
Since the 192.168.1.5 address is internal no one outside your network will be able to connect to your server. Make your server listen to 0.0.0.0 (or the equivalent IPAddress.Any) instead to make it allow connections from anyone.
I already acquired the code for Name of the modem. But I don't know how to use WMI because I can't really understand how does it works. For now, the code for it is so complicated.
Can you guys help me regarding to name my port? It's RFID Reader and it's USB Port. Look for the image as reference.
This image will show you that in Left Side < I got my COM 7 port in Modem but In right side > I got many option to pick, because my modem opens many port. I need to know how to name my other port(RFID COM PORT) so that I dont need to guess what is the right port for it.
Try
Dim searcher As New ManagementObjectSearcher( _
"root\cimv2", _
"SELECT * FROM Win32_SerialPort")
For Each queryObj As ManagementObject In searcher.Get()
MsgBox(queryObj("Name"))
enter code here
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
So, I change the code, and I just want the system to message me what is the open port. But, unfortunately, My RFID (Profilic USB-to-Serial Comm Port (COM3) is not being shown.
Edit
This is the output messagebox, well as you can see guys, there's no profilic. Even though it's been plugged, and seen by Device Manager.
But If I change the code to Win32_PnPEntity it will show all peripherals, and etc. and eventually It will show the Image
There are two serialports in my computer.
How do I get the serialport information?
(I know about coding of Serialport, but I really want to know about Serialport of my Computer information)
is it possible to get the information about Serialport of my computer?
I'm coding in vb.net
is is possible in vb.net?
if it possible, please tell me how to do it.
which information do you want to have about the serial port?
using the MSComm control you can use the following function to determine if the port exists and if it's already in use or not:
Public Enum PortAttr
PortFree = 0
PortInUse = 1
PortUnknown = 2
End Enum
Public Function CheckPort(intPort As Integer) As PortAttr
On Error GoTo ErrorFound
With MSComm1
If .PortOpen Then .PortOpen = False
.CommPort = intPort
.PortOpen = True
CheckPort = PortFree
If .PortOpen = False Then .PortOpen = True
End With 'MSComm1
Exit Function
ErrorFound:
Select Case Err.Number
Case 8002 'port doesnt exist
CheckPort = PortUnknown
Case 8005 'port already in use
CheckPort = PortInUse
Case Else
MsgBox Err.Description, vbCritical, "Error " & CStr(Err.Number) & " on Port " & CStr(intPort)
End Select
On Error GoTo 0
End Function
You can then loop from 1 to 16 to see if any of these ports exist (usb converters could add extra ports)
For intIndex = 1 To 16
Select Case CheckPort(intIndex)
Case PortFree
intFree = intFree + 1
cboPort.AddItem "Com" & CStr(intIndex), intFree 'add the port to the "free" list
cboPort.ItemData(intFree) = intIndex
Case PortInUse
cboPort.AddItem "Com" & CStr(intIndex) 'add the port to the "in use" ist
End Select
Next intIndex
Use the MSComm control in Visual Basic .NET to access serial ports
Because no Microsoft .NET Framework classes exist to access the communications resources that connected to your computer, you can use the MSComm control in Microsoft Visual Basic 6.0. The MSComm control provides serial communications for your application by enabling the transmission and reception of data through a serial port. To implement basic serial communications by using a modem, follow these steps:
Start Microsoft Visual Studio .NET.
On the File menu, point to New, and then click Project.
Under Project Types, click Visual Basic Projects.
Under Templates, click Console Application.
In the Name box, type MyConsoleApplication, and then click OK.
By default, Module1.vb is created.
Right-click the MyConsoleApplication project, and then click Add Reference.
Click the COM tab, click Microsoft Comm Control 6.0 under Component Name, click Select, and then click OK.
Note To use the MSComm control, you must install the related COM components of Microsoft Visual Basic 6.0 on the same computer that has Microsoft Visual Studio .NET installed.
For more information about license issues when you use Visual Basic 6.0 controls in Visual Studio .NET, click the following article number to view the article in the Microsoft Knowledge Base:
318597 Errors when you use Visual Basic 6.0 controls in Visual Studio .NET
Replace the code in Module1.vb with the following code example.
Imports MSCommLib
Module Module1
Sub Main()
'New a MSComm control
Dim MSComm1 As MSComm
MSComm1 = New MSComm
' Buffer to hold input string.
Dim Buffer As String
' Use the COM1 serial port.
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read the whole buffer when Input is used.
MSComm1.InputLen = 0
' Open the serial port.
MSComm1.PortOpen = True
Console.WriteLine("Open the serial port.")
' Tell the control to make the Input property return text data.
MSComm1.InputMode() = InputModeConstants.comInputModeText
'Clear the receive buffer.
MSComm1.InBufferCount() = 0
' Send the attention command to the modem.
MSComm1.Output = "ATV1Q0" & Chr(13)
Console.WriteLine("Send the attention command to the modem.")
Console.WriteLine("Wait for the data to come back to the serial port...")
' Make sure that the modem responds with "OK".
' Wait for the data to come back to the serial port.
Do
Buffer = Buffer & MSComm1.Input
Loop Until InStr(Buffer, "OK" & vbCrLf)
' Read the "OK" response data in the serial port.
' Close the serial port.
Console.WriteLine("Read the OK response data in the serial port.")
MSComm1.PortOpen = False
Console.WriteLine("Close the serial port.")
End Sub
End Module
Press CRTL+F5 to build and run this project. You will receive the following output messages:
Open the serial port.
Send the attention command to the modem.
Wait for data to come back to the serial port...
Read the OK response data in the serial port.
Close the serial port.
To read the rest of this you can go here How to access serial and parallel ports by using Visual Basic .NET
Currently I use the following code to retrieve the IP address of the local workstation...
strIPAddress = System.Net.Dns.GetHostEntry(strComputerName).AddressList(0).ToString()
This is fine for the Windows XP workstations. However, in Vista and Windows 7, this returns the IPv6 address which is not used at all. Is there a method of setting this to work so it always returns the IPv4 address regardless of platform?
I know I can increment the AddressList value to 1 and get the correct IP in Windows 7. The bad part is that this requires going through the motions of identifying the OS and choosing one or the other.
The must be some way of specifying IPv4 only. Perhaps getting a result from DNS on the network rather than the workstation itself?
You just need to loop through the AddressList looking at the AddressFamily seeing which is set to InterNetwork
Dim IP4 = New List(Of IPAddress)(Dns.GetHostEntry(strComputer).AddressList).Find(Function(f) f.AddressFamily = Sockets.AddressFamily.InterNetwork)
Or the longer way:
Dim IP4 As IPAddress
Dim AL = Dns.GetHostEntry(strComputer).AddressList
For Each A In AL
If A.AddressFamily = Sockets.AddressFamily.InterNetwork Then
IP4 = A
Exit For
End If
Next