-
Notifications
You must be signed in to change notification settings - Fork 17
A Serial to Websockets device #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What if the Serial Monitor RX/TX were available through a web socket? Would that also do the trick? |
A serial duplex communication to the real world would be a dream come true. I hope you don't mind, but my feature request aims higher. It is the same as connecting a Bluetooth or GPS module to pin 0 and 1 of a Arduino Uno. It works, but then a LCD display should be added to show messages. |
I think UART <-> websockets would be very useful. I would definitely look at extending my Python proxy to be bi-directional and less focussed on transporting only LED data. I think a library such as VSync may be useful. It provides a simple mechanism to sync variables between two sketches. It only supports |
Note: there's now an experimental support for connecting to an actual Serial port. Here's an example: https://wokwi.com/arduino/projects/306534287811805760 Only works on Chrome / Edge (other browser do not support the Serial API, and it's not very likely to change in the foreseeable future). You can also use a virtual serial port driver (such as com0com on Windows) to make your Wokwi project speak to another software on your computer (or to a different wokwi sketch) |
This one got high in the feature list, so it's a good time to start discussing the implementation. One way would be to extend For instance, we could add a "url" or a "websocket" attribute that would point at the URL of the web socket. Something along the lines of: {
"id": "serial",
"type": "wokwi-serial-port",
"attrs": { "websocket": "ws://localhost:1234", "baud": "115200" }
} Then, there's also the question of how we encode the data that goes into the socket. The most straightforward way would probably be to pass the binary data as-is, unmodified. We might need to introduce some buffering, as WebSocket are not stream-oriented. They break down the data into messages, and each message gets a 6 bytes header (client -> server). So sending every byte in a message would add a considerable overload, and might slow down the simulation (if we're running at 115,200, this would mean more than 10,000 messages per second). |
A device (such as the Logic Analyzer) could be connected to SoftwareSerial for example. Is the decoding of the serial signals possible ? What if the option to use multiple Arduino boards is implemented first and the websockets device uses parts of a Arduino Uno for the serial signals ? The existing implementation of the serial port via the Serial API of the Chrome browser has a few limitations. The data has to be exactly the same (binary) data of course. When |
It is possible indeed. You can also redirect the built-in Serial Monitor to different pins, and use SoftwareSerial on these pins. There's a guide explaining how set up the Serial Monitor with ATtiny85 + SoftwareSerial, but you can use the same process for the Arduino as well. So for that part we're all set. |
This would be awesome for me to use the simulator to develop this project I am working on. if you visit that GitHub project do yourself a favor and click on the image with the TV in it (few second youtube video) :) My vote for the "special device" would be an FTDI (or CH340 etc) board. The USB port would represent the websocket interface. You could start with just defining the websocket encapsulation protocol and let folks implement their translation in their language of choice. Or just lean on one of the many existing solutions like go-serial-websocket. This way the simulator could talk to localhost or some publically accessible webserver with the physical port. To test you can just connect two actual FTDIs TX->RX and RX-TX boards to your computer and have the serial console running on one and the other is the other one the server drives. Or.... It would not be all that hard for someone to write the server to provide a virtual serial port so you can connect any old serial console to it to interact from your keyboard. Nice thing if the reference implementation is implemented in GO there are no other dependencies a user would need on their computer to run the app and it is super easy to provide binary from many different platforms (Windows, Linux, Mac). Please ignore that I screwed up the wiring to the FTDI interface, it should be TX->RX and RX->TX :) |
In case you want to use it I drew up the Ada Fruit FTDI Friend 30mm x 17mm SVG file: The parts are grouped in the SVG so the name in the left panel in this image is the name of the ID in the SVG. This video also demonstrates the proportions to the actual physical board. Sorry I did not add it as an element and create a PR... I am still coming up to speed on all that..... And I am not sure this is the direction you would want to go. I will be glad to fix all the spacing issues in the drawing if it would actually be useful. |
Note about the com0com workaround (from @jflamy):
|
Note: this is now supported by Wokwi for VS Code. It uses the standard RFC2217 protocol, which is supported by the popular pySerial library. You can learn more here |
Is there a way to connect a Wokwi simulated Arduino's serial port to nodejs? Similar to the com0com suggestion, but I'm on mac so I need something different. Why did implementation of this feature use RFC2217 instead of websockets? It seems there isn't a good RFC2217 node.js library. |
If you are only interested in textual data, then you can treat the RFC2217 server as a standard TCP socket. If you need binary data, then you need to escape/unescape every occurrence of 0xff, but other than that, it's just a plain TCP socket. |
ah ok, it's been a while since I've used sockets but I've tried the following with the simulator running with the
I'm not sure the simulator is properly outputting my |
The simulator is opening a TCP server. You need to connect to it to see the prints. |
is that not what |
Wokwi open a TCP server. You need to connect to it, localhost:4000 (or whatever port you set in Wokwi.toml), not listen on port 4000 |
|
What does your wokwi.toml look like? Are you using a VS Code dev container or WSL? |
I'm on a Mac so not WSL nor a dev container. |
Looks good. Does your code keep printing stuff to the serial monitor? You'll only see what's been printed after netcat has connected. |
I found the correct code to use in order to make com0com compatible with wokwi I recommend using the following code: {
"version": 1,
"author": "AFIF_CODE",
"editor": "wokwi",
"parts": [
{
"type": "board-esp32-devkit-c-v4",
"id": "esp",
"top": 0,
"left": 0,
"attrs": {}
},
{
"id": "serial",
"type": "wokwi-serial-port",
"attrs": { "baud": "115200" }
}
],
"connections": [
["esp:TX", "$serialMonitor:RX", ""],
["esp:RX", "$serialMonitor:TX", ""],
["$serialMonitor:RX", "serial:RX", ""],
["$serialMonitor:TX", "serial:TX", ""]
],
"serialMonitor": {
"display": "always"
}
} |
Feature request: A special device with RX and TX pins that connects to a websocket. A Python3 script could be the websockets server and connect to a serial port on the computer.
If that is possible then a interface to a device (for example a scale with a serial port) can be developed from within Wokwi. It would also be possible to have a real Arduino board communicate with a simulated Arduino board.
The text was updated successfully, but these errors were encountered: