|
关于C# 上位机 与ARM 下位机 UDP连接问题
[复制链接]
本帖最后由 LS_lpy 于 2014-1-14 17:59 编辑
上位机界面
上位机UDP服务器C#代- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.Collections;
- using System.IO;
- namespace ceshi2
- {
- public partial class Form1 : Form
- {
- struct USER //存放客户信息
- {
- public EndPoint endpoint;
- public string Name;
- }
- ArrayList clientlist; //用户链表
- Socket server; //服务器
- byte[] byteData = new byte[1024];
- public Form1()
- {
- CheckForIllegalCrossThreadCalls = false;
- clientlist = new ArrayList();
- InitializeComponent();
- toolStripStatusLabel1.Text = null; // 状态栏 可无视
- textBoxIP.Text = "192.168.1.2"; //本地IP
- textBoxPORT.Text = "1026"; //本地端口
- }
- private string GetIP() //获取本地IP 无视
- {
- try
- {
- string serverIP = textBoxIP.Text;
- return serverIP;
- }
- catch
- {
- return "192.168.1.2";
- }
- }
- private int GetPort() //本地端口 无视
- {
- try
- {
- string serverPort = textBoxPORT.Text;
- return Convert.ToInt32(serverPort);
- }
- catch
- {
- return 1026;
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void buttonStart_Click(object sender, EventArgs e) //开始服务键 事件
- {
- try
- {
- server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- IPEndPoint serverPoint = new IPEndPoint(IPAddress.Parse(this.GetIP()), this.GetPort());
- server.Bind(serverPoint);
- IPEndPoint senderIP = new IPEndPoint(IPAddress.Any, 0);
- EndPoint mysender = (EndPoint)senderIP;
- byteData = new byte[1024];
- server.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref mysender, new AsyncCallback(myReceive), mysender);
- toolStripStatusLabel1.Text = "服务已开启";
- if (listBoxKH.Items.Count <= 0) //可无视
- {
- buttonSend.Enabled = false;
- buttonGB.Enabled = false;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message,"UdpTcpServer",MessageBoxButtons.OK,MessageBoxIcon.Error);
- }
- }
- private void myReceive(IAsyncResult Data) //接受信息 主要
- {
- try
- {
- IPEndPoint SenderIP = new IPEndPoint(IPAddress.Any, 0);
- EndPoint mysender = (EndPoint)SenderIP;
- server.EndReceiveFrom(Data, ref mysender);
- // string Msg = Encoding.Unicode.GetString(byteData);
- string Msg = Encoding.UTF8.GetString(byteData);
-
- string[] QT = Msg.Split(new char[] { '|' });
- if (QT[0] == "login") //login|***** 表示登入 **** 用户名
- {
- USER user = new USER();
- user.endpoint = mysender;
- user.Name = QT[1];
- clientlist.Add(user);
- listBoxKH.Items.Add(QT[1]);
- richTextBox1.AppendText("login: " + user.Name+Environment.NewLine);
- buttonSend.Enabled = false;
- buttonGB.Enabled = false;
- }
- else if (QT[0] == "logout")// 用户退出 无视
- {
- int i=0;
- foreach (USER user in clientlist)
- {
- string strEndPoint = user.endpoint.ToString();
- string strSender = mysender.ToString();
- if (strEndPoint.Equals(strSender))
- {
- clientlist.RemoveAt(i);
- listBoxKH.Items.RemoveAt(i);
- richTextBox1.AppendText("logout: " + user.Name + Environment.NewLine);
- }
- ++i;
- }
- //string strMsg = QT[1];
- // richTextBox1.AppendText(strMsg);
- }
- else if (QT[0] == "talk") // 交谈信息 无视先
- {
- richTextBox1.AppendText(QT[1]);
- string message = "talk|" + QT[1];
- // byteData = Encoding.Unicode.GetBytes(message);
- byteData = Encoding.UTF8.GetBytes(message);
- server.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, mysender, new AsyncCallback(mySend), mysender);
- }
- byteData = new byte[1024];
- server.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref mysender, new AsyncCallback(myReceive), mysender);
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message, "UdpTcpServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void mySend(IAsyncResult Data) // 发送信息
- {
- try
- {
- server.EndSend(Data);
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message, "UdpTcpServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void buttonSend_Click(object sender, EventArgs e) //发送键事件
- {
- if (listBoxKH.SelectedItems.Count > 0)
- {
- if (richTextBox2.Text.Length > 0)
- {
- string message = "talk|" + "server:" + richTextBox2.Text + "\n";
- byte[] DataSend = new byte[1024];
- // DataSend = Encoding.Unicode.GetBytes(message);
- DataSend = Encoding.UTF8.GetBytes(message);
- foreach (string ToUser in listBoxKH.SelectedItems)
- {
- foreach (USER user in clientlist)
- {
- if (user.Name.ToString().Trim() == ToUser.Trim())
- {
- server.BeginSendTo(DataSend, 0, DataSend.Length, SocketFlags.None, user.endpoint, new AsyncCallback(mySend), user.endpoint);
- }
- }
- }
- richTextBox1.AppendText("Server:" + richTextBox2.Text + Environment.NewLine);
- richTextBox2.Text = null;
- }
- else
- {
- MessageBox.Show("不能发送空信息!");
- }
- }
- else
- {
- MessageBox.Show("请选择发送的用户!");
- }
- buttonGB.Enabled = false;
- buttonSend.Enabled = false;
-
- }
- private void buttonGB_Click(object sender, EventArgs e) //广播键 事件 无视先
- {
- string message = "talk|" + "Server:" + richTextBox2.Text + "\n";
- byte[] DataSend = new byte[1024];
- // DataSend = Encoding.Unicode.GetBytes(message);
- DataSend = Encoding.UTF8.GetBytes(message);
- foreach (USER user in clientlist)
- {
- EndPoint strEndPiont = user.endpoint;
- server.BeginSendTo(DataSend, 0, DataSend.Length, SocketFlags.None, strEndPiont, new AsyncCallback(mySend), strEndPiont);
- }
- richTextBox1.AppendText("Server:" + richTextBox2.Text + Environment.NewLine);
- richTextBox2.Text = null;
- DataSend = null;
- buttonGB.Enabled = false;
- buttonSend.Enabled = false;
- }
- private void richTextBox2_TextChanged(object sender, EventArgs e) // 无视
- {
- if (richTextBox2.Text !=null)
- {
- buttonGB.Enabled = true;
- buttonSend.Enabled = true;
- }
- else
- {
- buttonGB.Enabled = false;
- buttonSend.Enabled = false;
- }
- }
- }
- }
复制代码
码:
下位机 ARM代码:
#include "string.h"
#include "inc/lm3s9b96.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "inc/hw_i2c.h"
#include "driverlib/i2c.h"
#include "driverlib/ethernet.h"
#include "driverlib/flash.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/uart.h"
#include "driverlib/watchdog.h"
#include "utils/locator.h"
#include "utils/lwiplib.h"
#include "utils/uartstdio.h"
#include "utils/ustdlib.h"
#include "drivers/set_pinout.h"
#include "utils/uartstdio.h"
#include "drivers/kitronix320x240x16_ssd2119_8bit.h"
#define SYSTICKHZ 100
#define SYSTICKMS (1000 / SYSTICKHZ)
#define SYSTICKUS (1000000 / SYSTICKHZ)
#define SYSTICKNS (1000000000 / SYSTICKHZ)
unsigned long ulUser0,ulUser1;
unsigned char pucMACArray[8];
unsigned char UdpData[]="login|woshica";
struct udp_pcb *Pcb1;
struct ip_addr ipaddr1;
struct pbuf *p;
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
void
SysTickIntHandler(void)
{
lwIPTimer(SYSTICKMS);
}
void AnimTimerIntHandler(void)
{
TimerIntClear(TIMER2_BASE, TIMER_TIMA_TIMEOUT);
}
void lwIPHostTimerHandler(void)
{
}
void init(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
PinoutSet();
Kitronix320x240x16_SSD2119Init();
SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeEthernetLED(GPIO_PORTF_BASE,GPIO_PIN_2|GPIO_PIN_3);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_3);
SysTickPeriodSet(SysCtlClockGet()/SYSTICKHZ);
SysTickEnable();
SysTickIntEnable();
IntMasterEnable();
FlashUserGet(&ulUser0,&ulUser1);
pucMACArray[0]=((ulUser0>>0)&0xff);
pucMACArray[1]=((ulUser0>>8)&0xff);
pucMACArray[2]=((ulUser0>>16)&0xff);
pucMACArray[3]=((ulUser1>>0)&0xff);
pucMACArray[4]=((ulUser1>>8)&0xff);
pucMACArray[5]=((ulUser1>>16)&0xff);
lwIPInit(pucMACArray,0xC0A80103,0,0,IPADDR_USE_STATIC);
}
int main(void)
{
init();
p=pbuf_alloc(PBUF_RAW,sizeof(UdpData),PBUF_RAM);
p->payload=(void*)UdpData;
p->flage=NULL;
IP4_ADDR(&ipaddr1,192,168,1,2);
Pcb1=udp_new();
udp_bind(Pcb1,IP_ADDR_ANY,1025); //本地端口 192.168.1.3:1025
udp_connect(Pcb1,&ipaddr1,1026); //连接PC 192.168.1.2:1026
while(1)
{
udp_send(Pcb1,p);//循环发送login|woshica 到192.168.1.2 : 1026
SysCtlDelay(SysCtlClockGet()/3);
}
}
结果发现C#上位机与TCP调试助手能信息互发,ARM和TCP调试助手也能信息互发 但是C#与ARM连不上? 大神给看看 问题出哪了
|
|