I am trying to send a value from a slider as a string in socket communication to an instrument. Currently, the only resources I have been able to find on sliders update labels but the values of the slider is not used elsewhere. Ideally, I would like to use the following code in my View Controller and replace values such as 1000 in the send frequency command with the value of a sider rather than hard coding 1000 into the method call:
-(IBAction)Sine {
//[myNetwork sendCommand:@"*CLS\n"];
//[myNetwork sendCommand:@"*RST\n"];
[myNetwork sen开发者_C百科dCommand:@"SOURCE1:FUNCTION SIN\n"];
[myNetwork sendCommand:@"SOURCE1:FREQUENCY 1000\n"];
[myNetwork sendCommand:@"SOURCE1:VOLT:UNIT VPP\n"];
[myNetwork sendCommand:@"SOURCE1:VOLT 2\n"];
[myNetwork sendCommand:@"SOURCE1:VOLT:OFFSET 0\n"];
[myNetwork sendCommand:@"OUTPUT1:LOAD 50\n"];
[myNetwork sendCommand:@"OUTPUT1 ON\n"];
[myNetwork sendCommand:@"OUTPUT2 ON\n"];
}
Below is the method definition for sendCommand():
//sends command to the instrument and reuturns true if successful
-(BOOL)sendCommand:(NSString *)command {
char *com = [command UTF8String];
int comLength = strlen(com);
//send the string to the server
if (send(sockNum, com, comLength, 0) != comLength) {
return FALSE;
}
return TRUE;
}
And here is the prototype in the .h file:
- (BOOL)sendCommand:(NSString *)command; //send command to instr
UISlider has a property value
that returns the current value as a float. If your instrument is expecting an integer, you may want to convert it first. By default the value is in the range 0 to 1, but you can override that by using the slider's minimumValue and maximumValue properties.
In your Sine method, just get the value of the slider and add it to the SOURCE1:FREQUENCY command:
int frequency = (int) frequencySlider.value;
[myNetwork sendCommand:[NSString stringWithFormat:@"SOURCE1:FREQUENCY %d\n", frequency]];
精彩评论