- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗯,我正在开发通过蓝牙与另一个 PCB 进行通信的应用程序。它由 3 个搜索栏组成,可调节 PCB 的 3 个参数值。
结构是这样的:
1. Send petition string for read the first value
2. Receive the first value
3. Send petition string for read the second value
4. Receive the second value
5. Send petition string for the third value
6. Receive the third value
我已经做到了,就像我这样的新手可以做到最好的方式。它可以工作,但有时会崩溃,所以,我知道还有其他方法可以使其完美工作。
所以,这是对一位慷慨的人的请求,希望查看代码并帮助我。
当我们按下“读取值”按钮时,就会发生这样的情况。必须注意的是,我使用可运行处理程序在传输之间等待 1 秒,以便让应用程序读取每个请求的答案:
public void receiveValues() {
/**Petition string that is sent to the PCB to request the variable's value*/
final String message_full1 = 2b e1 b4 e9 ff 1f b5; //variable 1
final String message_full2 = 2b e1 b8 e9 ff 1f b3; //variable 2
final String message_full3 = 2b e1 bc e9 ff 1f b1; //variable 3
final String message_full4 = 2b e0 bc f3 ff 1f 7c; //save request
final String message_full5 = 2b e0 be f3 ff 1f 7a; //save status
/*Send the first string to the PCB*/
byte[] send1 = message_full1.getBytes();
GlobalVar.mTransmission.write(send1);
/**Delay to wait until it receives the answer to the petition above*/
read1_handler.postDelayed(new Runnable() {
@Override
public void run() {
/**Read write confirmation after 1s = 1000ms*/
String inpuRead = "2b 00 ff fe c7 80"; //This string is what I receive as an answer via bluetooth
/**We check that the received string is not null, to avoid program crash*/
if (inpuRead != null) { //|| !inpuRead.equals("")) {
/*If it nos null, then we call the next function*/
int splitInt = splitReceivedString (inpuRead); //This function is declared below and extracts from the string , only the chars that we need.
receive1 = splitInt;
Toast.makeText(getApplicationContext(), "Loading values", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Communication error", Toast.LENGTH_SHORT).show();
}
}
}, 1000);
/**Delay to wait to send de second petition string*/
write2_handler.postDelayed(new Runnable() {
@Override
public void run() {
/**write message 2 after 1s = 1000ms*/
byte[] send2 = message_full2.getBytes();
GlobalVar.mTransmission.write(send2);
}
}, 2000);
/**Delay to wait until it receives the answer to the second petition string*/
read2_handler.postDelayed(new Runnable() {
@Override
public void run() {
/**Read write confirmation after 1s = 1000ms*/
String inpuRead = "2b 00 ff fe c7 80";
if (inpuRead != null) {
int splitInt = splitReceivedString (inpuRead);
receive2 = splitInt;
}
else {
Toast.makeText(getApplicationContext(), Communication error, Toast.LENGTH_SHORT).show();
}
}
}, 3000);
/**Delay to wait to send de third petition string*/
write3_handler.postDelayed(new Runnable() {
@Override
public void run() {
/**write message 3 after 1s = 1000ms*/
byte[] send3 = message_full3.getBytes();
GlobalVar.mTransmission.write(send3);
}
}, 4000);
/**Delay to wait until it receives the answer to the third petition string*/
read3_handler.postDelayed(new Runnable() {
@Override
public void run() {
/**Read write confirmation after 1s = 1000ms*/
String inpuRead = "2b 00 ff fe c7 80";
if (inpuRead != null) {
int splitInt = splitReceivedString (inpuRead);
receive3 = splitInt;
/**Set loaded values on seekbars*/
bar1.setProgress(receive1);
bar2.setProgress(receive2);
bar3.setProgress(receive3);
/**Message indicating the end of the transmission*/
Toast.makeText(getApplicationContext(), "Values loaded!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Communication error", Toast.LENGTH_SHORT).show();
}
}
}, 5000);
/**This makes a save request on the pCB*/
write4_handler.postDelayed(new Runnable() {
@Override
public void run() {
/**write message 3 after 1s = 1000ms*/
byte[] send4 = message_full4.getBytes();
GlobalVar.mTransmission.write(send4);
}
}, 6000);
/**This request a save statos on the PCB*/
write5_handler.postDelayed(new Runnable() {
@Override
public void run() {
/**write message 3 after 1s = 1000ms*/
byte[] send5 = message_full5.getBytes();
GlobalVar.mTransmission.write(send5);
/**Reset out string buffer to zero*/
GlobalVar.mOutStringBuffer.setLength(0);
}
}, 7000);
}
/**
* FUNCTION THAT SPLITS THE RECEIVED STRING TO GET THE DESIRED VALUES
*/
private int splitReceivedString (String s) { //For example, s = 2b 00 ff fe c7 80
StringTokenizer tokens = new StringTokenizer(s," ");
String one = tokens.nextToken();
String two = tokens.nextToken();
String three = tokens.nextToken();
String four = tokens.nextToken();
String five = tokens.nextToken();
String six = tokens.nextToken();
/**The next strings are whose got the seekbar's value*/ //f.e: "fffec780"
received_hexValue = three + four + five + six;
received_hexValue = received_hexValue.trim();
/**Conversion from hex to int to set the seekbar's values*/
int_value_receive = (int)Long.parseLong(received_hexValue, 16);
int_value_receive = -200000 - int_value_receive;
newIntValue = (int_value_receive * 100) / (200000 * (-1));
return newIntValue; //For this hex value, the int value to introduce in the seekbar is "60"
}
已更新 - 添加了问题
我想做的第一件事,但我不知道怎么做,当它进入 if
时,如果这是真的,那么它必须继续执行代码,但如果不是真的,那么它应该退出该函数并停止执行代码。但按照我的做法,它会继续执行。
我的意思是我有时会遇到的崩溃是有时,而不是以这种方式获得结构:
1. Send petition string for read the first value
2. Receive the first value
3. Send petition string for read the second value
4. Receive the second value
...
它的作用如下:
1. Send petition string for read the first value
2. Send petition string for read the second value
3. Receive the first value
...
这就是它崩溃的地方。
最佳答案
如果您希望轮流执行这些任务,则不需要所有 7 个额外线程。只需将它们组合到一个 Runnable
中,事情就会变得容易得多。
我的意思是,您所有的 /**Delay to wait until...*/
注释都是错误的,您在上一个操作开始后等待 1 秒。因此,您只是在“发送请愿字符串以读取第二个值”之前没有完成“接收第一个值”,因为它花费了超过 1 秒的时间。
示例:
您的代码已简化:
private Handler read1_handler = new Handler();
private Handler write2_handler = new Handler();
private Handler read2_handler = new Handler();
public void receiveValues() {
/**Delay to wait until it receives the answer to the petition above*/
read1_handler.postDelayed(new Runnable() {
@Override
public void run() {
String inpuRead = "2b 00 ff fe c7 80"; //This string is what I receive as an answer via bluetooth
if (inpuRead != null) { //|| !inpuRead.equals("")) {
int splitInt = splitReceivedString (inpuRead);
receive1 = splitInt;
Toast.makeText(getApplicationContext(), "Loading values", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Communication error", Toast.LENGTH_SHORT).show();
}
}
}, 1000);
/**Delay to wait to send de second petition string*/
write2_handler.postDelayed(new Runnable() {
@Override
public void run() {
byte[] send2 = message_full2.getBytes();
GlobalVar.mTransmission.write(send2);
}
}, 2000);
/**Delay to wait until it receives the answer to the second petition string*/
read2_handler.postDelayed(new Runnable() {
@Override
public void run() {
String inpuRead = "2b 00 ff fe c7 80";
if (inpuRead != null) {
int splitInt = splitReceivedString (inpuRead);
receive2 = splitInt;
}
else {
Toast.makeText(getApplicationContext(), Communication error, Toast.LENGTH_SHORT).show();
}
}
}, 3000);
}
具有正确执行顺序的代码:
private Handler handler = new Handler();
//TODO: don't call functions read1, write2 etc, call it something like "readSomeValue" where "SomeValue" is what you're trying to read
private void read1() throws IOException {
String inpuRead = "2b 00 ff fe c7 80"; //This string is what I receive as an answer via bluetooth
if (inpuRead != null) { //|| !inpuRead.equals("")) {
int splitInt = splitReceivedString (inpuRead);
receive1 = splitInt;
Toast.makeText(getApplicationContext(), "Loading values", Toast.LENGTH_LONG).show();
}
else {
throw new IOException("Error in read1");
}
}
private void write2() {
byte[] send2 = message_full2.getBytes();
GlobalVar.mTransmission.write(send2);
}
private void read2() throws IOException {
String inpuRead = "2b 00 ff fe c7 80";
if (inpuRead != null) {
int splitInt = splitReceivedString (inpuRead);
receive2 = splitInt;
}
else {
throw new IOException("Error in read2");
}
}
public void receiveValues() {
handler.post(new Runnable() {
@Override
public void run() {
try {
read1();
read2();
read3();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Communication error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
关于java - 构造此函数的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18633853/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!