Receive string from processing to arduino - java

I want to receive a string from processing to arduino. I am writing a string(Basically an array of single digits in the form of string eg: <0213> is an array: 0 2 1 3). I am able to write it to port but how do I receive it on arduino? Following code is to send to arduino.
int[] send={0,2,3,1};
myPort =new Serial(this,"/dev/ttyACM0", 9600);
String theStg = "<" + nf(send[0], 3) +
" " + nf(send[1], 3) +
" " + nf(send[2], 3) +
" " + nf(send[3], 3) +
">";
myPort.write(theStg);

if you were using a string of char*, rather than the String Class, then you could use avrlibc's strtok() command.
This has been addressed previously(click here)
And here is a nice function you can simply add to your code to do the equivalent of the strtok(), but on a String object.

Related

I can't set a string in a TextView in an Android app (using Java)

I'm building a simple calculator Android app in Java that will receive 2 numbers as inputs and when the user presses one of the 4 action buttons (+, -, *, /) the exercise and it's solution will appear in the bottom of the screen inside a TextView in this format:
{num1} {action} {num2} = {solution}
I tried to declare a string and form the exercise's string in it and in the end I used "setText" to change the TextView but instead of showing the full exercise when I run the app it shows something like "androidx.appcompat.widget.AppCom".
Here is an example for the string I form when the user clicks on the + button:
exerciseStr = etNum1.toString() + " + " + etNum2.toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
Does anybody know what the issue may be?
You should call getText() befor calling toString():
exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
Change it to like this.
exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));

How to compare actual message with expected message when the lines in actual message are changing

I am trying to compare the below messages through java-selenium.But the lines are changing in the actual message during execution.Please let me know how to handle it.
Ex:
String expMsg="Line1.\n"
+ "Line2\n"
+ "Line3\n"
+ "Line4\n"
+ Line5\n" + "\n"
+ "Line6."
String actMsg="Line1.\n"
+ "Line3\n"
+ "Line2\n"
+ "Line5\n"
+ Line4\n" + "\n"
+ "Line6."
I tried the following but it is failing :
if(actMsg.contains(expMsg){
System.out.println("both the messages are same")
}
Please let me know how to resolve this.
Convert your Strings to Streams
Arrays.stream(actMsg.split("\n"));
And then compare streams
Here is how I solved your example:
List<String> expMsg = Arrays.asList("Line1", "Line2", "Line3", "Line4", "Line5", "Line6");
String actMsg = "Line1.\n"
+ "Line3\n"
+ "Line2\n"
+ "Line5\n"
+ "Line4\n" + "\n"
+ "Line6.";
boolean containsAll = expMsg.stream()
//select only elements which are in actMsg
.filter(actMsg::contains) //e -> actMsg.contains(e)
//all strings from expMsg should be present
.count() == expMsg.size();
System.out.println(containsAll);
Split all lines from expMsg and check if all line are in actMsg.
AS per my understanding, the order of actualMessage does not matter, you just need to check all the expectedMessage is present in the actualMessage ? In this case save each line in a separate variable/array then verify each expectedMessage is present in actual using contains.
`
for(i=1, i<6, i++) {
if(actMsg.contains(expMsg+i){
System.out.println("both the messages are same")
}else
System.out.println("both the messages are not same")
}
`
Let me know if this works

GWT Java calculate days between two dates on the server side

I am using GWT java. I have a report on the client side that I want to export to csv. So I am trying to create the report on the server side to pass back to the client side as a csv file so the user can store the csv file in their selected destination.
The following code works on the client side; however, does not work on the server side (i.e., "Print 6." is displayed and "Print 7." is not displayed on the server side). There is no error message. The dates are "2016-11-09" and "2000-02-02".
System.out.println(todays_date);
System.out.println(pack.getDob());
System.out.println("Print 6.");
float diffDOB = (CalendarUtil.getDaysBetween(pack.getDob(), todays_date));
System.out.println("Print 7.");
I was talking about this with a fiend, who is not a programmer, and she said why not just send the MS Excel formula. Brilliant! So I have removed all the calclations and am now sending:
fileContent.append(pack.getSurname() + "," + pack.getFirstname() + "," + pack.getScout_no() + "," +
pack.getgroup() + "," + pack.getDob() + "," + '"' + "=ROUNDDOWN(((TODAY()-E"+row+")/365),1)" + '"' + "," +
pack.getstartDate() + "," + '"' + "=ROUNDDOWN(((TODAY()-G"+row+")/365),1)" + '"' + "," +
'"' + "=EDATE(E"+row+",216)" + '"' + "\n");
The only issue now is that spaces in the string fields appear as + in the output file.

How will I be able to printout the captured packets using pcap.loop() with a parameter of Pcap.LOOP_INFINITE into the JTextArea?

I'm quite new to JNetPcap and I'm still finding my way around with it, I'm trying to build a Packet sniffer for my project, Lately I'm trying printout packet information into a JTextArea by appending the information from a pcap.loop() that I am using, but when I set the first parameter using a specific integer value let say 5 the pcap.loop() outputs 5 packets that had been captured, Now what I want is to continuously capture and output the packet until I press the button stop. The syntax below shows the Packet handler.
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
public void nextPacket(PcapPacket packet, String user) {
// System.out.printf is included to check if my code works in a non GUI fashion
System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n",
new Date(packet.getCaptureHeader().timestampInMillis()),
packet.getCaptureHeader().caplen(), // Length actually captured
packet.getCaptureHeader().wirelen(), // Original length
user // User supplied object
);
Date a = new Date(packet.getCaptureHeader().timestampInMillis());
int b = packet.getCaptureHeader().caplen();
int c = packet.getCaptureHeader().wirelen();
String d = user;
pcktTextArea.append("Received packet at " + a + " caplen=" + Integer.toString(b) + " len=" + Integer.toString(b) + user + "\n" );
pcktTextArea.setForeground(Color.red);
pcktTextArea.setFont(font);
}
};
Now this bit here is my pcktTextArea which I use append to print out the information in the textarea:
pcktTextArea.append("Received packet at " + a + " caplen=" + Integer.toString(b) + " len=" + Integer.toString(b) + user + "\n" );
pcktTextArea.setForeground(Color.red);
pcktTextArea.setFont(font);
And Finally the Pcap.loop which I am having trouble with, if I replace that i with let say 5 it does get printed in the JTextArea but when I put the Pcap.LOOP_INFINTE it only prints the information through console but not in GUI JTextArea:
int i = Pcap.LOOP_INFINITE;
pcap.loop(i , jpacketHandler, " ");
/***************************************************************************
* Last thing to do is close the pcap handle
**************************************************************************/
pcap.close();
Is it because it has to finish the loop before printing the information out in the Textarea?
I assume you run the code in a thread. Use
SwingUtilities.invokeAndWait to call the pcktTextArea.append() code

Adding a " to a string in code

I'm writing some code for web services for my Android app which uses JSON. The url should look like this
url = url + "?maddr=" + mailAddr + "&pwd=FB&lect=" + """ + lectName + """ + "&fb=Test";
This is because the Lectname may be two or more words. However the compiler wont accept """, is there a character I can precede the " with to get the compiler to accept it into my string?
Try " \" ". You have to escape the "
http://en.wikipedia.org/wiki/Escape_character
You need this in (nearly) every programming language.

Categories

Resources