Message Boards Message Boards

Use DeviceWrite with a serial port communication device?

Posted 5 years ago

Hi,

I have a question for you about the serial port communication with Mathematica.

I can establish the communication with a serial port device, in fact, i can receive data from the device, using DeviceRead.

But i can't send any data to the device...

For example, with Putty, i write SHOW_DATE and click enter on the keyboard, and the device gives me his answer. If i execute this command with Mathematica, the device doesn't answer: DeviceWrite[dev,"SHOW_DATE"] I have also tried: DeviceWrite[dev,ToCharacterCode["SHOW_DATE"]]

Can someone help me? How i can send the "text" in the right way, like Putty?

Thank you!!

POSTED BY: Andrea Barresi
5 Replies
Posted 5 years ago

Thank you Neil!

With the carriage return, it works.

POSTED BY: Andrea Barresi

Andrea,

It may also be possible that your device requires a carriage return or line feed (that Putty is putting in automatically). Maybe try

DeviceWrite[dev, "show_date\n"]

Regards

POSTED BY: Neil Singer
Posted 5 years ago

Does DeviceWriteBuffer help?

Although immaterial, does DeviceExecute[dev, "ReadFlush"] after DeviceReadBuffer[dev] help?

POSTED BY: Suba Thomas
Posted 5 years ago

Ok, here is the code.

I open the port:

dev = DeviceOpen[
  "Serial", {"COM3", "BaudRate" -> 9600, "DataBits" -> 8, 
   "Handshake" -> "XOnXOff", "Parity" -> None, "StopBits" -> 1}]

Now, i turn ON the device, and if it passes the self-test, it sends a code to the serial port, so i read it executing this command:

DeviceReadBuffer[dev]

And here is the answer (correct), translated with ToCharacterCode[]:

ΓΏ%001000070

Now, i'd like to send a command to de device, like that:

DeviceWrite[dev, "show_date"]

And again i control if device has asnswered, with DeviceReadBuffer:

DeviceReadBuffer[dev]

But there isn't anything in the buffer.

Important: Note that if i send a wrong command to the device, it should answer me with an error message, so, it doesn't receive the command...

Thank you!

POSTED BY: Andrea Barresi

Andrea,

Please show more code. How are you opening the port? How are you reading the answer back after the Wrtie? Is there a delay from the device so you might have to pause to read it, etc. The problem could be at any part in the process and not necessarily with the DeviceWrite.

Regards,

Neil

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract