static void pc98_inhibit_repeat(void) { uint8_t code; while (serial_recv()) ; RETRY: PC98_RDY_PORT |= (1<<PC98_RDY_BIT); _delay_ms(500); serial_send(0x9C); PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); _delay_ms(100); while (!(code = serial_recv())) ; print("PC98: send 9C: "); print_hex8(code); print("\n"); if (code != 0xFA) goto RETRY; PC98_RDY_PORT |= (1<<PC98_RDY_BIT); _delay_ms(100); serial_send(0x70); PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT); _delay_ms(100); //code = serial_recv(); while (!(code = serial_recv())) ; print("PC98: send 70: "); print_hex8(code); print("\n"); if (code != 0xFA) goto RETRY; }
void slip_receive(cpu_state_t* state) { uint8_t c = serial_recv(); if(!in_progress) { if(c != END) return; buf = (uint8_t*)kmalloc(sizeof(uint8_t) * BUFSIZE); in_progress = true; return; } if((c == END && in_progress) || bufpos >= BUFSIZE) { in_progress = false; // TODO IPv6 support net_receive(mydev, NET_PROTO_RAW, bufpos, buf); bufpos = -1; } switch(c) { case ESC: c = serial_recv(); if(c == ESC_END) c = END; else if(c == ESC_ESC) c = ESC; default: buf[bufpos++] = c; } }
void usart1_isr(void) { unsigned char c; /* Check if we were called because of RXNE. */ if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART1) & USART_SR_RXNE) != 0) && (!serial_rb_full(&srx))) { c = serial_recv(); serial_rb_write(&srx, c); } /* Check if we were called because of TXE. */ else if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) && ((USART_SR(USART1) & USART_SR_TXE) != 0)) { if(!serial_rb_empty(&stx)) { serial_send(serial_rb_read(&stx)); } else { /* Disable the TXE interrupt, it's no longer needed. */ USART_CR1(USART1) &= ~USART_CR1_TXEIE; } } else { c = serial_recv(); } }
void matrix_init(void) { DDRD |= (1<<6); PORTD |= (1<<6); //debug_enable = true; serial_init(); // initialize matrix state: all keys off for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; // wait for keyboard coming up // otherwise LED status update fails print("Reseting "); while (1) { print("."); while (serial_recv()); serial_send(0x01); _delay_ms(500); if (serial_recv() == 0xFF) { _delay_ms(500); if (serial_recv() == 0x04) break; } } print(" Done\n"); return; }
uint8_t matrix_scan(void) { is_modified = false; uint8_t code; code = serial_recv(); if (!code) return 0; debug_hex(code); debug(" "); switch (code) { case 0xFF: // reset success: FF 04 print("reset: "); _delay_ms(500); code = serial_recv(); xprintf("%02X\n", code); if (code == 0x04) { // LED status led_set(host_keyboard_leds()); } return 0; case 0xFE: // layout: FE <layout> print("layout: "); _delay_ms(500); xprintf("%02X\n", serial_recv()); return 0; case 0x7E: // reset fail: 7E 01 print("reset fail: "); _delay_ms(500); xprintf("%02X\n", serial_recv()); return 0; case 0x7F: // all keys up for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; return 0; } if (code&0x80) { // break code if (matrix_is_on(ROW(code), COL(code))) { matrix[ROW(code)] &= ~(1<<COL(code)); is_modified = true; } } else { // make code if (!matrix_is_on(ROW(code), COL(code))) { matrix[ROW(code)] |= (1<<COL(code)); is_modified = true; } } return code; }
void serial_rx_task(struct serial_dev * sp) { char buf[1]; int c; #ifndef _WIN32 sigset_t set; sigemptyset(&set); sigaddset(&set, SIGINT); sigaddset(&set, SIGHUP); sigaddset(&set, SIGTERM); sigaddset(&set, SIGQUIT); pthread_sigmask(SIG_BLOCK, &set, NULL); #endif for (;;) { if ((c = serial_recv(sp, buf, 1, 500)) == 0) { printf("!"); fflush(stdout); continue; } if (c < 0) { printf("??"); fflush(stdout); sleep(1); continue; } printf("%02x", c); fflush(stdout); } }
int main (void) { int i = 0; // シリアル通信の作成 serial_t obj = serial_create("COM4",9600); // 受け取る配列 char buf[DATA_SIZE]; // 受け取ったバイト数 char len; if ( obj == NULL ) { fprintf(stderr,"オブジェクト生成に失敗\n"); return EXIT_FAILURE; } while (1) { // バイト数を受け取るとともに、データを受け取る len = serial_recv(obj,buf,sizeof(buf)); //if (len) serial_send(obj,(unsigned char *)buf,len); for (i = 0; i < DATA_SIZE; i++) { printf("but[i] : %d\n", i, buf[i]); } printf("len : %d\n", len); Sleep(100); if ( kbhit() ) break; } serial_delete(obj); return EXIT_SUCCESS; }
long urg_currentTimestamp(urg_t *urg) { char buffer[ScipLineWidth]; long timestamp = -1; int ret = 0; int n; /* Send TM1 */ int expected_ret[] = { 0, -1 }; int send_n = scip_send(&urg->serial_, "TM1\n"); if (send_n != 4) { return SerialSendFail; } ret = scip_recv(&urg->serial_, "TM", NULL, expected_ret, ScipTimeout); if (ret != 0) { return ret; } /* Decode the timestamp and return */ n = serial_getLine(&urg->serial_, buffer, ScipLineWidth, ScipTimeout); if (n == 5) { timestamp = decode(buffer, 4); } /* Read and throw the last response */ n = serial_recv(&urg->serial_, buffer, 1, ScipTimeout); if (! serial_isLF(buffer[0])) { serial_ungetc(&urg->serial_, buffer[0]); } return timestamp; }
static char console_driver_serial_read(console_info_t *info) { char c = serial_recv(); if (c < 0x20 && c != '\n' && c != '\r') return 0; return c; }
uint8_t matrix_scan(void) { is_modified = false; uint8_t code; code = serial_recv(); if (!code) return 0; print_hex8(code); switch (code) { case 0xFF: // reset success case 0xFE: // layout case 0x7E: // reset fail if (code == 0xFF) print("reset: 0xFF "); if (code == 0x7E) print("reset fail: 0x7E "); if (code == 0xFE) print("layout: 0xFE "); // response byte _delay_ms(500); if (code = serial_recv()) print_hex8(code); print("\n"); // FALL THROUGH case 0x7F: // all keys up for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; return 0; } if (code&0x80) { // break code if (matrix_is_on(ROW(code), COL(code))) { matrix[ROW(code)] &= ~(1<<COL(code)); is_modified = true; } } else { // make code if (!matrix_is_on(ROW(code), COL(code))) { matrix[ROW(code)] |= (1<<COL(code)); is_modified = true; } } return code; }
void serial_handler_a(registers_t *regs) { int8_t serial = serial_recv(SERIAL_PORT_A); irq_ack(SERIAL_IRQ); if (serial == 13) { serial = '\n'; } printf("%c", serial); serial_send(SERIAL_PORT_B, serial); }
static int buspirate_recv_bin(struct programmer_t *pgm, char *buf, size_t len) { int rc; rc = serial_recv(&pgm->fd, (unsigned char *)buf, len); if (rc < 0) return EOF; if (verbose > 1) { fprintf(stderr, "%s: buspirate_recv_bin():\n", progname); dump_mem(buf, len); } return len; }
static int serial_handler_ac(struct regs *r) { char serial; int port = 0; if (inportb(SERIAL_PORT_A+1) & 0x01) { port = SERIAL_PORT_A; } else { port = SERIAL_PORT_C; } serial = serial_recv(port); irq_ack(SERIAL_IRQ_AC); uint8_t buf[] = {convert(serial), 0}; write_fs(*pipe_for_port(port), 0, 1, buf); return 1; }
static int serial_handler_bd(struct regs *r) { char serial; int port = 0; debug_print(NOTICE, "Received something on secondary port"); if (inportb(SERIAL_PORT_B+1) & 0x01) { port = SERIAL_PORT_B; } else { port = SERIAL_PORT_D; } serial = serial_recv(port); irq_ack(SERIAL_IRQ_BD); uint8_t buf[] = {convert(serial), 0}; write_fs(*pipe_for_port(port), 0, 1, buf); return 1; }
static int buspirate_getc(struct programmer_t *pgm) { int rc; unsigned char ch = 0; if (pgm->flag & BP_FLAG_IN_BINMODE) { fprintf(stderr, "BusPirate: Internal error: buspirate_getc() called from binmode"); exit(1); } rc = serial_recv(&pgm->fd, &ch, 1); if (rc < 0) return EOF; return ch; }
uint32_t read_serial(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) { if (size < 1) { return 0; } memset(buffer, 0x00, 1); uint32_t collected = 0; while (collected < size) { while (!serial_rcvd((int)node->device)) { switch_task(1); } debug_print(NOTICE, "Data received from TTY"); buffer[collected] = serial_recv((int)node->device); collected++; } return collected; }
void * get_data_routine ( void* parg ) { ISP_EID eid = EID_OK; IspMessage ispmsg; recv_size = MESSAGE_SIZE; eid = serial_recv(&serial_dev, recv_buffer, &recv_size); printf("Received %d bytes\n", recv_size); if (eid == EID_OK) { eid = decode_message(recv_buffer, &ispmsg); if (eid == EID_OK) { // process message switch (ispmsg.hdr.typ) { case MSGT_ACK: printf("Message sent successfully: %d\n", ispmsg.msg.ack.resp); break; case MSGT_SIG_READ: for (int i = 0; i < recv_size; ++i) printf("%02X ", recv_buffer[i]); printf("\n"); printf("-------------\n"); printf("Length: %4d\n", ispmsg.hdr.len); printf("CRC: %04x\n", ispmsg.hdr.crc); printf("Signature: "); for (int i = 0; i < ispmsg.hdr.len; ++i) printf("%02X ", ispmsg.msg.data[i]); printf("\n"); break; default: printf("Invalid message type: %d\n", ispmsg.hdr.typ); break; } } } return (void *)(eid); }
/* read signature bytes - arduino version */ static int arduino_read_sig_bytes(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m) { unsigned char buf[32]; /* Signature byte reads are always 3 bytes. */ if (m->size < 3) { fprintf(stderr, "%s: memsize too small for sig byte read", progname); return -1; } buf[0] = Cmnd_STK_READ_SIGN; buf[1] = Sync_CRC_EOP; serial_send(&pgm->fd, buf, 2); if (serial_recv(&pgm->fd, buf, 5) < 0) return -1; if (buf[0] == Resp_STK_NOSYNC) { fprintf(stderr, "%s: stk500_cmd(): programmer is out of sync\n", progname); return -1; } else if (buf[0] != Resp_STK_INSYNC) { fprintf(stderr, "\n%s: arduino_read_sig_bytes(): (a) protocol error, " "expect=0x%02x, resp=0x%02x\n", progname, Resp_STK_INSYNC, buf[0]); return -2; } if (buf[4] != Resp_STK_OK) { fprintf(stderr, "\n%s: arduino_read_sig_bytes(): (a) protocol error, " "expect=0x%02x, resp=0x%02x\n", progname, Resp_STK_OK, buf[4]); return -3; } m->buf[0] = buf[1]; m->buf[1] = buf[2]; m->buf[2] = buf[3]; return 3; }
int main() { serial_t obj = serial_create( "COM3", 9600 ); char buf[128], len; if( obj == NULL ) { fprintf( stderr, "オブジェクト生成失敗\n" ); exit(1); } while(1) { len = serial_recv( obj, buf, sizeof(buf) ); printf("buf[0] : %d\n", buf[0]); printf("buf[1] : %d\n", buf[1]); printf("buf[2] : %d\n", buf[2]); } serial_delete( obj ); return 0; }
void serial_handler_b(registers_t *regs) { int8_t serial = serial_recv(SERIAL_PORT_B); irq_ack(SERIAL_IRQ - 1); serial_send(SERIAL_PORT_A, serial); }
int getDebugChar(void) { char c; while (!serial_recv(0, (uint8_t *)&c)); return c; }
static int xmodem_send_pkt(struct xmodem_send * sx, int data_len) { unsigned char * pkt = sx->pkt.hdr; unsigned char * cp; int retry = 0; int pkt_len; int ret; int c; if (sx->state == XMODEM_SEND_IDLE) { for (;;) { // Wait for NAK or 'C' if ((ret = serial_recv(sx->dev, pkt, 1, XMODEM_SEND_TMOUT_MS)) <= 0) { if (ret == 0) { DBG(DBG_TRACE, "serial_recv() timed out!"); if (++retry < 20) continue; } DBG(DBG_WARNING, "serial_recv() failed 1!"); return ret; } c = *pkt; if (c == CAN) { DBG(DBG_WARNING, "<-- CAN"); return -1; } if (c == 'C') { DBG(DBG_TRACE, "<-- 'C' (CRC mode)"); sx->state = XMODEM_SEND_CRC; break; } if (c == NAK) { DBG(DBG_TRACE, "<-- NAK (Checksum mode)"); sx->state = XMODEM_SEND_CKS; break; } } } if (data_len) { unsigned char * fcs; if (data_len == 1024) pkt[0] = STX; else if (data_len == 128) pkt[0] = SOH; else return -1; pkt[1] = sx->seq; pkt[2] = ~sx->seq; cp = &pkt[3]; fcs = &pkt[3 + data_len]; if (sx->state == XMODEM_SEND_CRC) { unsigned short crc = 0; int i; for (i = 0; i < data_len; ++i) crc = CRC16CCITT(crc, cp[i]); fcs[0] = crc >> 8; fcs[1] = crc & 0xff; pkt_len = 3 + data_len + 2; } else {