linux - C code for non-standard baud rate on Debian/Raspberry Pi -


I am working with a hardware device running only with 625000 non-standard baud rates.

I need to connect and read and write this device via USB port. That's why I'm trying to develop a small program that will allow me to do this. However, this code needs to work in Linux environment (/) and unfortunately my Linux skills are only preliminary.

So, I was hoping something could explain to me in a simplest form (code example would be great!) How I founded a non-standard baud rate of 625000 Linux, from my hardware device (ttyUSB0) Connects, and backs up 7 bytes from the device (write a bit stream to 0x02 0x01) and read from it.

I have seen the following stack overflow questions:

And the other ...

However, the holes in my Linux knowledge are just good enough to make me the necessary connection For how can I do this

after stumbling over the following code on a little more searching :

Below, there is a copy of the code given above Area is called down, but should now be much easier to implement it.

  Add # & lt; String.h & gt; #to & lt include, stdlib.h & gt; #to & lt include, stdio.h & gt; #to & lt include, errno.h & gt; # Include & lt; Unistd.h & gt; #to & lt include, fcntl.h & gt; #to & lt include, termio.h & gt; #include & lt; Linux / serial.h & gt; Fixed int rate_content (int baudet) {# Diffin B (X) Case X: Return B ## X Switch (Bondet) {B (50); B (75); B (110); B (134); B (150); B (200); B (300); B (600); B (1200); B (1800); B (2400); B (4800); B (9600); B (19200); B (38400); B (57600); B (115200); B (230400); B (460,800); B (500000); B (576,000); B (921,600); B (1000000) B (1152000) B (1500000); Default: return 0; } #undef B} int main () {struct termios option; Straight serial_struct serinfo; Int FD; Int speed = 0; Int rate = 625000; / * Open the serial port / configure / if ((fd = open ( "/ dev / ttyUSB0", O_RDWR | O_NOCTTY)) == -1) {return -1; } // If you entered a standard baud, then speed up the given function = rate_to_constant (rate); If (speed == 0) {/ * custom separator * / serinfo.reserved_char [0] = 0; If (Aiositiel (FD, Tiosijisiyriyl, and Sserinfo) & lt; 0) return -1; Serinfo.flags & amp; = ~ ASYNC_SPD_MASK; Serinfo.flags | = ASYNC_SPD_CUST; Serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate; If (serinfo.custom_divisor & lt; 1) serinfo.custom_divisor = 1; If (IoCtL (FD, TOCCRial, and CERINFO) & lt; 0) Return -1; If (Aiositiel (FD, Tiosijisiyriyl, and Sserinfo) & lt; 0) return -1; if (! serinfo.custom_divisor * Rate Serinfokbaud_base =) {Warnx ( "actual baudrate is% d /% d =% f", serinfo.baud_base, serinfo.custom_divisor, (boat) serinfo.baud_base / serinfo.custom_divisor); }} Fcntl (fd, F_SETFL, 0); TC jetter (FD, and option); Cfsetispeed (and option, speed ?: B38400); Cfsetospeed (& amp; option, speed ?: B38400); Cfmakeraw (& amp; option); Options.c_cflag | = (CLOCAL | CREAD); Options.c_cflag & amp; = ~ CRTSCTS; If (tcsetattr (fd, TCSANOW, and option)! = 0) {// return -1; } // return FD; Char ping_cmd [] = {2,1}; Four ping_reck [7]; Write (FD, & ping_cmd, sizeof (ping_cmd)); Read (FD, & ping_rec, sizeof (ping_rec)); Int i; For (i = 0; i & lt; sizeof (ping_rec); i ++) {printf ( "% d", ping_rec [i]); } Closed (FD); Return 0; }  

There you will see more smart coders, because I pulled my main code, even all those present "to 1" almost certainly bad programming practice, then , I'm not sure how to clean it and so I would love to hear your suggestions - I will edit as per the suggestions.

In the meantime, should you face a similar problem for me, should do well above.


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -