Active2 years, 3 months ago
Nov 01, 2006 How to read serial port (/dev/ttyS0) I'm having some troubles reading the serial port, I'm traying to communicate two machines (one with winxp and the other with slackware) when I set up the communication at 1200, 7, even parity and one(1) stop bits it works fine sending. In Linux/BSD, Serial port is more accessible then windows. So you can even write to it from shell/terminal, or use system call from most programming languages. Example in shell with an Android phone as modem, it may help for debugging: Reading serial port (need to be root): sudo su cat /dev/ttyACM0 As you can read just few lines as needed.
I know this question is scattered all over the internet, but still, nothing is getting me completely there yet. I want to write data to a serial port in C++ (linux) for a a Propeller board. Program works fine when taking input from the console, but when I write strings to it always return:
ERROR - Invalid command
from the device. I tried creating array of char
with Hex values then it worked. here's a working code, below. Chrome download windows 10. But how will i be able to just provide a string variable of command and send it to the serial port? perhaps, how do you I convert it to hex values if it's the only way? Thanks everyonenote: the loop is to use user input from console. What i need is a way to send a string variable to the serial port.
user33359662,04544 gold badges2323 silver badges2828 bronze badges
haikalpribadihaikalpribadi
30911 gold badge66 silver badges1818 bronze badges
2 Answers
I'm happy to solve my own solution but yet disappointed to not have seen the trivial matter much sooner.
char
by default are signed
in c++, which makes it holding the range -128 to 127. Cyberlink power dvd 10 free. However, we are expecting the ASCII values which are 0 to 255. Hence it's as simple as declaring it to be unsigned char str[]
and everything else should work. Silly me, Silly me. Still, Thank you everyone for helping me!!!
haikalpribadihaikalpribadi30911 gold badge66 silver badges1818 bronze badges
Are you sure you should end with 'r'? When entering text from console the return key will result in a 'n' character (on Linux) and not 'r'
Also error checking is missing on most functions (
open()
, fcntl()
, etc.). Maybe one of these functions fail. To find out how to check for errors read the man page (for example man 2 open
for the open()
command. In case of open()
the man page explains it returns -1 when it could not open the file/port.Linux Write To Serial Portsmouth
After your edit you wrote:
which is wrong.
rvestrlen
expects a '0' terminated string which str is obviously not so now it sends your data and whatever there is in memory until it sees a '0'. You need to add 0x00
to your str
array.Linux Write To Serial Port From Shell
rve3,75233 gold badges3030 silver badges5656 bronze badges