void dcm_start(DevComMaster_t* rMaster, uint32_t vFcpu, uint32_t vBaudrate) { setCurrentMaster(rMaster); DevComMaster_t* currentMaster = getCurrentMaster(); currentMaster->Ping = dcm_ping; currentMaster->SendResetSequence = dcm_sendResetSequence; currentMaster->RequestSlaveInformation = dcm_readSlaveInfo; currentMaster->Request = dcm_request; currentMaster->SendCommand = dcm_command; currentMaster->SendData = dcm_data; currentMaster->SendCommandBroadcast = dcm_commandBC; currentMaster->SendDataBroadcast = dcm_dataBC; char *portname = currentMaster->UART; fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { printf ("error %d opening %s: %s", errno, portname, strerror (errno)); exit(-1); } set_interface_attribs (fd, B500000, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking pthread_create( &readingThread, NULL, readSerial, (void*) currentMaster); }
mrb_value mrb_serialport_open(mrb_state *mrb, mrb_value self) { int fd; mrb_value mrb_portname = IV_GET("@port_name"); mrb_value mrb_baud = IV_GET("@baud"); mrb_value mrb_blocking = IV_GET("@blocking"); const char *portname = mrb_string_value_cstr(mrb, &mrb_portname); unsigned int baud = mrb_fixnum(mrb_baud); fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } if (!isatty(fd)) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } if (set_interface_attribs(fd, baud, 0)) { update_error(mrb, self); mrb_raise(mrb, E_RUNTIME_ERROR, strerror(errno)); } if (set_blocking(fd, mrb_bool(mrb_blocking) ? 1 : 0) != 0) { IV_SET("@error", mrb_str_new_cstr(mrb, "Could not set blocking behavior")); mrb_raise(mrb, E_RUNTIME_ERROR, "Could not set blocking behavior"); } IV_SET("@fd", mrb_fixnum_value(fd)); return self; }
int main(){ char *portname = "/dev/ttyACM0"; int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { error_message ("error %d opening %s: %s", errno, portname, strerror (errno)); return; } set_interface_attribs (fd, B9600, 0); // set speed to 9600 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking while(1){ //read from serial char message[100]; fgets(message, 100, stdin); write(fd, message, 100); //write to serial char buf[100]; read(fd, buf, sizeof buf); printf("%s\n", buf); } }
int main(int argc, char **argv) { char *portname = (char *)"/dev/ttyUSB0"; int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if(fd < 0) { printf ("error %d opening %s: %s", errno, portname, strerror (errno)); return 1; } set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking printf("Sending string\n"); char *sendstr=(char *) "#0P1500 #1P1500 #2P1500 #3P1500 #4P1500 #5P1500 \r"; write (fd, sendstr, strlen(sendstr)); usleep ((100 + 25) * 100); // sleep enough to transmit the 100 plus // receive 25: approx 100 uS per char transmit char buf [100]; int n = read (fd, buf, sizeof buf); // read up to 100 characters if ready to read buf[n]=0; printf("Received %d chars: %s\n",n,buf); return 0; }
int main(int argc, char *argv[]) { puts("opening port"); int f = open("/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_SYNC); if (f >= 0) { puts("opened OK"); } else { puts("problems"); } set_interface_attribs(f, B2400, 0); ssize_t cnt; char buff[10]; write(f, "\xff\x01\x00\x02\x00\x00\x03", 7); //cnt = read(f, buff, 4); //write(f, "\xff\x01\x00\x04\x01\x01\x07", 7); usleep(20000); write(f, "\xff\x01\x00\x00\x00\x00\x01", 7); puts("sent..."); cnt = read(f, buff, 4); printf("received %d bytes", cnt); close(f); return 0; }
int main(int argc, char** argv) { if(argc != 3) { fprintf(stderr, "usage: %s port speed\n", argv[0]); return 1; } char *portname = argv[1]; int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { error_message ("error %d opening %s: %s", errno, portname, strerror (errno)); return 1; } set_interface_attribs (fd, B19200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking sendMessage(fd, COMMAND_SYNC_STREAM, COMMAND_SYNC_STREAM); sendMessage(fd, COMMAND_SET_SPEED, 500); Message msg; while(!hasMessage(&msg, fd)); printf("message type = %d: high = %d low = %d\n", msg.type, msg.payload_high, msg.payload_low); }
static bool setup_serial_port(void) { bool rtn = false; char *portname = SERIAL_PORT_NAME; nordic_fp = open(portname, O_RDWR | O_NOCTTY | O_NONBLOCK); if (nordic_fp != -1) { return set_interface_attribs(); } return rtn; }
void serial_init() { fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { fprintf(stderr, "Error opening %s\n", SERIAL_PORT); exit(1); } set_interface_attribs(fd, B9600, 0); set_blocking(fd, 0); }
Serial::Serial(const char* port_name, int baud) { fd = open(port_name, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { std::cerr << "error " << errno << " opening " << port_name << ": " << strerror(errno) << std::endl; return; } set_interface_attribs(baud, 0); // set speed to <baud> bps, 8n1 (no parity) set_blocking(false); // set no blocking fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); }
void InitialiseUSART(char * portname){ fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { error_message ("error %d opening %s: %s", errno, portname, strerror (errno)); return; } set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 1); // set no blocking }
int main(int argc, char ** argv) { char portname[32]; if(argc < 2) { printf("Warning: Using default port /dev/ttyUSB1\n"); strcpy(portname,"/dev/ttyUSB1"); } else strcpy(portname,argv[1]); printf("<This is a sample sending program>\n"); printf("name of device %s\n",portname); #if _DEBUG printf("!!!DEBUG MODE\n"); #endif #if _DEBUG_XBEE_parse_XBEE_msg printf("!!!DEBUGING XBEE_parse_XBEE_msg()\n"); #endif int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC); if(fd < 0) { fprintf(stderr, "%s\n", strerror(errno)); return 0; } set_interface_attribs(fd,B57600,0); set_blocking(fd,0); XBEE xbee_coor(fd,0x00000000,0x0000ffff,0); //char dummy[32]; const char data[] = "hello A!"; uint32_t addr_hi = 0x0013a200; uint32_t addr_lo = 0x409c27b4; uint8_t net_addr_hi = 0xFF; uint8_t net_addr_lo = 0xFE; XBEE_msg msg; msg.set_tran_packet(addr_hi,addr_lo,net_addr_hi,net_addr_lo,(uint8_t *)data,strlen(data) + 1); XBEE_msg msg1; char data2[] = "???"; msg1.set_tran_packet(addr_hi,addr_lo,net_addr_hi,net_addr_lo,(uint8_t *)data2,strlen(data2) + 1); while(1) { sleep(2); //msg.show_hex(); xbee_coor.XBEE_send_msg(msg); sleep(1); xbee_coor.XBEE_send_msg(msg1); } return 0; }
// Initialise serial cable and run one time setup bool application_init(void) { char *portname = "/dev/ttyUSB0"; fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { NABTO_LOG_INFO(("error %d opening %s: %s\n", errno, portname, strerror (errno))); return 0; } else{ NABTO_LOG_INFO(("Opening %s: %s\n", portname, strerror (errno))); } set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking // Wake Roomba setRTS(fd, 0); usleep(100000); //0.1 sec setRTS(fd, 1); sleep(2); // Songs can be defined while in passive mode (which we are in right now) char DSB[] = {140, 0, 3, 74, 40, 75, 20, 70, 32}; write(fd, &DSB, sizeof(DSB)); usleep ((sizeof(DSB)+25) * 100); char reverse[] = {140, 1, 6, 84, 32, 30, 32, 84, 32, 30, 32, 84, 32, 30, 32}; write(fd, &reverse, sizeof(reverse)); usleep ((sizeof(reverse)+25) * 100); char car_low[] = {140, 2, 12, 74,10, 71,10, 67,10, 67,10, 67,10, 69,10, 71,10, 72,10, 74,10, 74,10, 74,10, 71,10}; write(fd, &car_low, sizeof(car_low)); usleep ((sizeof(car_low)+25) * 100); char car_high[] = {140, 3, 12, 86,10, 83,10, 79,10, 79,10, 79,10, 81,10, 83,10, 84,10, 86,10, 86,10, 86,10, 83,10}; write(fd, &car_high, sizeof(car_high)); usleep ((sizeof(car_high)+25) * 100); // Put into safe mode char safe[] = {128, 131}; write(fd, &safe, sizeof(safe)); usleep(20*1000); // When changing mode, allow 20 milliseconds // Set display to NAb T/O char display[] = {163, 55, 119, 124, 93}; write(fd, &display, sizeof(display)); usleep ((sizeof(display)+25) * 100); return 1; }
R_API int r_socket_connect_serial(RSocket *sock, const char *path, int speed, int parity) { int fd = open (path, O_RDWR | O_SYNC | O_BINARY, 0); // O_NOCTY if (fd == -1) { return -1; } if (speed < 1) { speed = 9600; // 19200 } (void)set_interface_attribs (fd, speed, parity); sock->fd = fd; return fd; }
int serialInitialization(){ int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { error ("error %d opening %s: %s", errno, portname, strerror (errno)); return -1; } set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking return fd; }
/** * Opens and initializes a serial port. */ int serial_create (char *device) { int fd = open(device, O_RDWR | O_NOCTTY | O_SYNC | O_NDELAY); if (fd < 0) { printf("error %d opening '%s', %s\n", errno, device, strerror(errno)); goto exit; } set_interface_attribs(fd, B19200, 0); set_blocking(fd, 0); exit: return fd; }
int main(int argc, char **argv){ char *portname0 = "/dev/ttyUSB1"; char *portname1 = "/dev/ttyUSB0"; int fd0 = open(portname0, O_RDWR | O_NOCTTY | O_SYNC); if (fd0 < 0) { fprintf(stderr, "error %d opening %s: %s", errno, portname0, strerror(errno)); return; } set_interface_attribs(fd0, B9600, 0); //B115200 set_blocking(fd0, 0); write(fd0, "hello!\n", 7); printf("hello!\n"); usleep((7+25)*1000); close(fd0); char buf[100]; int fd1 = open(portname1, O_RDWR | O_NOCTTY | O_SYNC); if (fd1 < 0) { fprintf(stderr, "error %d opening %s: %s", errno, portname1, strerror(errno)); return; } set_interface_attribs(fd1, B9600, 0); set_blocking(fd1, 0); int n = read(fd1, buf, sizeof buf); printf("%s",buf); close(fd1); return 0; }
int initializeSerialPort(char *portname) { int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); printf("FD OPEN, fd = %d", fd); if (fd < 0) { printf("error %d opening %s: %s", errno, portname, strerror (errno)); return -1; } set_interface_attribs (fd, B9600, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking return fd; }
int initMW(void) { int fd = open (MINDWAVEPORT, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { fprintf (stderr, "error %d opening %s: %s\n", errno, MINDWAVEPORT, strerror (errno)); exit (1); } set_interface_attribs (fd, B57600, 0); // 57,600 bps, 8n1 (no parity) set_blocking (fd, 0); // set non blocking return (fd); }
int initSerial( int *fd, int baudrate, char *devname ) { *fd = open( devname, O_RDWR | O_NOCTTY | O_SYNC ); if (*fd < 0) { fprintf(stderr, "! ERROR: Could not open serial port!\n"); perror("! ERROR Message from system"); return EXIT_FAILURE; } set_interface_attribs (*fd, baudrate, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (*fd, 0); // set no blockin return EXIT_SUCCESS; }
//Open the serial port file descriptor int openSerial(){ char *portname = "/dev/ttyMCC"; // Open serial port (file descriptor) int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { printf("error %d opening %s: %s", errno, portname, strerror (errno)); return 0; } // Configure the serial communication interface before use set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking return fd; }
int openPort(char* portname, int speed, int vmin, int vtime) { printf("trying to open port %s\n", portname); int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { printf("error %d opening %s: %s\n", errno, portname, strerror (errno)); return; } set_interface_attribs (fd, speed, 0); set_blocking (fd, vmin, vtime); return fd; }
int main(void) { char *portname = "/dev/ttymxc3"; int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { printf("error %d opening %s: %s", errno, portname, strerror (errno)); return; } set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking char buf [7]; int n; char buffON[2] = {13,1}; write(fd, buffON, 2); // send 2 character greeting sleep(1); // delay 1 second n = read (fd, buf, sizeof buf); // read up to 7 characters if ready to read if (n > 0) { //printf("quanti: %d\n", n); int i; for (i = 0; i < n; i++) { printf("%c",buf[i]); } } char buffOFF[2] = {13,0}; write(fd, buffOFF, 2); sleep(1); // delay 1 second n = read (fd, buf, sizeof buf); // read up to 7 characters if ready to read if (n > 0) { //printf("quanti: %d\n", n); int i; for (i = 0; i < n; i++) { printf("%c",buf[i]); } } }
int main(int argc, char **argv) { fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { printf("error %d opening %s: %s", errno, portname, strerror (errno)); return; } set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking if(argc == 2){ if(strcmp(argv[1], "red") == 0) sendcmd(254, 0, 0); else if(strcmp(argv[1], "green")== 0) sendcmd(0, 254, 0); else if(strcmp(argv[1], "blue")== 0) sendcmd(0, 0, 254); else if(strcmp(argv[1], "yellow")== 0) sendcmd(254, 254, 0); else if(strcmp(argv[1], "pink")== 0) sendcmd(254, 0, 254); else if(strcmp(argv[1], "cyan")== 0) sendcmd(0, 254, 254); else if(strcmp(argv[1], "black")== 0) sendcmd(0, 0, 0); else if(strcmp(argv[1], "white")== 0) sendcmd(254, 254, 254); else printf("\nUnknown argument\n"); } else if(argc == 4){ sendcmd(atoi(argv[0]), atoi(argv[1]),atoi(argv[2])); } else{ printf("GionjiLed\n"); printf("Usage:\n"); printf("gionjiled [red | green | blue | yellow | pink | cyan | white | black]\n"); printf("gionjiled R G B [from 0 to 254]\n"); printf("Enjoy"); } }
int read_port() { char *portname = "/dev/ttyACM0"; int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { printf("error %d opening %s: %s", errno, portname, strerror (errno)); return(0); } set_interface_attribs (fd, B9600, 0); // set speed to 9600 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking write (fd, "hello!\n", 7); // send 7 character greeting char buf [100]; int n = read (fd, buf, sizeof buf); // read up to 100 characters if ready to read printf("data is:",fd); }
int serial_init(int* fd,const char* seriale_dev) { /* apro la porta seriale*/ const char * portname = seriale_dev; *fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC); if (*fd < 0) { printf("error %d opening %s: %s", errno, portname, strerror (errno)); return -1; } /*imposto baud rate*/ set_interface_attribs (*fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (*fd, 0); return 1; }
int KeypadController::setup() { fd = open(keypadname.toLocal8Bit().data(), O_RDWR); //Sets FD to open said port if (fd < 0) //If Statement produces error message if selected port cannot be opened { qDebug() << "error" << errno << " opening " << keypadname << strerror(errno); return 1; } set_interface_attribs(B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking(0); // Set up Ports on PIO sendToKeypad("@00D000\r"); // Sets Port A to output // qDebug() << response; sendToKeypad("@00D1FF\r"); // Sets Port B to input // qDebug() << response; sendToKeypad("@00D200\r"); // Sets Port C to output // qDebug() << response; return 0; }
int main() { rs232_fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); if (rs232_fd < 0) { fprintf(stderr, "RS232 error\n"); exit(1); } set_interface_attribs(rs232_fd, B115200, 0); set_blocking(rs232_fd, 0); write(rs232_fd, "PCready\n", 8); printf("READY\n"); while(1) { int array[2]; array[0] = 0, array[1] = 1; toggle_sound(array, 2, 1); usleep(10000000); } /* while(1) { fprintf(rs232, "PC ready\n"); fgets(buffer, 10000, rs232); if (strcmp(buffer, "DE2 ready") == 0) { break; } } */ /* int mode; scanf("%d", &n); switch (mode) { case 1: break; case 2: break; } */ return 0; }
void main() { unsigned short opencmd = 0x0100; unsigned short closecmd = 0x0200; unsigned char openstr[2]={0x00,0x01}; unsigned char closestr[2]={0x00,0x02}; char *portname = "/dev/ttyUSB0"; int temp; char chout; int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { printf ("error %d opening %s: %s", errno, portname, strerror (errno)); return; } set_interface_attribs (fd, B1000000, 0); // set speed to 1000000 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking //write (fd, (char *)&opencmd, sizeof(unsigned short)); // send 2 character write (fd, (char *)closestr, 2); // send 2 character //char buf [100]; //int n = read (fd, buf, sizeof(buf)); // read up to 100 characters if ready to read while(1) { if(read(fd, &chout, sizeof(chout))) /* Read character from ABU (Auto buffering Unit) */ { temp = chout; printf("Got %02X.\n", temp); } chout=0; usleep(5000); } close(fd); }
void connectBluetooth(int* fd) { char buf[BUF_SIZE]; unsigned int t; int n = 0; int res; connecting = 1; *fd = open ("/dev/rfcomm0", O_RDWR | O_NOCTTY | O_SYNC); int flags = fcntl(*fd, F_GETFL, 0); fcntl(*fd, F_SETFL, flags | O_NONBLOCK); set_interface_attribs (*fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (*fd, 0); // set no blocking while(read(*fd, buf, BUF_SIZE) > 0) ; write(*fd, "ECHO", 4); t_echo = SDL_GetTicks(); }
/* Initialize and open UART communication * Return uart handle if successful * baudrate is set to 115200 */ int uart_init(void) { mraa_uart_context uart_dev = NULL; char *uart_dev_path = NULL; if ((uart_dev = mraa_uart_init(0)) == NULL) { printf("ERROR : UART\t mraa uart init failed\n"); return -1; } if ((uart_dev_path = mraa_uart_get_dev_path(uart_dev)) == NULL) { printf("ERROR : UART\t mraa uart path does not exist"); return -1; } if((uart = open(uart_dev_path, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) { printf("ERROR : UART\t opening serial device"); return -1; } set_interface_attribs(uart, B115200, 0); set_blocking(uart, 0); printf("UART successfully initialized on %s\n", uart_dev_path); tcflush(uart, TCIOFLUSH); return 0; }