ESP8266上电时串口打印乱码原因和发送AT指令串口返回信息含义

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_38405680/article/details/84571843

因为esp8266模块上电时,默认打印波特率为74880.

其固件中通信与串口默认为115200.

所以如果把串口设置为74880,之后再上电模块打印的信息就不会有乱码了,但AT指令默认通信波特率是115200.如果在使用AT通信时,在调到115200.

 

 

可参考乐鑫提供的esp8266开发手册:

https://www.espressif.com/sites/default/files/documentation/2a-esp8266-sdk_getting_started_guide_cn.pdf

学习还得是要看官方datasheet的。

展开阅读全文

串口打印发送指令没反应

08-21

打开Demo窗体时先初始化SerialPort对象rn[code=C#]rnprivate SerialPort _ComPort = new SerialPort();rnrnprivate void InitializeObject()rnrn InitPortsToList();rnrn //初始化SerialPort对象rn this._ComPort.NewLine = "\r\n";rn this._ComPort.RtsEnable = true;rn this._ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);rnrn[/code]rn然后打开串口COM1rn[code=C#]rnprivate void btnConnect_Click(object sender, EventArgs e)rnrn //根据当前串口对象,来判断操作rn if (this._ComPort.IsOpen)rn rn //打开时点击,则关闭串口rn this._ComPort.Close();rn rn elsern rn //关闭时点击,则设置好端口,波特率后打开rn this._ComPort.PortName = this.cmbPort.Text;rn this._ComPort.BaudRate = int.Parse(this.cmbBaudrate.Text);rn tryrn rn this._ComPort.Open();rn rn catch (Exception ex)rn rn //捕获到异常信息,创建一个新的comm对象,之前的不能用了。rn this._ComPort = new SerialPort();rn this._ComPort.PortName = this.cmbPort.Text; //COM1rn this._ComPort.BaudRate = int.Parse(this.cmbBaudrate.Text); //9600rn this._ComPort.Open();rn //现实异常信息给客户。rn MessageBox.Show(ex.Message);rn rn rnrn[/code]rn打开COM1之后,就发送要打印的内容和打印机的指令给串口rn[code=C#]rn List buffer = new List();rn byte[] tmp = 10 ; //这里的10是厂家说明书里的命令 16进制是0x0A,10进制是10,表示打印并换行rnrn string content = this.txtContent.Text.Trim(); //这里是打印的内容rn byte[] dataContent = Encoding.Default.GetBytes(content);rn buffer.AddRange(dataContent);rn buffer.AddRange(tmp);rn byte[] data = buffer.ToArray();rnrn this._ComPort.Write(data, 0, data.Length);rn this._ComPort.Close();rn[/code]rnrn最后write之后,打印机没有反应,我没接触过串口编程,不知道到底问题在哪里,请哪位朋友有碰到过类似问题的解答一下,打印机是热敏打印机rn 论坛

通过串口实现AT指令发送

04-02

我想做一个串口调试工具,通过向GSM MODEM发送AT指令,实现短信的收发rn我用的是WAVECOM的短信猫,请高手们帮我看一下串口发送函数设置有没有错误,因为我发送AT指令后收到的是一些乱码rn不知道是发送格式错了还是接收有问题rn例如我在编辑框输入 [color=#0000FF]AT[/color] 后点击发送,返回乱码 [color=#0000FF]A狹Z[/color]rn输入[color=#0000FF]AT+IPR=9600 [/color]发送,返回 [color=#0000FF]A猭櫎?y柸d慀[/color]rn发送的指令如果用小写(例如发送at at+ipr=9600)返回的数据也不一样,请指教rn是不是writefile()和readfile()函数参数错了,分不够可以加rnrnvoid CMSGCOMDlg::OnSetport() [color=#FF0000] //设置串口参数[/color]rnrn SetupComm(hCom, 1024, 1024); //设置发送和接收缓冲区大小rn //设置串口信息rn DCB dcb;rn GetCommState(hCom, &dcb);rn dcb.BaudRate = 9600;rn dcb.fBinary = TRUE;rn dcb.fParity = TRUE;rn dcb.ByteSize = 8;rn dcb.Parity = ODDPARITY;rn dcb.StopBits = ONESTOPBIT;rn if(!SetCommState(hCom,&dcb))rn rn MessageBox("设置失败", "提示");rn return; rn rn if(!SetCommMask(hCom, EV_RXCHAR | EV_TXEMPTY))rn rn MessageBox("掩码设置失败", "提示");rn return;rn rn DWORD param;rn hThread = CreateThread((LPSECURITY_ATTRIBUTES)NULL, //安全属性rn 0, //初始化线程栈的大小,缺省为与主线程大小相同rn (LPTHREAD_START_ROUTINE)ThreadFunction, //线程的全局函数rn &param, //此处传入了主框架的句柄 rn 0, //创建后立即激活rn &dwThreadID); //保存新线程的idrn if (hThread == INVALID_HANDLE_VALUE)rn rn MessageBox("线程创建失败", "提示");rn return;rn rn IsFun = TRUE;rnrnrnvoid CMSGCOMDlg::OnSend() [color=#FF0000]// 发送AT指令[/color]rnrn DWORD res;rn DWORD factdata = 0;rn CString str;rn m_data.GetWindowText(str); [color=#FF0000]//str接收编辑框输入的AT指令[/color]rn str += "\r\n";rn wOverLaped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); //创建一个事件对象rn IsStop = FALSE;rn if (WriteFile(hCom, str, str.GetLength(), &factdata, &wOverLaped)) //开始发送数据rn rn IsStop = TRUE;rn MessageBox("发送成功");rn rn else if(GetLastError()==ERROR_IO_PENDING)rn rn rn res = WaitForSingleObject(wOverLaped.hEvent, 2000); //延时2秒,等待数据发送rn if (WAIT_OBJECT_0 == res) //如果事件处于有信号状态,表示发送完成rn rn IsStop = TRUE;rn rn elsern IsStop = FALSE;rn rn Sleep(1000); //延时1秒钟 rn rnrnrnDWORD ThreadFunction(LPVOID pParam)[color=#FF0000]//线程函数[/color]rnrn DWORD dwEvtMask ,dwResult;rn tOverLaped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);//创建一个事件rn while(IsFun)rn rn //等待窗口事件rn WaitCommEvent(((CMSGCOMDlg*)AfxGetMainWnd())->hCom,rn &dwEvtMask, &tOverLaped);rn //如果事件没有信号,延时0.1秒rn dwResult = WaitForSingleObject(tOverLaped.hEvent, 100);rn rn if(dwResult == WAIT_OBJECT_0) //事件对象有信号rn rn if(dwEvtMask == EV_RXCHAR) //接收到数据rn rn if(IsStop)//发送停止rn rn IsStop = FALSE;rn //发送消息,由消息处理函数接收数据rn ::PostMessage(AfxGetMainWnd()->m_hWnd, CM_RECEIVE,rn 0,(LPARAM)EV_RXCHAR);rn rn rn rn rn return 0;rnrnrn rnvoid CMSGCOMDlg::OnRecieveData(WPARAM wParam, LPARAM lParam)[color=#FF0000]//自定义消息处理函数,用于接收消息 [/color]rnrn DWORD res, factbyte;rn rn memset(DataBuffer, 0, 1024); //初始化数据缓冲区rn COMSTAT rst;rn rn ClearCommError(hCom, &res, &rst); //清空串口错误标志,记录当前通信状态rn rn rOverLaped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); //创建一个事件对象rnrn if (ReadFile(hCom, DataBuffer, rst.cbInQue, &factbyte, &rOverLaped)) //读取数据到缓冲区中rn rn DataBuffer[rst.cbInQue] = 0;rn IsStop = FALSE;rn rn elsern rn res = WaitForSingleObject(rOverLaped.hEvent, 2000);rn rn IsStop = FALSE;rn CString str = DataBuffer;rnrn m_outwnd += str + "\r\n"; [color=#FF0000]//将收到的数据显示到编辑框控件上[/color]rn UpdateData(FALSE);rn 论坛

没有更多推荐了,返回首页