tangtangllll
2017-05-05 07:48使用API函数编写串口上位机,只能发送第一组数据
1在此贴出程序,希望实现按下回车发送一次angle。但是现在只能发送第一个数据。是因为有标志位没有恢复吗?
bool Initserial()
{
hCom = CreateFile("COM7", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
SetupComm(hCom, 5, 5);//输入/输出缓冲区大小
COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout = 1000;
TimeOuts.ReadTotalTimeoutMultiplier = 500;
TimeOuts.ReadTotalTimeoutConstant = 5000;
TimeOuts.WriteTotalTimeoutMultiplier = 500;
TimeOuts.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(hCom, &TimeOuts);
DCB dcb;
GetCommState(hCom, &dcb);
dcb.BaudRate = 9600; //波特率为9600
dcb.ByteSize = 8; //每个字节有8位
dcb.Parity = NOPARITY; //无奇偶校验位
dcb.StopBits = ONESTOPBIT; //一个停止位
SetCommState(hCom, &dcb);
PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
return true;
}
int main(int argc, char** argv)
{
Initserial();
cout<<"DCB2"<<endl;
namedWindow("a",CV_WINDOW_AUTOSIZE);
angle=40;
while (1)
{
int key = cvWaitKey(30);
if (key==13)//按下回撤键执行
{
angle=angle+20;
d[0]=0x30+angle/100;
d[1]=0x30+(angle-(angle/100)*100)/10;
d[2]=0x30+angle%10;
d[3]=0x0D;
d[4]=0x0A;
WriteFile(hCom, &d, dwBytesWritten, &dwBytesWritten, &m_osWrite);
cout<<"d"<<d<<endl;
} }
}
- 点赞
- 回答
- 收藏
- 复制链接分享
0条回答
为你推荐
- 请问如何将第一行dataframe数据设置为列索引?而不使用默认索引
- python
- 数据挖掘
- 1个回答
- 编写一个函数,实现将一个带头结点的单向链表数据逆置,代码怎么写呀?
- c语言
- 2个回答
- c语言编程问题:编写两个函数,求一个3*3矩阵对角线元素之和。
- c语言
- 1个回答
- 基于keras,使用imagedatagenerator.flow函数读入数据,训练集ACC极低
- 深度学习
- 神经网络
- 1个回答
- 怎么在一个子函数里使用全局变量作为参数???
- c
- stm32
- 单片机
- 10个回答
换一换