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; }
int main(int argc, char *argv[]) { signal(SIGINT, int_handler); signal(SIGTERM, int_handler); if (argc > 1) { portname = argv[1]; } if (init_serial() > 0) { printf("open port:(%s) failed, are you root?\n", portname); exit(-1); } // Initialize DDS Section int status; dds_qos_t * qos = NULL; dds_entity_t participant; dds_entity_t reader_topic; dds_entity_t subscriber; dds_entity_t reader; dds_readerlistener_t listener; // Initialize DDS status = dds_init(argc, argv); DDS_ERR_CHECK(status, DDS_CHECK_REPORT | DDS_CHECK_EXIT); // Create participant status = dds_participant_create(&participant, DDS_DOMAIN_DEFAULT, qos, NULL); DDS_ERR_CHECK(status, DDS_CHECK_REPORT | DDS_CHECK_EXIT); // Create a subscriber status = dds_subscriber_create(participant, &subscriber, qos, NULL); DDS_ERR_CHECK(status, DDS_CHECK_REPORT | DDS_CHECK_EXIT); // Create reader topic status = dds_topic_create(participant, &reader_topic, &Motion_coordinate_desc, "motion", NULL, NULL); DDS_ERR_CHECK(status, DDS_CHECK_REPORT | DDS_CHECK_EXIT); // data available callback memset(&listener, 0, sizeof (listener)); listener.on_data_available = data_available_handler; // Create reader with DATA_AVAILABLE status condition enabled qos = dds_qos_create(); dds_qset_reliability(qos, DDS_RELIABILITY_BEST_EFFORT, DDS_SECS (1)); dds_qset_history (qos, DDS_HISTORY_KEEP_ALL, 0); status = dds_reader_create(subscriber, &reader, reader_topic, qos, &listener); DDS_ERR_CHECK(status, DDS_CHECK_REPORT | DDS_CHECK_EXIT); dds_qos_delete (qos); printf("start listening\n"); while (do_loop) { dds_sleepfor (DDS_MSECS(10)); } // Clean up section rs232_end(port); dds_status_set_enabled(reader, 0); // disable reader callback DDS_ERR_CHECK(status, DDS_CHECK_REPORT | DDS_CHECK_EXIT); dds_entity_delete(participant); dds_fini(); exit(0); }