I am trying to get and send my MCU's IP Adress, SubnetMask and Gateway adress.
I got them but problem is merging them. I want to merge them with array and send for one step..
For example:
my values are
e2promIpAddress = 0A020705 // represents 10.2.7.5
e2promSubnetMask = FFFF0000 // represents 255.255.0.0
e2promGateway = 0A02070F // represents 10.2.7.15
When I add with sprintf()
char buffer[64];
sprintf(buffer,"%x%x%x",e2promIpAddress,e2promSubnetMask,e2promGateway);
Output is A020705FFFF00000A02070F
Unfortunatelly array must start with 0 but it goes away..
Thanks in advance
I finally find my answer and want to post here..
My values for example e2promIpAddress = 0A020705 is 4 bytes.
When I wrote this with;
sprintf(buffer,"%02x%02x%02x",e2promIpAddress,e2promSubnetMask,e2promGateway);
it did not pad "0"
when I wrote this with;
sprintf(buffer,"%08x%08x%08x",e2promIpAddress,e2promSubnetMask,e2promGateway);
all values which starts with "0" pad with "0"
Have a good day..
Related
So I'm learning C and got this exercise to do with functions. Not gonna ask how to do it.
So I created this function:
int menu(void) {
char user;
do {
printf("Choise: ");
user = getchar();
if (user == '1') {
/* code and no return here */
}
else if (user == '2') {
/* code */
return 2;
}
else if (user == '3') {
/* code */
return 3;
}
} while (user != '3');
Got others controls flows like isdigit(user) and !isdigit(user).
The problem here is that if the user input "fw" (for example), the program prints 2 times "Choise: " or more if there's more input of course.
I tried several others controls flows and changing the variable user to an array and force user[0] == ... && user[1] == '\n' but to no avail.
What I'm trying to do is, if the user don't enter one of the 3 options the program just stop reading after the 1st input and waits for another input.
Already checked several questions at StackOverflow but it doesn't answer to my question :/
I'm all ears to advises! Thank in advance!
The underlying cause here is that getchar() in C gets one single character; it's not like, for example, input() in Python, which gets an entire string of as many characters as you like. The usual technique in C to get a string consisting of more than one character is with pointers: You declare a variable, for instance, as char* a; To obtain user input, you use scanf() and store the input under that pointer address: scanf(&a); Technically, a is the pointer pointing at the memory address of the first character in your string, but the compiler stores the individual characters of the string in a contiguous block of memory until a null character is reached to mark the end of the string.
To avoid the risk of seg faults, you might want to reserve as much memory as you need to hold the longest string you think you'll need: a = malloc((sizeof(char)*n);, with n being the number of characters you want to set memory aside for.
Apologies for not posting in a while, I've been travelling. I thought about the problem again, and I think the root of the problem is that getchar() will return the character cast into an integer, namely, the ASCII value of the character. So if the user keys in "1", you can't run an if statement that tests if (user == '1') (C doesn't natively support direct string comparison). Instead, you should test if (user == 49) - 49 is the ASCII value of the digit "1" ("2" is 50, "3" is 51). If you write your if statements and the loop condition accordingly, that should work.
I have built an android app that will send a string of values (using getbyte()_) across a serial connection. I would like each of these values to be stored in a seperate variable/
For example:
a list of numbers like this:
10004056700003
are sent across the connection.
there are a bunch of variables on the arduino side:
A,B,C,D.... etc
i would like to be able to do this:
A = 1
B = 0
C = 0
D = 0
E = 4
F= 0
.... and so on. i will then use these variables to run a certain sequence of functions on the arduino. In this sense the android application is just to control the arduino.
Thanks for the help! :D
Serial communication usually happens byte-wise.
So if you want to transfer a sequence of numbers (>255) the easiest way is to send each digit as a byte.
On the receiving end you basically have two options.
a) you read each byte and do something with it befor reading the next byte.
b) you read the bytes into a buffer array and do something with it later.
If you want to minimize the number of bytes transferred you of course can split the number value into several bytes instead of transferring each digit.
Try sending the data as String, and then you can access each character of the String using the method: StringVariableName.charAt(pos);
With this approach, your code will be more readable.
Check out charAt function here.
I am trying to import numbers from files and change them if they are at a certain value. I am using torch to get values form gesture and change them from 101 to 10 or from 100 to 9 if the input is the corresponding number (10 or 9). Unfortunately, I have figured out that in Lua, the input is of type userdata which cannot be converted to integers and not compared to integers or torch tensors.
So my question is: How can I check for equality of numbers if the input type is userdata?
Is there potentially a way to convert the input to a number such that comparison is possible?
gesture = matio.load(val, 'gesture')
print(type(gesture)) --prints `userdata`
print(gesture) --prints 10 (for example)
if gesture == th.FloatTensor({101}) then
gesture = th.FloatTensor({10})
print("101 Detected! New value is: ")
print(gesture)
os.exit(0)
elseif gesture == th.FloatTensor({100}) then
gesture = th.FloatTensor({9})
print("100 Detected! New value is: ")
print(gesture)
os.exit(0)
end
Just found an easier way
local gesture_int = gesture:uint()
This is fairly complex question and after testing through the hard way, I found a way to compare the userdata number with an integer.
print(type(gesture)) -> prints `userdata`
print(gesture) -> 10
gesture_str = tostring(gesture)
print(type(gesture_str)) -> prints `string`
gesture_int = tonumber(gesture_str)
print(type(gesture_int)) -> prints `number`
Single line solution is as follows
gesture_int = tonumber(tostring(gesture))
This new variable stores the userdata number as an integer.
I am trying to get information from an Excel worksheet and send it through a serial port as a byte array, using Windows API. This is just a small part of it:
lngSize = UBound(byteData) - LBound(byteData)
WriteFile(udtPorts(intPortID).lngHandle, byteData, lngSize, _lngWrSize, udtCommOverlap)
My current problem: when I am sending a byte array of length 1 (just one byte), I receive it correctly (I am using a hyperterminal to check what I'm sending), but when I send an array of length > 1, here comes the problem; instead of receiving it like this:
letter = 65
For i = 0 To 5
dataToSend(i) = letter
letter = letter + 1
Next
what I should receive
what I get is this:
what I receive
I really cannot figure out what could be the problem and I would be grateful if someone had a clue. Thank you!
First, the correct number of elements in an array is:
lngSize = UBound(byteData) - LBound(byteData) + 1 ' <-- add 1
More importantly, your code is not applying the call convention for the WriteFile API. Namely, the second parameter should be a LPCVOID pointer to the first Byte to transfer. Passing the array's name byteData to the function wont achieve that, because the array is a complex COM data structure, not like a C array. What you should do is:
First get the address of the array's data structure, using VarPtrArray:
Then add 12 to it to get the address of the first byte.
.
Private Declare Function VarPtrArray Lib "VBE7" Alias "VarPtr" (var () As Any) As Long
...
WriteFile(udtPorts(intPortID).lngHandle, VarPtrArray(byteData()) + 12, lngSize, _lngWrSize, udtCommOverlap)
' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For information about handling arrays' data and their pointers, excellent examples can be found [on this page].(https://www.codeproject.com/Articles/729235/VB-and-VBA-Array-Buffering)
Also make sure that you declared you array as a Byte array, like
Redim byteData(someSize) As Byte
' ^^^^^^^
There might be other errors in the parts of code you didn't show (possibly the settings of udtCommOverlap), but hopefully these corrections will put you on the right track.
This is my first post, so I hope someone can help!
I am reading in audio data (in CoreAudio) using the AudioFileReadPackets function, this code is working correctly and loads the 16 bit PCM values into a buffer.
The first sample value is this: '65491' (There is silence at the beginning of this audio). I understand that this is an Unsigned integer, so my question is, how to convert this value to a range of -1 to 1.
Currently I am dividing the sample value by 32768.0 into a float variable, like so...
for (UInt32 i = 0; i < packetCount; i++){
sample = *(audioData + i);
//turn it into the range -1.0 - 1.0
monoFloatDataLeft[i] = (float)sample / 32768.0;
}
However, for the sample given above (as example) this results in an output of '1.998626709' which is not zero (as it should be for silence)?
Saying this, when I look at a sample much later on in the file, the value of which i know to be around the '0.3' mark, the result of the algorithm comes out at '0.311584473' which i believe to be correct?
So why are the first samples not being read as zero, as i know them to be?
You need to subtract 32768 from your unsigned data first, so it's 0 centered.