Go Down

Topic: (Mac) Cocoa: Arduino restarts when sending (IOKit) (Read 932 times) previous topic - next topic

ixde

Hello,

im using the IOKit in order to send something to the Arduino board. But as soon as i run my file, the board restarts upon sending the message.

Arduino Code:
Code: [Select]
void setup()
{
  pinMode (2, OUTPUT);
  pinMode (3, OUTPUT);
  pinMode (4, OUTPUT);
 
  Serial.begin (9600);
}

void loop ()
{
  digitalWrite (2, HIGH);
 
  if (Serial.available () > 0)
  {
    int c = Serial.read();
   
    if (c == 255)
    {
      digitalWrite (3, HIGH);
    }
    else
      digitalWrite (4, HIGH);
  }
}




XCode:
Code: [Select]

    // open the serial like POSIX C
    serialFileDescriptor = open(
                                "/dev/tty.usbmodemfa131",
                                O_RDWR |
                                O_NOCTTY |
                                O_NONBLOCK );
   
    struct termios options;
   
    // block non-root users from using this port
    ioctl(serialFileDescriptor, TIOCEXCL);
   
    // clear the O_NONBLOCK flag, so that read() will
    //   block and wait for data.
    fcntl(serialFileDescriptor, F_SETFL, 0);
   
    // grab the options for the serial port
    tcgetattr(serialFileDescriptor, &options);
   
    // setting raw-mode allows the use of tcsetattr() and ioctl()
    cfmakeraw(&options);
   
    speed_t baudRate = 9600;
   
    // specify any arbitrary baud rate
    ioctl(serialFileDescriptor, IOSSIOSPEED, &baudRate);
   
    // start a thread to read the data coming in
    [self
     performSelectorInBackground:
     @selector(incomingTextUpdateThread:)
     withObject:
     [NSThread currentThread]];
   
    int val = 255;
    write(serialFileDescriptor, val, 1);

PaulS

Opening the serial port, regardless of the mechanism used to do that, resets the Arduino, unless you have modified it to not reset when the serial port is opened. If that is not what you want, you have to modify your hardware. It's pretty easy, and need not be permanent. Just google.

ixde

Okay, i just added a sleep (5) and then try to send the message, but the program freezes then:

Code: [Select]
// open the serial like POSIX C
    serialFileDescriptor = open(
                                "/dev/tty.usbmodemfa131",
                                O_RDWR |
                                O_NOCTTY |
                                O_NONBLOCK );
   
    struct termios options;
   
    // block non-root users from using this port
    ioctl(serialFileDescriptor, TIOCEXCL);
   
    // clear the O_NONBLOCK flag, so that read() will
    //   block and wait for data.
    fcntl(serialFileDescriptor, F_SETFL, 0);
   
    // grab the options for the serial port
    tcgetattr(serialFileDescriptor, &options);
   
    // setting raw-mode allows the use of tcsetattr() and ioctl()
    cfmakeraw(&options);
   
    speed_t baudRate = 9600;
   
    // specify any arbitrary baud rate
    ioctl(serialFileDescriptor, IOSSIOSPEED, &baudRate);
   
    NSLog (@"before");
    sleep (5); // Wait for the arduino to restart
    NSLog (@"after");
   
    int val = 255;
    write(serialFileDescriptor, val, 1);
    NSLog (@"after2");


It outputs before and after, but never comes to after2.

Go Up