Arduino read from port 1, send to port 0 - serialization

I got this snippet of code from the Blueduino board manufacturer's page. It works fantastically for two-way communication, it seemed simple but after much searching I can't understand what it is doing.
This is the code:
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
}
It's obvious it's writing the in to the out and vice-versa (Rx to Tx I believe) but I am not sure why.
This code is working and when I write in the serial monitor it is received on the phone, but I'm not sure how this code is taking the message I send in the serial monitor and inserting it in the stream.
Thanks in advance for your help.

This code is forwarding messages from one serial device to another.
Most likely between your Arduinos serial interface (to your pc) to the blueduino board.
You cannot connect your PC to the Phone directly and you cannot wire the blueduino-board directly to the serial lines of your PC.
Hence you need something in between. (your arduino) that forwards every single byte.
The code you have basically does the same for both ports. If a byte is in the serial receive buffer, read it and send it to the other port.

Related

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.

GSM Modem GPRS Data Send Delays

I'm trying to make a TCP connection between a server(in this case, my PC) and my telit gl865-dual modem.
I am connecting the modem via serial port(ftdi adaptor) and send or recieve data and commands directly my computer.
The connection can be established and data transmission can be done both ways. But when the modem sends data, there is a delay at least 3-5 seconds, the answer of server can be seen on module in miliseconds.
The commands I use(>> indicates the respond from module):
ad#sd = 1, 0, 4444, "myserversip"
>> CONNECT
Is there a way to arrange send time like server's?
Thanks.
This must be due to slow network.
Check your network speed, if good then you have to see your server side code, why it's delaying.

Set the time to live (TTL) of IP packets for outgoing UDP datagrams on Arduino Ethernet

I'm using an Arduino Ethernet to send UDP datagrams to a remote host. The code I use to send a single datagram is:
Udp.begin(localPort);
...
Udp.beginPacket(remoteIP, remotePort);
Udp.write(data);
Udp.endPacket();
My issue is that I need to customize the TTL of the outgoing UDP/IP packet, but none of Udp.begin, Udp.beginPacket, Udp.write and Udp.endPacket provide a parameter to set such option.
I know that the TTL field belongs to the IP header but it seems you don't handle raw IP packets using Arduino's Ethernet / socket / w5100 libraries.
I looked into the definitions of the above functions, expecially in EthernetUDP::beginPacket where I was wondering to find something useful being it called just before I pass the payload of the message, but I got stuck since it contains not much more than a call to startUDP() (socket.cpp), and the latter deals with methods of the W5100 class that are not clear to me.
Do someone know if there is a somehow high-level facility to set the TTL of a packet, or should one go deeper into the libraries to achieve that?
Finally I found a solution. The WIZnet W5100 socket provide registers that describe the socket's behaviour as documented in W5100 Datasheet Version 1.1.6. One of these registers is Socket 0 IP TTL (S0_TTL) (address 0x0416). I see that those registers are written in the startUDP function (in socket.cpp) in order to set the socket's destination IP address and port:
W5100.writeSnDIPR(s, addr);
W5100.writeSnDPORT(s, port);
so I appended there a call to
W5100.writeSnTTL(s, (uint8_t) 255); // set TTL to 255
and it indeed worked, i.e. the sketch got compiled. Such method is undocumented, I figured it out looking at the other register-writing methods and finding on the web that exists a couple of projects that make use of it.
I also wrote this patch to provide the override Udp.beginPacket(remoteIP, remotePort, ttl) to the Ethernet libraries that come with Arduino 1.0.1 - 2012.05.21.

Serial port communication

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.

Getting Epson receipt printer to print from Arduino

I'm trying to build a microprinter using an Arduino and an Epson TM-T88II receipt/POS printer. The printer uses the Epson Esc/POS system, but I can't get it to do anything at all from the Arduino. I'm doing things like:
#include <SoftwareSerial.h>
#define out_pin 3
#define in_pin 2
SoftwareSerial printer = SoftwareSerial(in_pin, out_pin);
void setup()
{
pinMode(in_pin, INPUT);
pinMode(out_pin, OUTPUT);
printer.begin(9600);
delay(1000);
printer.print(0x1B, BYTE);
printer.print('#'); // ESC(HEX 1B) # is supposed to initialize the printer
printer.print("hello world");
printer.print(0xA, BYTE); // print buffer and line feed
}
I just can't get the printer to respond at all. The printer powers up and prints its self test just fine. It's a serial (RS-232) printer, and I'm connecting it to the Arduino through a MAX233 chip. I've checked and rechecked my connections through the chip, which I think are right based on a friend who has a similar setup working. I read somewhere that the TM-T88 printers need null-modem serial cables, so I bought an adapter, and that didn't seem to make any difference.
I'm new to electronics, so I'm completely stumped. I just want to get it to print something, so I can get to the fun part - the programming :). Any thoughts on things to test/try? I can give more detail on wiring or anything else, just didn't want this to get TOO long.
Are you using an RS-232 transceiver? The Arduino outputs 0 and 5 V for serial, while the printer uses -12 and 12 V for serial. You should use a MAX232 or similar device to get the correct physical interface. (You might be able to cheat if you invert the serial port on the Arduino, but that might not work, and it's more trouble when just getting started.)
Once that's taken care of, the RTS and DTR may be your problem. You should be able to change the DIP-switch settings on the printer and turn off flow control altogether, or switch it to software flow control.
Also, you may need to send both line feed and carriage return.
However, once all that's done it should print just fine, even without any reset commands. Send a bunch of ASCII characters and line feed/carriage returns, and it'll spit it all out.
You can ignore the RX line (on the Arduino side, TX on the printer side) for now - just send it data until you figure out the wiring, level conversion, flow control, etc.
You could check whether you can communicate with a PC, both from the Arduino and to the printer.
I would use an oscilloscope to see if the serial signals come out of the Arduino and the MAX as they should, but then you probably don't have one.
Are you sure the communication settings are correct? You set the baud rate to 9600, but what about data bits, parity, stop bits? What about the control lines?
I'd hook another PC instead of the printer to the other end of the serial cable and run telnet or PuTTY on that system to make sure you are communicating out and actually talking through the serial port. If so, you could use the same solution to send data to the printer to confirm all settings such as number of data bits, parity, etc.
I've done a similar project and had the same issue. You need a null modem / crossover cable to go from max232 to the printer as both devices are in slave configuration