小学生
最后登录1970-1-1
在线时间 小时
注册时间2017-8-29
|
问题描述:各位大佬好!根据官方的野火物联APP控制wifi例程,可以知道APP向STM32发送了命令控制了LED灯。现在我用C#做的一个上位机,能通过wifi连接到板子,但从上位机发送命令CMD_LED_1_1到板子的时候,灯不能亮,而且会返回一串乱码。我疑惑的是1,亮灯命令是否是CMD_LED_x_x 2,返回的乱码来自哪里。已经试过把void ESP8266_StaTcpClient_UnvarnishTest() 里的print语句都注释掉依然还有乱码返回。(代码中文复制粘贴后变乱码,这应该影响不大)
- //C#上位机代码
- //能够连接不能控制
- namespace wincli
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- Control.CheckForIllegalCrossThreadCalls = false;
- }
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- }
- TcpClient tcpClient;
- private void button2_Click(object sender, EventArgs e)
- {
- tcpClient = new TcpClient();
- try
- {
- tcpClient.Connect(this.textBox3.Text, Convert.ToInt32(this.textBox4.Text));
- this.textBox1.Text = "连接成功— " + DateTime.Now.ToShortTimeString() + "\r\n" +
- this.textBox1.Text;
- this.backgroundWorker1.RunWorkerAsync();
- }
- catch (Exception ex)
- {
- MessageBox.Show("连接失败!"+ex.Message);
- }
- }
- //主线程
- private void button1_Click(object sender, EventArgs e)
- {
- string message = this.textBox2.Text;
- this.textBox1.Text = "发送“"+message+"”" + DateTime.Now.ToShortTimeString() + "\r\n" +
- this.textBox1.Text;
- NetworkStream stream = tcpClient.GetStream();//管道
- byte[] byteArray = Encoding.Unicode.GetBytes(message);//把字符串转换成Byte字节
- stream.Write(byteArray,0,byteArray.Length);//发送字节数组
- }
- //异步
- private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
- {
- NetworkStream stream = tcpClient.GetStream();
- byte[] byteArray = new byte[1024];//暂存接收到的数组
- while (true)
- {
- int length = stream.Read(byteArray, 0, 1024);//会把流里的字节数组放到byteArray数组中,但后面部分有空,返回一个长度就是客户端内容的字节长度。
- string receiverMessage = Encoding.Unicode.GetString(byteArray, 0, length);//取出其中的字符串
- this.textBox1.Text = "接收到:" + receiverMessage + "-" + DateTime.Now.ToShortTimeString() + "\r\n" +
- this.textBox1.Text;
- }
- }
- }
- }
复制代码- void ESP8266_StaTcpClient_UnvarnishTest ( void )
- {
- uint8_t ucId, ucLen;
- uint8_t ucLed1Status = 0, ucLed2Status = 0, ucLed3Status = 0, ucBuzzerStatus = 0;
- char cStr [ 100 ] = { 0 }, cCh;
- char * pCh, * pCh1;
- DHT11_Data_TypeDef DHT11_Data;
-
- printf ( "\r\nÕýÔÚÅäÖÃ ESP8266 ......\r\n" );
- macESP8266_CH_ENABLE();
-
- ESP8266_AT_Test ();
-
- ESP8266_Net_Mode_Choose (AP );
- while ( ! ESP8266_CIPAP ( macUser_ESP8266_TcpServer_IP ) );//ģʽѡÔñ
- while ( ! ESP8266_BuildAP ( macUser_ESP8266_BulitApSsid, macUser_ESP8266_BulitApPwd, macUser_ESP8266_BulitApEcn ) ); //Á´½ÓÍⲿwifi
-
- ESP8266_Enable_MultipleId ( ENABLE );
-
- while ( ! ESP8266_StartOrShutServer ( ENABLE, macUser_ESP8266_TcpServer_Port, macUser_ESP8266_TcpServer_OverTime ) );//¿ªÆô·þÎñÆ÷ºÍ¶Ë¿Ú
- ESP8266_Inquire_ApIp ( cStr, 20 );//ap IP address
- printf ( "\r\n±¾Ä£¿éWIFIΪ%s£¬ÃÜÂ뿪·Å\r\nAP IP Ϊ£º%s£¬¿ªÆôµÄ¶Ë¿ÚΪ£º%s\r\nÊÖ»úÍøÂçÖúÊÖÁ¬½Ó¸Ã IP ºÍ¶Ë¿Ú£¬×î¶à¿ÉÁ¬½Ó5¸ö¿Í»§¶Ë\r\n",
- macUser_ESP8266_BulitApSsid, cStr, macUser_ESP8266_TcpServer_Port );
-
-
- strEsp8266_Fram_Record .InfBit .FramLength = 0;
- strEsp8266_Fram_Record .InfBit .FramFinishFlag = 0;
-
-
-
- while ( 1 )
- {
- if ( strEsp8266_Fram_Record .InfBit .FramFinishFlag )
- {
- USART_ITConfig ( macESP8266_USARTx, USART_IT_RXNE, DISABLE ); //½ûÓô®¿Ú½ÓÊÕÖжÏ
- strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ] = '\0';
-
- printf ( "\r\n%s\r\n", strEsp8266_Fram_Record .Data_RX_BUF );
- if ( ( pCh = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "CMD_LED_" ) ) != 0 )
- {
- cCh = * ( pCh + 8 );
-
- switch ( cCh )
- {
- case '1':
- cCh = * ( pCh + 10 );
- switch ( cCh )
- {
- case '0':
- macLED1_OFF ();
- ucLed1Status = 0;
- break;
- case '1':
- macLED1_ON ();
- ucLed1Status = 1;
- break;
- default :
- break;
- }
- break;
-
- case '2':
- cCh = * ( pCh + 10 );
- switch ( cCh )
- {
- case '0':
- macLED2_OFF ();
- ucLed2Status = 0;
- break;
- case '1':
- macLED2_ON ();
- ucLed2Status = 1;
- break;
- default :
- break;
- }
- break;
- case '3':
- cCh = * ( pCh + 10 );
- switch ( cCh )
- {
- case '0':
- macLED3_OFF ();
- ucLed3Status = 0;
- break;
- case '1':
- macLED3_ON ();
- ucLed3Status = 1;
- break;
- default :
- break;
- }
- break;
-
- default :
- break;
-
- }
-
- sprintf ( cStr, "CMD_LED_%d_%d_%d_ENDLED_END", ucLed1Status, ucLed2Status, ucLed3Status );
-
- }
-
- else if ( ( pCh = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "CMD_BUZZER_" ) ) != 0 )
- {
- cCh = * ( pCh + 11 );
-
- switch ( cCh )
- {
- case '0':
- macBEEP_OFF ();
- ucBuzzerStatus = 0;
- break;
- case '1':
- macBEEP_ON ();
- ucBuzzerStatus = 1;
- break;
- default:
- break;
- }
-
- sprintf ( cStr, "CMD_BUZZER_%d_ENDBUZZER_END", ucBuzzerStatus );
-
- }
-
- else if ( ( ( pCh = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "CMD_UART_" ) ) != 0 ) &&
- ( ( pCh1 = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "_ENDUART_END" ) ) != 0 ) )
- {
- if ( pCh < pCh1)
- {
- ucLen = pCh1 - pCh + 12;
- memcpy ( cStr, pCh, ucLen );
- cStr [ ucLen ] = '\0';
- }
- }
- else if ( strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "CMD_READ_ALL_END" ) != 0 )
- {
- DHT11_Read_TempAndHumidity ( & DHT11_Data );
- sprintf ( cStr, "CMD_LED_%d_%d_%d_ENDLED_DHT11_%d.%d_%d.%d_ENDDHT11_BUZZER_%d_ENDBUZZER_END",
- ucLed1Status, ucLed2Status, ucLed3Status, DHT11_Data .temp_int,
- DHT11_Data .temp_deci, DHT11_Data .humi_int, DHT11_Data .humi_deci,
- ucBuzzerStatus );
- }
-
-
- if ( ( pCh = strstr ( strEsp8266_Fram_Record .Data_RX_BUF, "+IPD," ) ) != 0 )
- {
- ucId = * ( pCh + strlen ( "+IPD," ) ) - '0';
- ESP8266_SendString ( DISABLE, cStr, strlen ( cStr ), ( ENUM_ID_NO_TypeDef ) ucId );
- }
-
- strEsp8266_Fram_Record .InfBit .FramLength = 0;
- strEsp8266_Fram_Record .InfBit .FramFinishFlag = 0;
-
- USART_ITConfig ( macESP8266_USARTx, USART_IT_RXNE, ENABLE ); //ʹÄÜ´®¿Ú½ÓÊÕÖжÏ
-
- }
-
- }
-
-
- }
复制代码
|
-
|