communication between pic18f4520 board and Visual basic application via UDP - vb.net

I am trying to communicate with application written in Visual basic. When i send data from application to board everything works perfectly, but in other direction it doesn't work. I do follow network traffic with wireshark and everything seems ok. When send data from board with PIC to application(PC) I can see it in wireshark, but I can't detect any data inside of application I think that my problem is at application , I am not very good at VB programming, not good at all..this is my first program.. I've attached code so you can see what I've done. Application(PC) ip address 192.168.1.11, pic ip address 192.168.1.10 (mask 255.255.255.0). So if someone could give me advice I would be very grateful..
PS. sorry for my bad english.
Imports System.Net
Imports System.Net.Sockets
Imports System.Text.Encoding
Imports System.Text
Public Class Form1
Dim controlData() As Byte = {&H55, &H55, &H55, &H55, &H55}
Const portTo As Integer = 5000
Const portFrom As Integer = 5001
Dim udpTransmit As New Sockets.UdpClient(portFrom)
Dim udpReceive As New Sockets.UdpClient(portTo)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnTo.Click
udpTransmit.Connect(tbToIp.Text, portTo)
udpTransmit.Send(controlData, controlData.Length)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
Dim ep As New IPEndPoint(tbToIp.Text, portTo)
Do
Dim receiveData() As Byte = udpReceive.Receive(ep)
text1.Text = ASCII.GetString(receiveData)
Loop
Catch ex As Exception
End Try
End Sub
End Class
[data form PIC to PC]
[data from PC to PIC]1

Related

Getting an access denied error when trying to pause/resume an outgoing message queue

Imports System.Messaging
Imports System.Collections
Imports MSMQ
Imports System.IO
Imports System
Imports System.Messaging.MessageQueue
Imports System.Runtime.InteropServices
Public Class PauseOutMessages
'Declare everything to be in the scope of all methods.
Dim mgmt As New MSMQManagement
Dim outqmgmt As MSMQOutgoingQueueManagement
Dim q As New MSMQApplication
Dim outgoingQueues As New ArrayList
Dim myQueue As New MessageQueue("FormatName:DIRECT=OS:myMachine\Private$\myQueue", QueueAccessMode.ReceiveAndAdmin)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each queue In q.ActiveQueues
If queue.IndexOf("DIRECT=") >= 0 Then
outgoingQueues.Add(queue)
End If
Next
End Sub
Private Sub Pause_Click(sender As Object, e As EventArgs) Handles Pause.Click
For Each queuePath In outgoingQueues
mgmt.Init(FormatName:=queuePath)
outqmgmt = mgmt.Pause()
Next
End Sub
Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
For Each queuePath In outgoingQueues
mgmt.Init(FormatName:=queuePath)
outqmgmt = mgmt.Resume()
Next
End Sub
Private Sub Send_Click(sender As Object, e As EventArgs) Handles Send.Click
myQueue.Send("Test")
For Each queue In q.ActiveQueues
If queue.IndexOf("DIRECT=") >= 0 Then
outgoingQueues.Add(queue)
End If
Next
End Sub
End Class
Here is the code I am using, by sending the test message to a non-existing path it gets stuck in the outgoing queue where I want to be able to call MSMQOutgoingQueueManagement.Pause or .Resume to be able to start and stop all outgoing queues.
However I keep getting an error on either mgmt.Pause() or mgmt.Resume() saying Access is denied. I can't seem to find a way to get on to the properties of outgoing queues to be able to adjust security settings. Any help would be greatly appreciated!
SOLVED!
Turns out I just needed to start up visual studio as an administrator and then it worked.

RAM usage increasing when reading frames from camera

I'm making some tests with AForge library. I'm trying to read data coming from my usb camera(frames). It works very nice but the only problem is the RAM. It leaks. It seems taht a frame takes ~30 KB but the used memory keeps increasing.
Here's my code:
Imports AForge
Imports AForge.Controls
Imports AForge.Video
Imports AForge.Video.DirectShow
Imports System.Threading
Imports System.IO
Imports System.Collections.Concurrent
Imports System.ComponentModel
Public Class Form1
Dim sources As New FilterInfoCollection(FilterCategory.VideoInputDevice)
Dim WithEvents device As VideoCaptureDevice
Dim count As Long, bit As Bitmap
Dim read As New Thread(AddressOf read_que)
Dim pic_que As New ConcurrentQueue(Of Bitmap)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each cam As FilterInfo In sources
ComboBox1.Items.Add(cam.Name)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
device = New VideoCaptureDevice(sources(ComboBox1.SelectedIndex).MonikerString)
AddHandler device.NewFrame, new Video.NewFrameEventHandler(AddressOf frame)
device.WaitForStop()
device.Start()
End Sub
Sub frame(obj As Object, args As NewFrameEventArgs)
If bit IsNot Nothing Then
bit.Dispose()
bit = Nothing
End If
bit = New Bitmap(args.Frame)
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Invoke(New MethodInvoker(Sub() PictureBox1.Image.Dispose()))
End If
PictureBox1.Image =bit ' or ... = Imaging.Image.Clone(args.Frame)
End Sub
End Class
I tried even to put all the frames in a concurrent queue and then in a separate thread to read it(I posted the simplified version that seems to take the least ram memory).
But there's another problem (this is not that important): when I start the app the picturebox is blanck and the used ram is 16 MB (constant-so it doesn't work).
Only when I enter task manager and I press End Process(without actually closing it) it starts showing the frames. I thing it's GUI related(when I press the End Process maybe it fires an event that starts the frame-reading class?).
Only at random times it seems to work from the first time(it's true that it might be the camera's problem because it old and works only on XP so I had to use .NET Framework 4).
Where is the problem(the priority is the ram leakeage)?
Try using this:
Sub frame(obj As Object, args As NewFrameEventArgs)
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() frame(obj, args))
Else
Dim oldImage = PictureBox1.Image
Dim bitmap = New Bitmap(args.Frame)
args.Frame.Dispose() 'Not sure if it has a Dispose
PictureBox1.Image = bitmap
If oldImage IsNot Nothing Then oldImage.Dispose()
End If
End Sub
Solved it. Thanks for all your help but I knew that my code was working(I used before a delegate to draw on picture box so it was thread-safe-the code I provided was "quick and dirty"). The problem was...the VM. I was testing the code on a VM. It works on a normal PC.
The code looks now like this:
Imports AForge
Imports AForge.Controls
Imports AForge.Video
Imports AForge.Video.DirectShow
Imports System.Threading
Imports System.IO
Imports System.Collections.Concurrent
Imports System.ComponentModel
Public Class Form1
Dim sources As New FilterInfoCollection(FilterCategory.VideoInputDevice)
Dim device As VideoCaptureDevice
Delegate Sub lp(ByRef pic As Bitmap)
Delegate Sub lpp(nr As Integer, nr2 As Integer)
Delegate Sub slp()
Dim count As Long, bit As Bitmap
Dim read As New Thread(AddressOf read_que)
Dim pic_que As New ConcurrentQueue(Of Bitmap)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each cam As FilterInfo In sources
ComboBox1.Items.Add(cam.Name)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
device = New VideoCaptureDevice(sources(ComboBox1.SelectedIndex).MonikerString)
AddHandler device.NewFrame, new Video.NewFrameEventHandler(AddressOf frame)
'device.WaitForStop()
device.Start()
'read.IsBackground = True
'read.Start()
End Sub
Sub frame(obj As Object, args As NewFrameEventArgs)
'If bit IsNot Nothing Then
' bit.Dispose()
' bit = Nothing
'End If
'bit = New Bitmap(args.Frame)
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Invoke(New MethodInvoker(Sub() PictureBox1.Image.Dispose()))
End If
PictureBox1.Invoke(New MethodInvoker(Sub() PictureBox1.Image = Imaging.Image.Clone(args.Frame))) 'Imaging.Image.Clone(args.Frame) ' or ...=bit
End Sub
End Class

Vb.Net compiling error

When I try and run my program in debug i keep receiving an error BC30456 it reads as follows:
Severity Code Description Project File Line Suppression State
Error BC30456 'Form1' is not a member of 'serialtest2'. serialtest2 C:\Users\Rhans\Desktop\VB6 Programs\Ethernet Socket\serialtest2\My Project\Application.Designer.vb 35 Active
I am looking to monitor a serial port that has a mettler toledo scale hooked to it and I am trying to display a continuous weight on the form...
Any help would be greatly appreciated.
The code is as follows:
Imports System.IO.Ports
Imports System.IO.Ports.SerialPort
Public Class SerialCommunication
Private WithEvents Port As New SerialPort
Private Sub SerialCommunication_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With Port
.PortName = "COM5"
.RtsEnable = True
.BaudRate = 9600
.Open()
End With
End Sub
Private Sub port_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Port.DataReceived
Dim buffer As String = Port.ReadExisting()
txtDisplay.Text = buffer
End Sub
Your SerialCommunication Class replaces Form1 by the looks of it.
Go to Project > Properties then select Startup Form: Serial Communication or change the name of your class to "Form1" instead of "SerialCommunication"
I had the same problem and my solution came from Web.config. I had removed the default language from compilation node. When i put it back it worked fine.
<compilation debug="true" defaultLanguage="vb" targetFramework="4.5.2">

Emgu webcam in VB beginner

lets go with the idea that i know nothing about nothing...
https://www.youtube.com/watch?v=37l6-O0T6EA
I was following this vids, all going well. but failing on "capturez.QueryFrame"
Imports Emgu.CV
Imports Emgu.CV.Util
Imports Emgu.CV.Structure
Public Class Form1
Dim capturez As Capture = New Capture
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim imagez As Image(Of Bgr, Byte) = capturez.Retrieve() 'Instead of QueryFrame, you may need to do RetrieveBgrFrame depending on the version of EmguCV you download.
PictureBox1.Image = imagez.ToBitmap()
End Sub
End Class
There is no Retrieve() method in vb.
Try to replace capturez.Retrieve() with capturez.QueryFrame() or Capturez.RetrieveBgrFrame().
If you already changed that but you get failing on capturez.QueryFrame, it means you are missing some dll files in x86 (bin/debug).

Online Multiplayer Game Send/Receive Information

I am coding a poker game in vb.net, and I want to create a multiplayer game using the internet, whether it being on WAN or LAN. I really have no idea where to start, and I don't know what to search on Google. I know that I will have to listen on a port, and I need to send and receive packets. No searches on Google are helping. Any links/ideas?
Also, how would I be able to show a list box of all the games currently in progress and allow the user to join? And how to create a new game?
I'd recommend learning the UdpClient class, it's a good option for beginners.
You first start by 'setting up' two clients, one to send data, and one to listen for incoming data. Once you've assigned the clients with the appropriate addresses and port numbers you then start the listening client in a loop, so that you can consistently listen for data.
Then you 'wire up' your sending client to some form of trigger, (in the example i've given below, i've set my sending client up to a button event) so you can send data at irregular intervals, or you can set your client in a loop to continuously send data.
Once you've done that, you now need to convert the data you want to send from string to a byte array, then you can finally send it, and vis versa for receiving data (from byte array to string).
Here's a simple example,
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Net
Public Class Form1
Private Const port As Integer = 9653 'Or whatever port number you want to use
Private Const broadcastAddress As String = "255.255.255.255"
Private receivingClient As UdpClient
Private sendingClient As UdpClient
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
InitializeSender()
InitializeReceiver()
End Sub
Private Sub InitializeSender()
sendingClient = New UdpClient(broadcastAddress, port) 'Use broadcastAddress for sending data locally (on LAN), otherwise you'll need the public (or global) IP address of the machine that you want to send your data to
sendingClient.EnableBroadcast = True
End Sub
Private Sub InitializeReceiver()
receivingClient = New UdpClient(port)
ThreadPool.QueueUserWorkItem(AddressOf Receiver) 'Start listener on another thread
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim stringToSend As String = TextBox1.Text 'Assuming you have a textbox with the data you want to send
If (Not String.IsNullOrEmpty(stringToSend)) Then
Dim data() As Byte = Encoding.ASCII.GetBytes(stringToSend)
sendingClient.Send(data, data.Length)
End If
End Sub
Private Sub Receiver()
Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, port) 'Listen for incoming data from any IP on the specified port
Do While True 'Notice that i've setup an infinite loop to continually listen for incoming data
Dim data() As Byte
data = receivingClient.Receive(endPoint)
Dim message As String = Encoding.ASCII.GetString(data) 'Recived data as string
Loop
End Sub
End Class
And now for adding a list box for available games.
Short answer, it's impossible very, very hard to do unless you have a server.
Longer answer, you'll need a server, and assuming you have a server you'll need to create an additional program to handle the data that's being sent to it, and to send data to other users.
I could go on to explain how to setup the additional program etc, etc, but judging that your network programming skills are still 'spreading out their wings', I'd suggest that you leave out advanced features like this until you have some more experience, then when you're more confident have a try by yourself and if you're still struggling just pop a question on here and I'm sure someone will help you.