Serial port communication - vb.net

i'm creating a windows form to send/receive data to/from serial port.
At first : i send the data as string to the serial port .
Second: i tried to read the string again for test the successfull transmission , but i recieved empty string
this is my code :
Try
Dim SerialPort1
As New SerialPort("Com1",9600, Parity.None, 8, StopBits.One)
SerialPort1.Open()
SerialPort1.DtrEnabled=True
SerialPort1.WriteLine("This is my test message ." )
' ================= Read from serial port
Label1.Text=SerialPort1.ReadExisting() ' this returns empty string
Catch ex As Exception
MessageBox.Show(
"Error writing to serial port:" & ex.Message)
Finally
SerialPort1.Close()
End Try
i need to ask another question:
is it required to connect device to serial port to send/recieve data successfully ????
please i need an urgent help
thanks

As Heinzi already mentioned, if you want to see data you need to have some coming in. There is no automatic echo of data you send out.
To answer your other question: Yes, you need to have another device connected to your serial port in order to send/receive data successfully. With no other device, what would be the point?
Fortunately for you, the "device" you connect can be as simple as a plug with some wires. Here is a set of instructions and diagrams for building a so-called loopback plug: http://www.airborn.com.au/serial/rs232.html
This will allow you to echo your output to your input using very simple hardware. If you're not into soldering up your own plug, you can use a so-called breakout box or board. Here's an example: http://www.breakoutboxes.com/D-Series-9-Position-Breakout-Board_p_31.html .

ReadExisting returns the data sent by the device you are communicating with, not the data sent by you.

You could try to use com0com for generating a virtual serial port pair, then you can rename one of these virtual ports to common name like "COM4". You should open other SW like hyperterminal for serial communication, then set to open the remaining port from the pair.
You may try to use com0com fist with two hyperterminals.

You will need a device through which your application will send and receive data.
For testing, you might consider creating a virtual serial port with software like this Virtual Serial Port Driver. It allows you to create serial ports that aren't actually connected to any physical device. You can then debug your program with another program or with something like HyperTerminal or PuTTY.

Assuming you have a loopback plug (simply connect pins 2 and 3 together) so that anything you send is immediately received.
However - you need to consider that the RS232 is slowly. ("S L O W L Y") and the transfer happens asynchronously so your program is hitting the readexisting long before the data has even been sent. So you're reading an empty buffer.
Just for the sake of your test you need to put some delay in there. so that you can wait a moment after sending before trying to receive.
In a real application though you'd use the receive event to read the serial port.

Related

send data to serial Keil uvision

I am looking for a GUI window in Keil uvision4 to send data to the serial. When I open serial window (USART #1-3), I am not able to type any characters on the screen to send to the serial.
Is there a way to manually type data that will be seen by the simulating environment (uvision) as having been received on the serial port?
The serial window should do what you're asking for in simulation. You may need to confirm that you have correctly configured your simulation, and the serial port you're using is the same one which you are monitoring in software.

MBED Serial dropping data

I use MBED (online IDE & libraries) for my application with host board NUCLEO-411RE and 4D Systems touch display connected by full duplex serial communication.
I am able to send data successfully from host to display without errors. However when sending data from display back to host I am losing data.
Reducing baud to 9600 does not solve the problem.
The host processor remains in a super loop with the first action to check if LCD sends serial data ( lcd4d.readable() ).
Host then receives one character at a time ( lcd4D.getc() ), echos it to the PC via usb ( pc.printf(&recChar) ) and does some further processing.
I am also monitoring the physical host receive pin on a separate terminal session. Using this I am certain that the LCD sends data correctly, however this data is not received and echoed correctly by the host processor (echo to PC is only used for debugging purposes).
Refer to super loop code snippet:
do {
if ( lcd4D.readable() ) {
recChar = lcd4D.getc();
pc.printf(&recChar);
lcd4D_intr_Rx();
}
Also refer to attached screen print showing terminal left PC echo (data loss) and terminal right hardware pin monitor (confirming data sent correctly).
Implementing SerialRX interrupt also does not help the situation with data loss still occurring.
Thanks for any suggestions; I am out of ideas.
I have solved the problem.
The issue was that the host processor needed to respond fast enough to the serial data received. I basically implemented a fast serial receive buffer and ensured that received characters are buffered immediately upon interrupt.

PySerial: Create serial object without opening port

I'm running some code on Arduino, I have website with a slider which uses a cgi script to pass values to Python. Then the python script uses pySerial to send the values to Arduino which controls a motor.
The plan was to be able to control the motor speed using the slider on the website.
However I seem to be coming up against a problem of the Arduino resetting whenever I change a value on the website, causing the motor to stop. Is there any way around this? My python code is below.
#!/usr/bin/env python
import cgi
form=cgi.FieldStorage()
import json
ser = serial.Serial('dev/ttyACM0', 9600)
#I think there should be a sleep here for 1.5 seconds
ser.write("%s\n" % (form["value"]))
ser.close()
print "Content-type: application/json"
print
print(json.JSONEncoder().encode({"status":"ok"}))
So, say I have the motor running at 50% speed, then change a value on the website, it runs this script which executes the serial.Serial('dev/ttyACM0', 9600) command. That opens the port which resets the arduino, stopping the motor before passing the new command.
Is there any way to pass ser.write("%s\n" % (form["value"])) to the arduino without freshly opening the port? Such as leaving the port open between python sessions?
If not, is there any other way around this problem?
I believe the reset is due to a hardware design of the specific Arduino device you are using. I suggest using an Arduino Micro or Leonardo board instead. They use a virtual serial port and should not restart your sketch each time a serial port is opened. This is from the Arduino site:
No reset when you open the serial port.
Unlike the Arduino Uno, the Leonardo and Micro won't restart your
sketch when you open a serial port on the computer. That means you
won't see serial data that's already been sent to the computer by the
board, including, for example, most data sent in the setup() function.
Serial re-enumeration on reset.
Since the boards do not have a dedicated chip to handle serial
communication, it means that the serial port is virtual -- it's a
software routine, both on your operating system, and on the board
itself. Just as your computer creates an instance of the serial port
driver when you plug in any Arduino, the Leonardo/Micro creates a
serial instance whenever it runs its bootloader. The board is an
instance of USB's Connected Device Class (CDC) driver. This means that
every time you reset the board, the USB serial connection will be
broken and re-established. The board will disappear from the list of
serial ports, and the list will re-enumerate. Any program that has an
open serial connection to the Leonardo will lose its connection. This
is in contrast to the Arduino Uno, with which you can reset the main
processor (the ATmega328P) without closing the USB connection (which
is maintained by the secondary ATmega8U2 or ATmega16U2 processor).
This difference has implications for driver installation, uploading,
and communication...
I you might be able to use the setDTR(False), but I have tested this yet. A while back they fixed the bug that were initially associated with setDTR. What operating system are you use this on?
ser = serial.Serial('dev/ttyACM0', 9600)
ser.timeout = 1
ser.setDTR(False)
Let us know if this does not work.

Receive data by some processes

Can I receive data from network by some processes simultaneously?
For instance, I have two computes in the LAN. One computer send udp packet to other computer on port 5200. In computer number two I want to receive this packet by two processes. Can I create two sockets on same ip and port?
I forget to say that Process A I can't modify. In other words, I want to create application that receive same data as Process A. (Proccess A and Proccess B locate on the computer number two that receive data)
Yes! You can. Open the socket and set setsockopt with REUSE_PORT and REUSE_ADDRESS.
How about you create process A to act as middleware between the two processes B and C. And then add extra data to the packets sent to process A which will be used to determine the final destination of the data - process B or process C.
EDIT:
To answer your question exactly "no", for TCP/IP
"You can only have one application listening on a single port at one time."
Actually you question has been asked by someone else before, and I have only just cited the answer. The full answer can be found -> here.

How to access a PCMCIA modem's serial number?

A Sprint cellular modem plugs into a laptop - often the PCMCIA slot. To connect, the user opens a program called the Connection Manager. This program has a menu option to display the serial number and phone number of the modem that's currently inserted.
Does that imply that the serial/phone number of the modem could be available to other programs running on the system? (Note: let's assume this is Windows XP.)
Specifically, could a company's VPN software be configured to pass along information about which modem is being used to connect?
Finally, is there existing VPN software that already does this, or would it have to be custom-programmed?
Sometimes you can get the modem's serial number using the AT command set. To see this in action, go to your control panel and open up Phone and Modem Options. Select the Modems tab, select the modem you're interested in, and choose Properties.
In the modem window, select the Diagnostics tab, and press the Query Modem button.
This opens the serial port and sends a series of AT commands to gather various settings and information. You can open the serial port in your program (or a terminal program), send the AT command, and get the same information back.
You may need to check your specific modem's AT command set to find where the serial number is stored, or use a serial port spy program to see how Sprint's program does it.
I'm not aware of any VPNs that use this information, and I can think of several ways to spoof it, since communications between the modem and the computer are not cryptographically secure.
-Adam
Open hyperterminal or make a serial port connection programatically and use Hayes AT language to talk to it. Most software also has it listed in the device properties and/or diagnostics.
AT+GSN
press enter