में सीरियल पोर्ट पर STRINGS लिखना मुझे पता है कि यह प्रश्न इंटरनेट पर बिखरा हुआ है, लेकिन फिर भी, मुझे अभी तक पूरी तरह से कुछ भी नहीं मिल रहा है। मैं एक प्रोपेलर बोर्ड के लिए सी ++ (लिनक्स) में एक सीरियल पोर्ट में डेटा लिखना चाहता हूं। कंसोल से इनपुट लेते समय प्रोग्राम ठीक काम करता है, लेकिन जब मैं इसे स्ट्रिंग लिखता हूं तो हमेशा डिवाइस से ERROR - Invalid command
लौटाता है। मैंने हेक्स मानों के साथ char
की सरणी बनाने का प्रयास किया, फिर यह काम किया। नीचे एक कामकाजी कोड है। लेकिन मैं केवल एक स्ट्रिंग वैरिएबल प्रदान करने में सक्षम हूं और इसे सीरियल पोर्ट पर भेज सकता हूं? शायद, मैं इसे हेक्स मानों में कैसे परिवर्तित करूं यदि यह एकमात्र तरीका है? धन्यवाद सभीसी ++ लिनक्स
नोट: लूप कंसोल से उपयोगकर्ता इनपुट का उपयोग करना है। मुझे सीरियल पोर्ट में एक स्ट्रिंग वैरिएबल भेजने का एक तरीका है।
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(int argc,char** argv){
struct termios tio;
struct termios stdio;
int tty_fd;
fd_set rdset;
unsigned char c='D';
printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
cfsetospeed(&tio,B115200); // 115200 baud
cfsetispeed(&tio,B115200); // 115200 baud
tcsetattr(tty_fd,TCSANOW,&tio);
//char str[] = {'V','E','R','\r'};
//the above str[] doesn't work although it's exactly the same as the following
char str[] = {0x56, 0x45, 0x52, 0x0D};
write(tty_fd,str,strlen(str));
if (read(tty_fd,&c,1)>0)
write(STDOUT_FILENO,&c,1);
while (c!='q')
{
if (read(tty_fd,&c,1)>0) write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
if (read(STDIN_FILENO,&c,1)>0)
if(c!='q')
write(tty_fd,&c,1); // if new data is available on the console, send it to the serial port
}
close(tty_fd);
}
क्या यह सी या सी ++ है? यदि सी ++, तो आप 'std :: string' का उपयोग क्यों नहीं करते? –
@ बेसिलस्टारनकेविच: मैंने इसे करने का प्रयास किया है: 'std :: string str =" VER \ r "; लिखें (tty_fd, str.data(), str.size()); 'यह अभी भी त्रुटि देता है। मैंने कई तरीकों की कोशिश की है लेकिन केवल हेक्स काम करता है। क्या तुम्हारे पास कोई विचार है? कोड सी से है, लेकिन मेरा प्रोग्राम वास्तव में सी ++ है। –
क्या यह मुझे है, या आपने हमें बताया नहीं है कि त्रुटि वास्तव में क्या कहती है? –