I am still a beginner at Cocoa so apologies for asking what must be a basic question,
I am trying to connect to a CC128 electricity monitor to my Mac using the usbserial port.
The CC128 operates at 56700 baud with 8 data bits , 1 stop and no parity and I can read the data using Zterm and also successfully using Perl
I really want to use Cocoa to develop a simple app but how do I set up the options in the dictionary to set the speed, data bits and parity info. AMSerialPort is opening the port OK but the data is garbled because of the speed etc.
Can anyone help?
Thanks again to Sean McBride and Nick Zitzmann. Sean made the changes necessary for the code to work under 10.5 and garbage collection while Nick contributed the compatibility headers for older SDKs.
The project was tested under Xcode 2.5 and Xcode 3.0. It builds for 10.3.9, 10.4u and 10.5 SDKs, with and without GC enabled.
Jeremy,
[scalePort writeString:@"\x02\x34\x35"
usingEncoding:NSASCIIStringEncoding
error:NULL];
Jeremy,
that's one way of doing it.
I'd probably put the data in some external file and load it from there.
In case you have more questions about Objective-C or Cocoa, I suggest you join the cocoa-dev mailing list at lists.apple.com.
I ended up doing the following...
u_char chrArray[5] = {0x02, 0x00, 0x01, 0x80, 0x81};
[port writeData:[NSData dataWithBytes:& chrArray length:5] error:NULL];
Hi Andreas,
I am a novice to Objective-C... so please, bear with me for some indubitably dumb question(s) and remark(s)...
I am trying to send out a hex command via serial port to a GPS...
Is this at all possible with either the writeString or writeData methods?
I tried so many different things that I'm insanely confused... (heck, I even tried to convert char(hex) -> nsstring).
Using python I was using Py-Serial and sending out a command like :
write(chr(0x02) + chr(0x00) + chr(0x01) + chr(0x80) + chr(0x81))
but.... apparently things are not as simple with Obj-C ;-)
Danke viel mol for any assistance...!
Jeremy
Hi Andreas,
I don't know what went wrong last time, but after some reboots and using commitchanges all the time, it now works. And much better than the ruby serial library I had to resort to! Thanks for a nice library. Maybe I should write some documentation for it, but the documentation is in the source after all...
I just totally overlooked the commitchanges method :-(
Hi Axel. Setting the speed with
[port setSpeed:38400];
[port commitChanges];
works for me. (Also tested with ZTerm for good measure.)
The port must be open before making any changes; but since you said it reports the new settings, I guess that's not your problem. I'm afraid without more Information, I can't be of any help.
Bill,
"My workaround: skip the NSData create and delegate calls in readDataInBackgroundThread if bytesRead isn't greater than 0."
Thank you. I'll add that for the next release.
Hi Andreas,
I've been tearing my hair out trying to solve a weird problem with my serial program. When I connect to it using ZTerm, it works, but when I connect using AMSerial, it doesn't. I've traced it down to a problem with the serial port, but am unable to continue further, due to my limiting skills as a C hacker.
What happens is that although I set the speed to 38400, the speed within AMSerial is always 9600. I've tested setting the speed using the following code within AMSerialPort:setSpeed:
errorCode = cfsetspeed(options, speed);
test = cfgetospeed(options);
test = cfgetispeed(options);
Both speeds are set to e.g. 38400, and the test confirms that. However a test with ZTerm shows that it's still sending and receiving at 9600.
I've tried several things after setting the port as in the example:
[self setPort:[[[AMSerialPort alloc] init:deviceName withName:deviceName type:(NSString*)CFSTR(kIOSerialBSDModemType)] autorelease]];
[port setSpeed:38400];
[port commitChanges];
None of these work. There is no example code that uses another speed than 9600, so I'm at a loss.
Also, all my test equipment (Keyspan + Belkin adapter) work with ZTerm, PC software and a ruby serialport implementation.
Tested on 10.4.11 (G5) and 10.5.2 (Intel)
Any idea what could be wrong?
Great resource. Thanks!
BUG: If an active port is unplugged while reading in background an exception results when readDataInBackgroundThread attempts to create an NSData with bytesRead = -1. I am catching the port removed notification and calling stopReadInBackground, but evidently I cannot stop the read thread soon enough. My workaround: skip the NSData create and delegate calls in readDataInBackgroundThread if bytesRead isn't greater than 0.
10.5.2 with gc on, Mac Pro.
1) Bitte geben Sie möglichst Ihren echten Namen an. (Momentan gibt es keine Möglichkeit, einen festen Account anzulegen.)
2) Ihre Email-Adresse wird nie veröffentlicht werden. Die Angabe ermöglicht mir, Sie ggf. zu kontaktieren.
3) Die Adresse Ihrer Webseite wird zusammen mit Ihrem Namen veröffentlicht. Bitte kurz halten.
Ich behalte mir das Recht vor, Beiträge aus beliebigen Gründen zu löschen.
Also bitte keinen SPAM posten, beim Thema bleiben, nicht unverschämt werden, etc.