Im new at writing code in general and I need some help converting a Python Script to OBJ-C for an iOS App.
I have a python script running an XMLRPC server on a Network Bridge.
I have an iOS App written in Xcode4 that needs to make a call to the python script running on the Network Bridge when a button is pressed.
I also have an XMLRPC Client script written in Python that can access the XMLRPC Server.
Basically I need to convert the Python XMLRPC Client script to OBJ-C so I can run it on the iOS app.
Python XMLRPC Client Script:
from xmlrpclib import ServerProxy, Binary
import time
yourAddr = Binary('\x00\x00\x04') # SNAP Address of this client application
ourAddr = Binary('\x4c\x6f\x8c') # SNAP Address of this client application
CONN_TYPE_USB = 2
count =0
def listenForResponse(srv):
global count
""" Wait and listen for a mcast count message """
eventResp = srv.waitOnEvent(ourAddr,1)
if eventResp != None and eventResp['methodName'] == "setButtonCount":
# The count should be the first parameter
print "The button count = ", (eventResp['parameters'][0]), "\n"
else:
if eventResp == None:
print "time out "
else:
print "...not the response we were looking for; continue waiting "
print eventResp['methodName']
# print "mcastRpc setButtonCount to snap server"
# count = (count + 1) %100
# srv.mcastRpc( ourAddr, 1,5, 'setButtonCount',[count])
# time.sleep(1)
if __name__ == '__main__':
""" The main program loop for this example """
# Connect to SNAPconnect server via TCP port 8080
#
# srv = ServerProxy("http://192.168.0.x:8080", allow_none=False)
srv = ServerProxy("http://192.168.0.x:8080", allow_none=False) #call from the xmlrpc library
# srv = ServerProxy("public:public:http://192.168.1.106:8080")
# Connect to SNAP bridge node (We'll assume it is on the first USB port)
print "opening serial" #remote procedure call being executed on the E10, telling the E10 to listen to the SNAP bridge on Serial port #1
srv.connectSerial(1,"/dev/ttyS1") # for the e10
# Continually listen for the count
while True:
listenForResponse(srv)
My Code from Xcode:
-(IBAction)getResponse:(id) sender{
NSString *ourAddr = @"1";
NSString *dev = @"/dev/ttylS1";
NSString *server = @"192.168.0.x:8080";
XMLRPCRequest *waitOnEvent = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
[waitOnEvent setMethod:@"connectSerial" withObject:[NSString stringWithString: ourAddr]];
NSString *result=[self executeXMLRPCRequest:waitOnEvent];
[waitOnEvent release];
if( ![result isKindOfClass:[NSString class]] ) //err occured.
lblResponse = result; //@"error";
else
lblResponse.text = @"error";
}
- (id)executeXMLRPCRequest:(XMLRPCRequest *)req {
XMLRPCResponse *userInfoResponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:req];
return [userInfoResponse object];
}
What I get from the XMLRPC Server After Pushing the "getResponse" button in my App:
15:48:15:611 INFO SNAPconnect Now listening for XML-RPC connections on port 8080
15:48:23:981 INFO http_server connectSerial(1, )
15:48:24:003 INFO http_server 192.168.0.x:51752 - - "POST / HTTP/1.1" 200 479
15:48:24:010 INFO http_server <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string><type 'exceptions.TypeError'>:connectSerial() takes at least 3 arguments (2 given)</string></value>
</member>
</struct></value>
</fault>
</methodResponse>
So I see from this that connectSerial is only getting 2 arguments [connectSerial(1, )] passed and it needs 3. [connectSerial(1, /dev/ttylS1)]
If I try to pass multiple arguments with something like:
[waitOnEvent setMethod:@"connectSerial" withObject:[NSString stringWithString: ourAddr: dev]];
I just get a BAD_EXC or SIGABRT
What I get from Python Server when I run the Python Client script:
15:54:00:505 INFO http_server connectSerial(1, /dev/ttyS1, )
15:54:00:534 INFO http_server 192.168.0.x:51772 - - "POST /RPC2 HTTP/1.0" 200 225
15:54:00:541 INFO http_server <?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value><boolean>1</boolean></value>
</param>
</params>
</methodResponse>
15:54:00:573 INFO http_server waitOnEvent(4c6f8c, 1, )
Any help or insight to this would be greatly appreciated! Sorry it was so long winded. I provide any clarification as needed.
Thanks in advance!
EDIT: To clarify further for future reference:
Well its seems like I've gotten closer, but not quite there yet...
If I use:
NSString *const ourAddr = @"1";
NSString *const dev = @"/dev/ttyS1";
-(IBAction)getResponse:(id) sender{
NSArray *args = [NSArray arrayWithObjects: ourAddr, dev, nil];
NSString *server = @"开发者_Go百科http://192.168.0.x:8080";
XMLRPCRequest *waitOnEvent = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:server]];
[waitOnEvent setMethod:@"connectSerial" withObjects: args];
The server gives me this:
20:17:20:954 INFO http_server connectSerial(1, /dev/ttyS1, )
20:17:20:975 INFO http_server 192.168.0.x:52253 - - "POST / HTTP/1.1" 200 467
20:17:20:981 INFO http_server <?xml version='1.0'?>
<methodResponse>
<fault>
<value><struct>
<member>
<name>faultCode</name>
<value><int>1</int></value>
</member>
<member>
<name>faultString</name>
<value><string><type 'exceptions.ValueError'>:Serial interface type must be an integer</string></value>
</member>
</struct></value>
</fault>
</methodResponse>
So now I just need to figure out how to declare the "1" as an integer and then pass both and integer and a string simultaneously. So far everything ive tried has given me a BAD_EXC...
My guess is that you need to create an array containing your arguments:
NSArray *args = [NSArray arrayWithObjects:ourAddr, dev, nil];
and then pass that to your XMLRPCRequest:
[waitOnEvent setMethod:@"connectSerial" withObject:args];
You cannot invoke a script on the iOS device. You can in OS X, but not in an (app store approved) iOS app.
精彩评论