Ejemplo n.º 1
0
/*
 * rs232 = require("luars232")
 * error, port = rs232.open(device)
 * error, port = rs232.open("/dev/ttyUSB0")
 */
static int lua_port_open(lua_State *L)
{
	int ret = 0;
	struct rs232_port_t *p = NULL;
	struct rs232_port_t **ud = NULL;
	const char *dev = luaL_checkstring(L, 1);

	p = rs232_init();
	if (p == NULL) {
		lua_pushinteger(L, RS232_ERR_CONFIG);
		lua_pushnil(L);
		return 2;
	}

	DBG("p=%p \n", (void *)p);

	rs232_set_device(p, dev);
	ret = rs232_open(p);
	if (ret > RS232_ERR_NOERROR) {
		free(p->pt);
		free(p);
		lua_pushinteger(L, ret);
		lua_pushnil(L);
		return 2;
	}

	lua_pushinteger(L, RS232_ERR_NOERROR);
	ud = lua_newuserdata(L, sizeof(struct rs232_port_t *));
	*ud = p;

	luaL_getmetatable(L, MODULE_NAMESPACE);
	lua_setmetatable(L, -2);

	return 2;
}
Ejemplo n.º 2
0
// настроить порт
int port_init ( askue_port_t *Port, const char *file, const char *speed, const char *dbits, const char *sbits, const char *parity )
{
    int RS232 = rs232_open ( file );
    if ( rs232_init ( RS232, &( Port->Termios ) ) )
    {
        rs232_close ( RS232 );
        return -1;
    }
    
    rs232_set_databits ( &( Port->Termios ), dbits );
    rs232_set_stopbits ( &( Port->Termios ), sbits );
    rs232_set_parity ( &( Port->Termios ), parity );
    rs232_set_speed ( &( Port->Termios ), speed );
    
    if ( rs232_apply ( RS232, &( Port->Termios ) ) )
    {
        rs232_close ( RS232 );
        return -1;
    }
    
    Port->RS232 = RS232;
    if ( __port_open ( Port ) )
    {
        close ( Port->RS232 );
        return -1;
    }
    else
    {
        return 0;
    }
}
Ejemplo n.º 3
0
int openCPAPDevice( void )
{
    int uartIndex;
    int io_descriptor;
    pthread_t threadTestUART[TEST_UART_NUMBER];

    if ( cpap_global.uart_fd != -1 )
    {
        printf_debug("close uart set fd:%d to -1\n" , cpap_global.uart_fd );
        close( cpap_global.uart_fd );
    }

    cpap_global.uart_fd=-1;

    for ( uartIndex=0 ; uartIndex<TEST_UART_NUMBER ; uartIndex++ )
    {
        threadTestUART[uartIndex] = -1;
        char *deviceName = cpap_global.uart_id[uartIndex].deviceName;

        bzero( deviceName , sizeof(deviceName) );
        sprintf( deviceName , "/dev/ttyUSB%d" , uartIndex );
        io_descriptor = rs232_open( deviceName , 9600 );

        if ( io_descriptor > 0 )
        {
            printf_debug( "open %s ok\n" , deviceName );
            cpap_global.uart_id[uartIndex].descriptor=io_descriptor;
            pthread_create( &threadTestUART[uartIndex] , 0 , functionTestUART , (void *)uartIndex );
            continue;
        }
        else
        {
            cpap_global.uart_id[uartIndex].descriptor = -1;
            printf_debug( "open %s error\n" , deviceName );
        }
    }

    for ( uartIndex=0 ; uartIndex<TEST_UART_NUMBER ; uartIndex++ )
    {
        if ( threadTestUART[uartIndex] != -1 )
            pthread_join( threadTestUART[uartIndex] , 0 );
    }

    int use_descriptor=GetCPAPDescriptor();

    if ( use_descriptor > 0 )
    {
        printf_debug( "use descriptor:%d\n" , use_descriptor );
        SetLed( 1 );
    }else{
        printf_debug("cant find any cpap device\n" );
        SetLed( 0 );
    }
    return use_descriptor;
}
Ejemplo n.º 4
0
//-------------------------------------------------------------------------------
int init_serial_port()
{
	int ttys_descriptor = rs232_open(SERIAL_PORT,B9600,CS8,PNONE,SB1);
	// Test port descriptor 
	if(ttys_descriptor == -1)
	{
		perror("ERROR:serial port not open!!!");
		fputs("ERROR:serial port not open",DEBUG_DATA_FILE);
		return -1;
	}
	return ttys_descriptor;
}
Ejemplo n.º 5
0
bool QRS232::open(QString & port, enum rs232_baud_e baud)
{
	m_port = rs232_init();
	rs232_set_device(m_port, port.toLatin1().data());
	rs232_set_baud(m_port, baud);
	unsigned int ret = rs232_open(m_port);
	if (ret == RS232_ERR_NOERROR) {
		m_open = true;
		return true;
	}

	return false;
}
Ejemplo n.º 6
0
Archivo: main.c Proyecto: gc87/ctools
unsigned int rs232_simple_test(void)
{
	unsigned int try = 0;
	unsigned int bytes = 0;
	unsigned char data[1];
	unsigned int ret = 0;
	struct rs232_port_t *p = NULL;

	p = rs232_init();
	if (p == NULL)
		return 1;

#ifdef WIN32
	rs232_set_device(p, "COM1");
#else
	rs232_set_device(p, "/dev/ttyS1");
#endif
	ret = rs232_open(p);
	if (ret) {
		rs232_end(p);
		return err(ret);
	}

	rs232_set_baud(p, RS232_BAUD_115200);
	printf("[*] port settings: %s\n", rs232_to_string(p));

	rs232_flush(p);
	while (try++ < 10) {
		printf("%02d. [*] read: ", try);
		data[0] = 0x00;
		ret = rs232_read_timeout(p, data, 1, &bytes, 1000);
		if (ret)
			err(ret);
		else
			printf("0x%02x len: %d\n", data[0], bytes);

		data[0] = 0x05;
		bytes = 0;
		printf("%02d. [*] write: ", try);
		ret = rs232_write_timeout(p, data, 1, &bytes, 1000);
		if (ret)
			err(ret);
		else
			printf("len: %d\n", bytes);
	}

	rs232_end(p);
	return 0;
}
Ejemplo n.º 7
0
int main()
{
   RS232_INFO info;
   char str[10000];

   printf("Enter port [/dev/ttyS0]: ");
   fgets(str, sizeof(str), stdin);
   if (strchr(str, '\n'))
      *strchr(str, '\n') = 0;

   if (!str[0])
      strcpy(str, "/dev/ttyS0");

   info.fd = rs232_open(str, 9600, 'N', 8, 1, 0);

   if (info.fd < 0) {
      printf("Cannot open ttyS0\n");
      return 0;
   }

   /* turn on debugging, will go to rs232.log */
   rs232(CMD_DEBUG, TRUE);

   printf("Connected to ttyS0, exit with <ESC>\n");

   do {
      memset(str, 0, sizeof(str));
      str[0] = ss_getchar(0);
      if (str[0] == 27)
         break;

      if (str[0] > 0)
         rs232_puts(&info, str);

      rs232_gets(&info, str, sizeof(str), "", 10);
      printf(str);
      fflush(stdout);

   } while (1);

   ss_getchar(TRUE);
   rs232_exit(&info);

   return 1;
}
Ejemplo n.º 8
0
static int init_serial(void)
{
    unsigned int ret = 0;
    port = rs232_init();
    if (port == NULL) return 1;

    rs232_set_device(port, portname);
    ret = rs232_open(port); // open port

    if (ret) 
    {
        printf("%s (%s)\n", rs232_strerror(ret), errno > 0 ? strerror(errno) : "");
        return ret;
    }

    rs232_set_baud(port, RS232_BAUD_115200);
    rs232_flush(port);
    // wait for arduino init
    sleep(3); 
    return 0;
}
Ejemplo n.º 9
0
//------------------------------------------------------
void reset_cmps10()
{ 
	int ttys_descriptor;
	int bytes_arrived;
	unsigned char buffer_in[MAX_LEN_BUFFER],cmd_1,cmd_2,cmd_3;
	
	// Open serial port 
	ttys_descriptor = rs232_open(SERIAL_PORT,B9600,CS8,PNONE,SB1);

	// Test port descriptor 
	if(ttys_descriptor == -1)	{printf("ERROR:serial port not open,exit!!!");exit(1);}
	
	// Command to send
	cmd_1 = 0x6A;cmd_2 = 0x7C;cmd_3 = 0x81;
	
	// Send request 0x23
	bytes_arrived = 0;
	rs232_write(ttys_descriptor,&cmd_1,1);
	usleep(50000);
	bytes_arrived = rs232_bytes_arrived(ttys_descriptor,0);
	rs232_get(ttys_descriptor,buffer_in, 1);
	printf("Bytes arrived %d - Value %d\n",bytes_arrived,buffer_in[0]);
	
	rs232_write(ttys_descriptor,&cmd_2,1);
	usleep(50000);
	bytes_arrived = rs232_bytes_arrived(ttys_descriptor,0);
	rs232_get(ttys_descriptor,buffer_in, 1);
	printf("Bytes arrived %d - Value %d\n",bytes_arrived,buffer_in[0]);
		
	rs232_write(ttys_descriptor,&cmd_3,1);
	usleep(50000);
	bytes_arrived = rs232_bytes_arrived(ttys_descriptor,0);
	rs232_get(ttys_descriptor,buffer_in, 1);
	printf("Bytes arrived %d - Value %d\n",bytes_arrived,buffer_in[0]);
	rs232_close(ttys_descriptor);
	
}
Ejemplo n.º 10
0
int main (void) {
//  int i;
  int ret;
  char *emergency="! EMERGENCY !";
  char *welcome="Welcome.";
  char *enter_pin="Please Enter PIN.";
  char button_read = FALSE;  // Local snapshot of Global 'Button'

  /* error handling - reset LEDs */
  atexit(closing_time);
  signal(SIGINT, closing_time);

  term_initio();
  rs232_open();
  
  setup_ports();
  ret = pthread_create( &keypad_thread, NULL, keypad, NULL);

//  display_string(welcome,PADDED,BLOCKING);

  while(alive){
    if(state == EMERGENCY){
        display_string(emergency,PADDED,NOT_BLOCKING);
      continue;
    }
    switch(state){
      case WAITING_LOGGED_OUT:
        display_string(enter_pin,PADDED,NOT_BLOCKING);
        digits[0] = 0x80;  // Set cursor position
        
        while(!button && alive && state == WAITING_LOGGED_OUT){ // Just Wait 
         usleep(SLEEP);
        }
        if(state == EMERGENCY) break; // Get out if there's an emergency
        if(button >= '0' && button <= '9'){
          state = INPUTTING_PIN; // Fall through to next state
        }
        else{
          break;
        }
        
      case INPUTTING_PIN:
        if(button_read = button){ // Intentionally Assignment
          input_pin(button_read);  // Sends a snapshot of button
        }
        cursor_blink();
        break;
        
      case WAITING_LOGGED_IN:
        display_string("Enter Track Number.",PADDED,NOT_BLOCKING);
        digits[0] = 0x80;  // Set cursor position
        
        while(!button && alive && state == WAITING_LOGGED_IN){ // Just Wait 
          usleep(SLEEP);
        }
        if(state == EMERGENCY) break; // Get out if there's an emergency
        if(button >= '0' && button <= '9'){
          state = INPUTTING_TRACK_NUMBER; // Fall through to next state
        }
        else if(button == ENTER_MENU){
          state = MENU_SELECT; // Fall through to next state
          break;
        }
        else{
          break;
        }
        
      case INPUTTING_TRACK_NUMBER:
        if(button_read = button){ // Intentionally Assignment
          input_track_number(button_read);  // Sends a snapshot of button
        }
        cursor_blink();
        break;
        
      case MENU_SELECT:
        display_string("MENU.",PADDED,NOT_BLOCKING);
        menu_select();
        break;
        
      default:
        break;
    }
    delay();
  }


  pthread_join(keypad_thread, NULL);

  term_exitio();
  rs232_close();
  return 0;
}
Ejemplo n.º 11
0
CommunicatieBeheerder::CommunicatieBeheerder() {
    rs232_open();
}
Ejemplo n.º 12
0
/*------------------------------------------------------------------------------
 * main -- Open terminal and launch keypad thread.
 *         Check for button input and respond appropriately
 *------------------------------------------------------------------------------
 */
int main (void) {
//  int i;
  int ret;
  char *emergency="! EMERGENCY !";
  char button_read = FALSE;  // Local snapshot of Global 'Button'
  char prev_button = 0;

  /* error handling - reset LEDs */
  atexit(closing_time);
  signal(SIGINT, closing_time);

  term_initio();
  rs232_open();
  
  setup_ports();
  ret = pthread_create( &keypad_thread, NULL, keypad, NULL);
    
  while(alive){
    button_read = button;
    if(button_read){
      if(prev_button != button_read){
        if(blocking){
          while(blocking == TRUE && alive){
            usleep(SLEEP);
          }
        }
        else{
          printf("button: %c\n",button_read);
          switch(button_read){
            case 'A':
              display_string("Hello. =)",BLOCKING);
              break;
            case 'B':
              move_cursor(LEFT);
              break;
            case 'C':
              display_string("Cancel =(",NOT_BLOCKING);
              break;
            case 'D':
              delete_char();
              break;
            case 'E':
              display_string(emergency,BLOCKING);
              break;
            case 'F':
              move_cursor(RIGHT);
              break;
            case ERROR:
              break;
            default:
              insert_char(button_read);
              break;
          }
        }
      }
    }
    prev_button = button_read;
  }

  pthread_join(keypad_thread, NULL);

  term_exitio();
  rs232_close();
  return 0;
}
Ejemplo n.º 13
0
bool CProber::Open (int portNr)
{
	if (IsOpen()) return false;
	rs232 = rs232_open(portNr, 9600, 'N', 8, 1, 0);
	return rs232 >= 0;
}
Ejemplo n.º 14
0
void setup_term(){
  term_initio();
  rs232_open();
  setup_ports();
}
Ejemplo n.º 15
0
int rs232drv_open(int device)
{
    return rs232_open(device);
}