I want to send data from my PI (that doesn't have a serial port) to a serial device.

now for this I understadn I need to create a virtual port on the pi and send the data from it

in windows I use VSPE software , that create my the virtual port and make the connection to the TcoToSerail tcp server (Moxa).

what I have so far is a connection to the TcpServer(the PI is a TCP client ) : from this post :

Raspberry Pi used as a cheap serial-to-WLAN converter

I use this line only :

socat -,raw,echo=0,escape=0x1d tcp:MYServerIP:12345

and I can se there is a connection (the tcp server show me that my PI is connected )

my question now is :

how do I send to it data? I want to send to the server this message:

"0x02 0x30 0x31 0x03 0x38 0x39"

Thanks ,

**** if someone have another solution \ way to do this - I will accept any help

  • Sounds like XY problem - and based on misconception - all Pi have a serial port – Milliways Dec 17 '18 at 22:02
  • I'm not using the serial port ..... I'm using Ip device – David12123 Dec 18 '18 at 8:16
  • BUT your (rather vague) question claims you need to use a "virtual port" (whatever that may be) because it "doesn't have a serial port" . Nowhere do you say WHAT you actually want to do. – Milliways Dec 18 '18 at 8:37

Using the line you've given socat is listening on STDIN for input (the - at the start). So, you can modify your line like so to pipe the message to socat:

printf "\x02\x30\x31\x03\x38\x39" | socat -,raw,echo=0,escape=0x1d tcp:MYServerIP:12345

Have a look at the manual for socat, for fixed messages like in your example you might prefer using one of the other address types such as socat OPEN:"/path/to/command.txt" tcp:MYServerIP:12345 or socat SYSTEM:"printf \"command text\"" tcp:MYServerIP:12345.

You could also use netcat to connect to the server in the same way if you are happy to pipe the commands in:

printf "\x02\x30\x31\x03\x38\x39" | nc MyServerIP:12345
  • yes , when I debug it - I can see he send me the command , so I'm starting to get to where I want. but now I need to send this data in HEX . I thought that if I add "0x.." it will translate it to HEX, but no:-) , so how can I translate it and send this as HEX and notr as ASCII ? Thanks, – David12123 Dec 18 '18 at 8:19
  • Should already be in HEX: printf uses \xHH to generate the HEX code 0xHH. So, if your want to send 0x550xAA you would printf "\x55\xAA". You might need to add some terminating \r\n to your message as well. – Roger Jones Dec 18 '18 at 9:08
  • Thank you! , now it's working. now I need to write a code that does it auto, so I will think I will create a file first and send the content of the file, so how do I do this? I have try "print test | nc myserver 1234" but i get error - "no print mailcap rules found for type text\plain" – David12123 Dec 18 '18 at 9:19
  • You've missed the "f" in "printf" (and the "5" in "12345" as well). To send a file try cat "/path/to/file.txt" | socat ... – Roger Jones Dec 18 '18 at 9:31
  • not working - in the test.txt I have "\x2\x30\x33\x31\x39\x3\x33\x30" - so it send me also the "" , x , \ . can it been ignore? – David12123 Dec 18 '18 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.