1

I am trying to program a robot that has motordrivers on certain serial ports (i.e. ttyS9)

Through cutecom (as hex input), I can send the following input, which gives me the result I expect:

5aaa0700fffff000 

Now I am trying to achieve the same result with a C program, that does the following:

int port9 = open("/dev/ttyS9", O_RDWR | O_NONBLOCK); char buff[17] = "5aaa0700fffff000"; write(port9, buff, 16); 

I've also tried to initialize buff with the hex values seperately:

buff[0] = 0x5; buff[1] = 0xa; 

etc etc.

Both do not work. Is the problem in my code, or in the driver?

I compile using gcc and then run it with sudo. The open function also returns values that are proper (no errors), as well as the write.

1

2 Answers 2

4

Filling a character array with strings will convert characters to their ascii representation not hex as you need.

char buff[17] = "5aaa0700fffff000"; // Incorrect for saving 0x5aaa0700fffff000 to buff 

As i see, you want to write 8 byte 0x5aaa0700fffff000 on serial port, a char is 8bit (1 byte) and you have to send 8 bytes not 16, so the code should be something like this

buff[0] = 0x5a; buff[1] = 0xaa; ... write(port9, buff, 8); 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this works! However, I've found that it only works when I have cutecom open, which has ttyS6 or ttyS9 open, and then it works only for these ports respectively. Any idea how this is possible?
We don't know if OP uses ASCII encoding. And we also don't know if OP uses characters. In fact using a terminal work is an indicator it might be characters.
@bart-kn - Along the lines of what Olaf is saying... does it work when you manually type that sequence from a serial terminal? And do you have to press Enter or Return for the the command to work? If yes to both those questions, you might be missing an end-of-line character or characters. Either '\r', '\n' or "\r\n". If that's the case, you may need to send something like char buff[18] = "5aaa0700fffff000\n".
@bart-kn - And to add to that thought about the use of newlines (if its an ascii protocol), you should take a look at man tcsetattr, in particular, understand OCRNL ONLCR. It's good practice to setup the TTY the way you need it to work anyway.
-1

but maybe it's a number so you'll need unsigned long long int t= 0x5aaa0700fffff000; write(.., (char *)&t, 8);

that means byte order in memory is completely different to one you'll gain with just assigning bytes as you read it from screen.

9 Comments

...that will invoke Undefined Behavior...long long int is probably 64 bits long, but surely not 128 bits
@LPs: long long is at least 64 bits. It can very well have 128 or more bits.
this is a comment, not an answer.
@Olaf I meant: I can bet 1K billion euros that OP long long int is 64 bit long, but I'm sure that isn't 128 bits. ;)
@LPs: Please provide a reference to the standard where it disallows that. (if you provide a trusted guareantee you will of the money, I will present you a C compiler with 128 bit long long. It actually would make sense on LP64 systems like POSIX. You missed my point: If you want a fixed width interger, use one! The standard provides them.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.