Kevin's Week 8 Report
Week 8
Over spring break, I altered my Ethernet code to connect to the W5100 on Ashely’s end. The initial code I had to connect to the W5100 was not the right frame of thinking. The code I have included below can be implemented in the client/server relationship required for the W5100 to communicate with the PC. For now the serial communication code and random data code is commented out.
public void startThread()
{
MySqlConnection dbCon = new MySqlConnection(connStr);
DateTime currTime=DateTime.Now;
int secondTag = currTime.Second;
int minuteTag = currTime.Minute;
int dayTag = currTime.Day;
//serialPort = new SerialPort("COM4");
//serialPort.BaudRate = 9600;
//serialPort.Parity = Parity.None;
//serialPort.DataBits = 8;
//serialPort.StopBits = StopBits.Two;
while (true)
{
currTime = DateTime.Now;
if (secondTag != currTime.Second) //changed to == from !=
{
//Store second
//Read Sensor
String sensorValue = ""; //resets sensorValue
try
{
//WebClient client = new WebClient();
//sensorValue = client.DownloadString(address);
//possible serial testing
/*
if(!(serialPort.IsOpen==true)) serialPort.Open();
serialPort.Write("p");
sensorValue=Convert.ToString(serialPort.ReadChar());
//sensorValue2 = sensorValue2 + 10;
// sensorValue = Convert.ToString(sensorValue2);
serialPort.Close();
*/
//client code
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect("172.21.5.99", 8001); //ip address subject to change for ip of w5100
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] datasend = asen.GetBytes("1"); //will send a 1 to request the data
stm.Write(datasend, 0, datasend.Length); //writes "1" to the stream connection
byte[] dataread = new byte[100]; //reads data that was sent from w5100
int datalength = stm.Read(dataread, 0, 100);
for (int i = 0; i < datalength; i++) //goes through each character and puts it in string
sensorValue= sensorValue+(Convert.ToChar(dataread[i]));
tcpclnt.Close();
//or for server setup
/*
IPAddress ipAd = IPAddress.Parse("66.133.71.113"); //ip address of this computer
TcpListener myList = new TcpListener(ipAd, 8001); //creates listener for clients to connect
myList.Start();
Socket myConnect = myList.AcceptSocket();
byte[] datareads = new byte[100];
int datalengths = myConnect.Receive(datareads);
ASCIIEncoding asen = new ASCIIEncoding();
myConnect.Send(asen.GetBytes("1"));
byte[] datareads = new byte[100];
int datalengths = myConnect.Receive(datareads);
for (int i = 0; i < datalengths; i++)
sensorValue = sensorValue + (Convert.ToChar(dataread[i]));
myConnect.Close();
myList.Stop();
*/
}
catch (Exception e)
{
string error = "Unable to read " + name + ": Error..... " + e.StackTrace;
form.debug.BeginInvoke(
new Form1.DebugCallback(form.UpdateDebugOutput), error);
}
In the above code, the bold section is the code for a client setup on the PC side. The underlined section, which is for now commented out to avoid errors, is for the code for a server setup on the PC side. In talking with Ashely, we want to test both a client and server relationship on the PC side and the opposite on the W5100 side to see which works better.
For the client mode, we first connect to IP and port of the W5100. Once connected and the connection stream obtained, the PC writes a “1” to the W5100 server. When the W5100 receives this, it will send the data for the power consumption to the PC. The PC will then get the size of the data and go through each character and store it as the sensor value. After that it closes the connection until the connection is opened again on the next second. From there, it will operate the same as it has in the code I have previously posted.
If we decide to use the server mode on the PC side, we first set up a listener with the IP address of the computer specified along with a port to connect to. We then start the listener and accept any connections. We read the connection from the client, but do not store it as we have not requested information yet. From there we operate the same as the client PC side and send a “1” to the W5100 client. We then read the data from the client and store it as the sensor value. We then close the connection and stop the listener until it starts again. Again, from there it operates the same as in the previously posted code.
Over the next week, I plan to debug our code with Ashley and have the Ethernet communications working. After that, I just have to touch up the website so it looks more professional.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home