如何解决用D2xx写入数据
我有一个简单的程序,尝试使用FTDI D2xx库通过串行端口写入数据。
程序仅打开端口,并尝试以一定速率写入数据。偶尔在示波器上我会看到一个1缺失的字样。我还只能通过调用SetBreakOn,然后再调用SetBreakOff来查看数据。如果不这样做,则看不到示波器上的任何数据。
我在这里做错了吗?在我的循环中,一旦发送了数据,是否会一直发送0直到我写回缓冲区?
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <ftd2xx.h>
#include <stdlib.h>
#include "string.h"
#include <unistd.h>
int main(int argc,char *argv[])
{
uint32_t numDevs;
FT_HANDLE FT_handle;
FT_STATUS FT_status; // status of the FT 232 chip
//Open Device
FT_STATUS ftStatus;
char * value = "FT4T6TXN";
ftStatus = FT_OpenEx(value,FT_OPEN_BY_SERIAL_NUMBER,&FT_handle);
if(ftStatus == FT_OK){
printf("successful open!\n");
}else{
printf("failure open! %d\n",ftStatus);
}
UCHAR Mask = 0xFF; //all output
UCHAR Mode = 0x04; //Selects the Synchronous Bit Bang Mode
ftStatus = FT_SetBitMode(FT_handle,Mask,Mode);
if (ftStatus == FT_OK) {
// synchronous Bit bang Mode enabled
printf("sync bit mode!\n");
}
else {
// FT_SetBitMode Failed!
printf("setBitModeFailed!\n");
return -1;
}
DWORD Baudrate;
Baudrate = 366;
FT_status = FT_SetBaudrate(FT_handle,Baudrate);
if(FT_status != FT_OK){
printf("BAUD RATE SET Failed! \n");
return -1;
}
uint32_t bytesWritten = 0;
UCHAR Buffer = 0x01; // data to be written to the port
//set flow control
FT_status = FT_SetFlowControl(FT_handle,FT_FLOW_RTS_CTS,NULL,NULL);
if(FT_status != FT_OK){
printf("flow not okay\n");
}
//Begin Looping Inf
while(true){
//I was unable to see Data without calling this?
FT_SetBreakOn(FT_handle);
FT_SetBreakOff(FT_handle);
//WRITE
FT_status = FT_Write( FT_handle,// handle to the chip
&Buffer,// address of the data
sizeof(Buffer),// Size of the Buffer
&BytesWritten // Number of bytes written
);
sleep(1);
if(FT_status != FT_OK){
printf("write not successful! %d \n",FT_status);
}
}
FT_Close(FT_handle); // Close the Serial port connection
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。