I'm using SerialPort https://www.npmjs.com/package/serialport in a node.JS test script. I need to write the following sequence:
"\xc0\x00--EOF-Pattern--"
The device behind the Serial port expects the bytes to transmit and then the EOF pattern to show the end of the payload. A number of other binary send works fine.
However when I try to send "0xC0" it always ends in sending crap bytes. It looks that my hex is always somewhere translated into Unicode (or something similar).
Even when I do something like
var buff = Buffer.from("c0002d2d454f462d2d5061747465726e2d2d","hex");
and send "buff" to the SerialPort the Hex sequence is destroyed. Again - this only happens when a "0xC0" is in.
Any hint what I can do ? How to prevent ANY recoding on the serial line API ?
Related
I simply have run out of ideas.
After following this codethrough which deals with socket programming in c I wanted to add some funtionality.
In the video the message to be send from the client to the server is hardcoded into the client.c file, like so
#define DATA "String to send to server"
I wanted to be able to parse a string from the command line instead, like so
./client localhost "String to send to server"
This was done by defining
#define DATA argv[2]
The problem is that if I hardcode the string into the c file the full message is passed through but if I do the argv[2] only the first 4 characters show up on the output from the server, kind of like this:
client(12345) -> server(12345)
client 12345 -> server(1234)
What am I doing wrong here ? It must be on the client side since it works as long as the message to send is hardcoded into the client.c file.
Full code can be found at my Github.
I need some help on how to configure a new port and how to send/receive data from that port.
thus far I have:
import serial
ourPort1 = serial.Serial(
port = 0, #how to define for rs-232, rs-485, or usb
baudrate = 9600, #can i set this so its can also use 38600?
parity = serial.PARITY_NONE,
bytesize = serial.EIGHTBITS,
stopbits = serial.STOPBITS_ONE
)
I have tried reading the official documentation but I'm not sure how to approach sending 5byte commands and receiving 24 byte status packets with PySerial. Any help or references would help me a lot. I can't seem to find much for this module.
This may be way too late an answer, but I'll take a crack at it anyway:
the port property should be a string with the name of the port you want to connect to: on Windows it'll be something like "COM1", on Linux "/dev/ttyS0".
You can then use ourPort1.write(msg) to send your 5-byte message, where msg is a string 5 characters long. Be aware that if you want to send literal integers as bytes instead of ASCII values, you'll need to do a bit of extra work.
Use reply = ourPort1.read(24) to read 24 bytes into a variable called reply. Once again, this will be stored as a string full of ASCII characters by default -- you'll have to use chr() and ord() to convert back and forth between byte value and ASCII letters.
For more info, start here: http://pyserial.sourceforge.net/pyserial_api.html
I'm receiving UDP packets from a server ( exactly: Open Sound Control packets). i'm storing those packets in a ByteArray.
I want to convert this ByteArray into String so i can exploit the data received. I tried a lot of conversion but each time i'm having non readable charachters.
Here is the code :
| server peerAddr |
server := SocketAccessor newUDPserverAtPort: 3333.
peerAddr := IPSocketAddress new.
buffer := ByteArray new: 1024.
[ server readWait.
server receiveFrom: peerAddr buffer: buffer.
Transcript show: (buffer asString) ; cr ; flush. ] repeat.
I also tried the following conversion but in vain:
buffer asByteString.
buffer asStringEncoding:#UTF8.
buffer asStringEncoding:#UTF16.
buffer asString.
buffer asBase64String.
buffer asFourByteString
buffer withEncoding: #ASCII
Here is the string output :
Any help?
Additional info: The received data is open sound control data so it has a specific formatting, that's why it's diplayed like that, i need to parse ints, floats, strings, whitin a specific bytearray indexs. Does anyone recomand a package that offer those possibilities ?
Thx in advance.
if you want to read the data from the byte array, use the UninterpretedBytes class for that.
You can do:
ubytes := UninterpretedBytes from: aByteArray.
ubytes doubleAt: 5.
stuff like that.
you can also use the uninterpreted bytes to read a string from the bytes.
The correct way to convert bytes to string is definitely applying the right character encoding. The following
(65 to: 75) asByteArray asStringEncoding: #UTF8
should yield
'ABCDEFGHIJK'
Using #asStringEncoding: is the right way to do this. However looking at your screen capture it seems that the bytes you're receiving are not a straight string. There's probably some binary packet format that you need to take apart first and then only decode into strings those parts that you know are actually utf8 encoded (or whatever the encoding is).
You probably can borrow a lot of the code for Squeak's OSC package: http://opensoundcontrol.org/implementation/squeak-osc
I'm providing a route in my express app that provides the contents of a cloud file as a download. I have access to the cloud file's input stream, and I'd like to pipe that directly into the response's output stream. However, I'm using express, which doesn't seem to support an input stream.
I was hoping I could do this:
res.send (cloudInputStream);
but this doesn't work. Express' send takes a body or a buffer, but apparently not an input stream.
Since that's the case, what I'd like to do is set the headers using res.setHeader(), then get access to the raw output stream, and then:
cloudInputStream.pipe (responseOutputStream);
Is this possible?
Alternatively, I could turn read the input stream into a buffer, and provide that buffer to send. However, this reads the entire cloud file's contents into memory at one time, which I'd like to avoid.
Any thoughts?
All you have to do is cloudInputStream.pipe(res) after setting your headers.
You can do anything node can do. Use pipe for streams and res.set for header fields, or res.sendfile.
I want to send data hex format (0x01) or (0xff) to the port where rs232 cable is connected. I am using the MSCOMM control in VC++ 6.0. Is that the correct way to send the hex data. This is the code I have.
CString output;
UCHAR data1,data2;
data1=0x01;
output.Format("%x",data1);
m_mscom.SetOutput(COleVariant(output));
data2=0xff;
output.Format("%x",data2);
m_mscom.SetOutput(COleVariant(output));
If it is not correct, please tell me how to send hex format data to the port.
Thanks in Advance
If your data is simply a hex value that you want to send out (as opposed to a hex formatted string), you probably want "%c" rather than "%x". Given a data value of (e.g.) 0x65, "%x" will produce two bytes on the serial port: '6' (ascii value 54) and '5' (ascii value 53). "%c" will produce a single byte on the serial port: 'e' (ascii value 100 or 0x65).
As far as sending data on a serial port in C, have a look at CSerial or CSerialPort: they may simplify things for you a bit (note that I've not used them, I tend to do serial port apps in python with the pyserial module or in wxWidgets with the ctb library).
Edit: The other one that's quite good (I have used it before, but couldn't find the link when I wrote the original post) is CSerialFile, part of the WFC libraries. As I said, I tend to use wxWidgets now, but if you're using the Microsoft foundation classes, CSerialFile does make serial access very easy.
I'm no familiar with MSCOM but it seems like it won't work. Format may re-format the data to an ASCII string representation instead.
Alternatively, you can just use any serial port as a 'file' in Windows. Look at the windows api for opening files and you will see that you can address certain devices as files by using a filename like 'COM1:' instead.
Then, you can read/write from it like a file.