向串口写数据时发送一个字符数组接收时回来却乱码解决方案
向串口写数据时发送一个字符数组接收时回来却乱码
请教串口读写的问题:
1.sendStr是我定义的要发送的字符数组,为什么写数据之后,收回来的数据是乱码。
2.请教一下这个读串口线程应该在初始化对话框那里OnInitDialog那里,还是打开串口OpenSerialPort那里,还是具体写数据时创建,来保证输出缓冲区一有内容就显示到界面上,这个过程中还需要创建“事件”之类的机制吗?
------解决方案--------------------
串口上数据是不知道什么时候过来的么?
线程定义在局部,光这个就有问题啊
请教串口读写的问题:
1.sendStr是我定义的要发送的字符数组,为什么写数据之后,收回来的数据是乱码。
2.请教一下这个读串口线程应该在初始化对话框那里OnInitDialog那里,还是打开串口OpenSerialPort那里,还是具体写数据时创建,来保证输出缓冲区一有内容就显示到界面上,这个过程中还需要创建“事件”之类的机制吗?
- C/C++ code
//连接串口 void CUpperendDlg::WriteData() { unsigned char sendStr[6]; memset(sendStr, 0, 6); int is_motor3_roll; int cycles; int direction = atoi(v_DIRECTION); int speed = atoi(v_SPEED); int tofloor = atoi(v_TOFLOOR); int baseStation = atoi(v_BASESTATION); tofloor == baseStation ? is_motor3_roll = 1 : is_motor3_roll = 0; cycles = atoi(v_CYCLES); sendStr[0] = 'A'; sendStr[1] = direction; sendStr[2] = speed; sendStr[3] = is_motor3_roll; sendStr[4] = cycles; sendStr[5] = 'B'; CString str; str.Format("%s", sendStr); AfxMessageBox(str); BOOL WirteState = spOperate.WritePort(sendStr, 6); if(WirteState) { AfxMessageBox(_T("向串口写数据成功")); //创建读串口线程 DWORD lpThreadId; CreateThread( (LPSECURITY_ATTRIBUTES)NULL, 0, (LPTHREAD_START_ROUTINE)GetSerialContent, GetSafeHwnd(), 0, &lpThreadId ); // }else { AfxMessageBox(_T("写数据失败!")); } }
------解决方案--------------------
串口上数据是不知道什么时候过来的么?
线程定义在局部,光这个就有问题啊