Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <strings.h>
- #define SERIALPORT1 "/dev/ttyUSB0"
- #define SERIALPORT2 "/dev/ttyUSB1"
- int main(void)
- {
- int fd, fd2, bytes, i;
- unsigned char a = 0xA1;
- char buf[255];
- // open the data channel
- fd = open(SERIALPORT1, O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd == -1) {
- printf("Error opening data channel!\n");
- } else {
- printf("Data channel open.\n");
- }
- // open the control channel
- fd2 = open(SERIALPORT2, O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd2 == -1) {
- printf("Error opening control channel!\n");
- } else {
- printf("Control channel open.\n");
- }
- // flush I/O buffers on both serial devices
- tcflush(fd, TCIOFLUSH);
- tcflush(fd2, TCIOFLUSH);
- a = 0xA1; // get version info command code
- bytes = write(fd, &a, 1);
- if (bytes != 1) {
- printf("Error sending request: %d (%s)\n", bytes, strerror(errno));
- } else {
- // wait 200ms
- usleep(200000);
- // zero out the buffer
- bzero(buf, 255);
- // read the bytes
- for(i = 0; i < 255; i++) {
- bytes = read(fd, &buf[i], 1);
- if(bytes <= 0)
- break;
- }
- // output the version string
- printf("Version: %s\n", buf);
- printf("bytes read: %d\n", i);
- }
- // must clean up...
- close(fd);
- close(fd2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement