基于FT2232官方API开发C驱动程序,封装DLL,承接JAVA UI,JNI(2)

继续前文。
摘要由CSDN通过智能技术生成

继续前文。

        7、IR

        根据之前的JTAG框图,下一步就是选择寄存器,也就是IR in。

	JNIEXPORT jstring JNICALL Java_copy_FTDI_1copy_runIRcommand
	(JNIEnv* env, jobject, jstring line) {
		const char* cStr = env->GetStringUTFChars(line, nullptr);
		int linelength = strlen(cStr);
		int writebytecnt = 999;
		int writecnt = 0;
		std::string cap = "";

		dwNumBytesToSend = 0;
		byOutputBuffer[dwNumBytesToSend++] = 0x4B;
		byOutputBuffer[dwNumBytesToSend++] = 0x03;
		byOutputBuffer[dwNumBytesToSend++] = 3;

		if (linelength-1 > 0) {
			writebytecnt = ((linelength - 1) / 8);

			if (writebytecnt > 0) {
				int length = writebytecnt - 1;
				byOutputBuffer[dwNumBytesToSend++] = 0x39;
				byOutputBuffer[dwNumBytesToSend++] = (length & 0xFF);
				byOutputBuffer[dwNumBytesToSend++] = ((length / 256) & 0xFF);
				do {
					std::string str = std::string(cStr).substr(linelength - ((writecnt + 1) * 8), 8);
					BYTE num = std::stoi(str.c_str(), nullptr, 2);
					byOutputBuffer[dwNumBytesToSend++] = num;
					writecnt++;
				} while (writecnt < writebytecnt);
			}
		}

		int remainbitcnt = ((linelength - 1) % 8);
		if (remainbitcnt > 0) {
			byOutputBuffer[dwNumBytesToSend++] = 0x3B;
			byOutputBuffer[dwNumBytesToSend++] = remainbitcnt - 1;
			std::string str = std::string(cStr).substr(1, remainbitcnt);
			BYTE num = std::stoi(str.c_str(), nullptr, 2);
			byOutputBuffer[dwNumBytesToSend++] = num;
		}

		//get last bit
		std::string str = std::string(cStr).substr(0, 1);
		BYTE num = std::stoi(str.c_str(), nullptr, 2);
		printf("IR last num =%s _ %d\n", str.c_str(), num);
		byOutputBuffer[dwNumBytesToSend++] = 0x6E;
		byOutputBuffer[dwNumBytesToSend++] = 0x03;
		byOutputBuffer[dwNumBytesToSend++] = (num*128 + 3);

		byOutputBuffer[dwNumBytesToSend++] = 0x87;

		ftStatus |= FT_Write(ftHandle, byOutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
		if (ftStatus != FT_OK) {
			cap += "device ir write fail!!!!";
		}
		dwNumBytesToSend = 0;

		//read buffer
		int byteCnts = (linelength % 8) == 0 ? linelength / 8 : linelength / 8 + 1;
		int targetBytes = ((linelength - 1) % 8) == 0 ? (((linelength - 1) / 8) + 1) : (((linelength - 1) / 8) + 2);
		int RemainingBits = (byte)(8 - ((linelength - 1) % 8));

		//do
		//{
		//	ftStatus = FT_GetQueueStatus(ftHandle, &dwNumBytesToRead);
		//} while ((dwNumBytesToRead == 0) && (ftStatus == FT_OK));//or Timeout
		ftStatus |= FT_Read(ftHandle, &byInputBuffer, targetBytes, &dwNumBytesRead);

		printf("capture1 = %d\n", dwNumBytesRead);
		printf("capture3 = %d\n", targetBytes);

		std::string binaryStr = "";
		if (RemainingBits < 8) {

			//binaryStr = std::bitset<8>(byInputBuffer[dwNumBytesRead]).to_string();
			binaryStr = std::string(binaryStr).substr(0, remainbitcnt);
			//cap += binaryStr;
			//printf("capture00_%2d = %s\n", 00, cap.c_str());

			binaryStr = std::bitset<8>(byInputBuffer[dwNumBytesRead - 1]).to_string();
			binaryStr = std::string(binaryStr).substr(3, 1);
			cap += binaryStr;
			printf("capture11_%d = %s\n", 11, cap.c_str());

			binaryStr = std::bitset<8>(byInputBuffer[dwNumBytesRead - 2]).to_string();
			binaryStr = std::string(binaryStr).substr(0, remainbitcnt);
			cap += binaryStr;
			printf("capture22_%d = %s\n", 22, cap.c_str());

			for (int i = dwNumBytesRead - 3; i >= 0; i--) {
				binaryStr = std::bitset<8>(byInputBuffer[i]).to_string();
				cap += binaryStr;
				printf("capture4_%d = %s\n", i, cap.c_str());
			}
			printf("\ncapture333 = %s\n", cap.c_str());
		}
		else {
			binaryStr = std::bitset<8>(byInputBuffer[dwNumBytesRead - 1]).to_string();
			binaryStr = std::string(binaryStr).substr(3, 1);
			cap += binaryStr;
			printf("capture11_%d = %s\n", 11, cap.c_str());

			for (int i = dwNumBytesRead - 2; i >= 0; i--) {
				binaryStr = std::bitset<8>(byInputBuffer[i]).to_string();
				cap
  • 10
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 JNI 调用 Windows API 的示例代码: 首先,我们需要编写一个 C 文件,实现我们需要调用的 Windows API 函数,并将其编译成 DLL。例如,我们需要调用 MessageBoxA 函数,可以编写以下代码: ``` #include <windows.h> #include <stdio.h> #include "jni.h" JNIEXPORT void JNICALL Java_com_example_MessageBox_messageBox(JNIEnv* env, jobject obj, jstring message) { const char* utfMessage = (*env)->GetStringUTFChars(env, message, NULL); MessageBoxA(NULL, utfMessage, "Message", MB_OK | MB_ICONINFORMATION); (*env)->ReleaseStringUTFChars(env, message, utfMessage); } ``` 这个函数接受一个字符串参数,调用 Windows API 的 MessageBoxA 函数,并显示消息框。 接下来,我们需要将这个 C 文件编译成 DLL。这里使用 MinGW-w64 编译器,可以使用以下命令: ``` x86_64-w64-mingw32-gcc -shared -o MessageBox.dll MessageBox.c ``` 这将生成一个名为 MessageBox.dllDLL 文件。 现在,我们可以在 Java 中使用 JNI 调用这个 DLL。首先,我们需要加载这个 DLL: ``` System.loadLibrary("MessageBox"); ``` 然后,我们可以调用这个 DLL 中的函数: ``` public class MessageBox { static { System.loadLibrary("MessageBox"); } public static native void messageBox(String message); public static void main(String[] args) { MessageBox.messageBox("Hello from JNI!"); } } ``` 这个 Java 类中包含一个名为 messageBox 的本地方法,它调用了我们刚刚编写的 C 函数。在 main 方法中,我们调用这个本地方法,传入一个字符串参数,这个字符串将被显示在消息框中。 这样,我们就完成了使用 JNI 调用 Windows API 的示例。需要注意的是,这个示例中使用了 MinGW-w64 编译器,如果使用其他编译器,可能需要进行相应的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值