I have a problem with the receiving part using ENC28. I want to communicate between the PC and G120 using ENC28. The PC and the ENC28 module is both connected to a router.
When I send a UDP packet from G120 to the PC I see the message in the output box. But the G120 can’t receive from the PC when it goes the other way.
I can see the yellow LED on ENC28 is blinking when I send and that indicates the message is received (I think).
Here is some of my code on G120:
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);
client.Connect(ep);
byte[] RxBuffer = new byte[20];
while (true)
{
if (!button1.Read() == true)
{
byte[] pakke = { 0x7f, 0x63, 0x00, 0x0a };
client.SendTo(pakke, ep);
Thread.Sleep(1000);
}
int i = client.Receive(RxBuffer);
for (int i = 0; i < RxBuffer.Length; i++)
{
Debug.Print(RxBuffer[i] + "-");
}
}
And the code on my PC:
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Parse(IP1), listenPort);
byte[] receive_byte_array;
// if I press a button in Windows Form.
byte[] pakke = { 0x7f, 0x63, 0x00, 0x0a };
client.SendTo(pakke, ep);
receive_byte_array = listener.Receive(ref groupEP);
for (int i = 0; i < receive_byte_array.Length; i++)
{
Console.Write(receive_byte_array[i] + "-");
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Test_UDP_PC
{
class Program
{
static void Main(string[] args)
{
Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint MyServiceEndPoint = new IPEndPoint(IPAddress.Any, port);
mySocket.Bind(MyServiceEndPoint);
while (true)
{
while (mySocket.Poll(200000, SelectMode.SelectRead))
{
if (mySocket.Available > 0)
{
byte[] inBuf = new byte[mySocket.Available];
EndPoint recEndPoint = new IPEndPoint(IPAddress.Any, 0);
mySocket.ReceiveFrom(inBuf, ref recEndPoint);
Console.WriteLine("Message From " + ((IPEndPoint)recEndPoint).Address.ToString());
Console.WriteLine(new string(Encoding.UTF8.GetChars(inBuf)));
String msg = "Hello FEZ Spider! I am PC";
byte[] bytesToSend = Encoding.UTF8.GetBytes(msg);
mySocket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None,
(IPEndPoint)recEndPoint);
}
}
}
}
}
}
And here is on G120
System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint endPoint = new IPEndPoint(ip, port);
socket.Connect(endPoint);
String msg = "Hello PC, I am FEZ spider";
byte[] bytesToSend = Encoding.UTF8.GetBytes(msg);
while (true)
{
socket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None, endPoint);
while (socket.Poll(2000000, SelectMode.SelectRead))
{
if (socket.Available > 0)
{
byte[] inBuf = new byte[socket.Available];
EndPoint recEndPoint = new IPEndPoint(IPAddress.Any, 0);
socket.ReceiveFrom(inBuf, ref recEndPoint);
if (!recEndPoint.Equals(endPoint))// Check if the received packet is from the 192.168.0.2
continue;
Debug.Print(new String(Encoding.UTF8.GetChars(inBuf)));
}
}
Thread.Sleep(100);
}
What if I just want to receive from the G120 board? I don’t want to sent packets. I tried to delete the operations where you sent (on G120). Then I deleted the receiving part on the PC.
I can’t get it worked.
Is that impossible?
Case:
I want to sent a packets from PC to G120 every second. The G120 is just reading the packets (And show them in debug mode).
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Test_UDP_PC
{
class Program
{
static void Main(string[] args)
{
Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint MyServiceEndPoint = new IPEndPoint(IPAddress.Any, port);
mySocket.Bind(MyServiceEndPoint);
byte[] inBuf = new byte[mySocket.Available];
EndPoint recEndPoint = new IPEndPoint(IPAddress.Any, 0);
String msg = "Hello FEZ Spider! I am PC";
byte[] bytesToSend = Encoding.UTF8.GetBytes(msg);
while (true)
{
mySocket.SendTo(bytesToSend, bytesToSend.Length, SocketFlags.None, (IPEndPoint)recEndPoint);
Thread.sleep(1000);
}
}
}
}
It is still not working. I got an error message says:
"An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in System.dll"
at the PC side on the line: