int CPAP_send( int descriptor , char *cmd , int cmd_length ) { if ( cpap_global.uart_fd > 0 ) descriptor = cpap_global.uart_fd; if ( descriptor <= 0 ) { tryToOpenCPAP(); return -1; } if ( rs232_write( descriptor , cmd , cmd_length ) < 0 ) { tryToOpenCPAP(); if ( descriptor >= 0 ) return CPAP_send( descriptor , cmd , cmd_length ); else return -1; } if ( debug ) { char message[32]; sprintf( message , "FD(%d) <<<\n" , descriptor ); printData( cmd , cmd_length , message , 1 ); } return 0; }
u_int8_t bs_inverter_write(char* msg, int* size) { set_interface_attr(B9600, 0); rs232_write(msg, size); return 0; }
void iso_write(HANDLE_ISO iso, unsigned char format, unsigned char target, unsigned char source, unsigned char *data, unsigned int len) { int index; unsigned char checksum; checksum = (format & 0xf0) | (len+4); rs232_write(iso->rs232, &checksum, 1); rs232_write(iso->rs232, &target, 1); rs232_write(iso->rs232, &source, 1); rs232_write(iso->rs232, data, len); checksum += target + source; for(index = 0; index < len; index++) { checksum += data[index]; } rs232_write(iso->rs232, &checksum, 1); }
// ------------------------------------- int write_cmd(int ttys_descriptor, unsigned char cmd) { int ret; // Command to send // unsigned char cmd = 0x23; ret = rs232_write(ttys_descriptor,&cmd,1); if(ret == -1) { perror("rs232_write error :\n"); fputs("ERROR:can't write on serial port",DEBUG_DATA_FILE); return -1; } return ret; }
//------------------------------------------------------ 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); }
static int SendCommand( int descriptor , char *command_code , int command_length , uint8_t *recv_buffer , int recv_buffer_length , int expected_recv_length ) { uint8_t checkedXor; checkedXor = getCheckedXor( (uint8_t *)command_code , command_length ); command_code[command_length]=(char)checkedXor; command_length++; if ( rs232_write( descriptor , command_code , command_length ) < 0 ) return -1; int responseSize; responseSize = recvCPAPResponse( descriptor , recv_buffer , recv_buffer_length , command_code[1] , expected_recv_length ); return responseSize; }
/* * error, written_len = port:write(data [, timeout_ms]) */ static int lua_port_write(lua_State *L) { int ret = 0; int argc = 0; unsigned int timeout = 0; unsigned int wlen = 0; size_t len = 0; const char *data; struct rs232_port_t *p = NULL; p = *(struct rs232_port_t**) luaL_checkudata(L, 1, MODULE_NAMESPACE); lua_remove(L, 1); if (p == NULL || !rs232_port_open(p)) { lua_pushinteger(L, RS232_ERR_PORT_CLOSED); lua_pushinteger(L, 0); return 2; } argc = lua_gettop(L); switch (argc) { case 1: { data = luaL_checklstring(L, 1, &len); ret = rs232_write(p, (unsigned char*) data, (unsigned int) len, &wlen); break; } case 2: data = luaL_checklstring(L, 1, &len); timeout = (unsigned int) luaL_checknumber(L, 2); ret = rs232_write_timeout(p, (unsigned char*) data, (unsigned int) len, &wlen, timeout); break; default: lua_pushinteger(L, RS232_ERR_CONFIG); lua_pushinteger(L, 0); return 2; } lua_pushinteger(L, ret); lua_pushinteger(L, wlen); return 2; }