Hello,
I have problem with my OLIMEXINO-NANO (ATMEGA32U4 Arduino Leonardo compatible) with MOD-RS232 (MAX3232CDR chip by TI).
I wanted to send 10bytes (for example 10 times 0x00) using Serial1.write function. Below is my code:
byte byteOne[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
Serial.read();
Serial.write(byteOne, 10);
Serial1.write(byteOne, 10);
}
}
To send frame, any data on USB must have been sent.
The problem is that - on USB channel (Serial) I have proper frame with 10x 00, but on RS232 (Serial1) only first 4 bytes are zeros and the rest are random:
Have anyone meet the same problem?
How can I "repair" my code to avoid this situation?
simple.ino (251 Bytes)
The problem is that - on USB channel (Serial) I have proper frame with 10x 00, but on RS232 (Serial1)
Serial1 is NOT a RS232 port. What IS connected to the RX1 and TX1 pins?
PaulS:
Serial1 is NOT a RS232 port. What IS connected to the RX1 and TX1 pins?
I connected them to port#1 on my computer - physical RS232 on DB9 without any adapters.
OK, I managed with problem. I modified my code:
byte byteOne[10];
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
static int j = 0;
if (Serial.available())
{
// read from port 0, send to port 1:
char ch = Serial.read();
byteOne[j] = ch - '0';
j++;
if (Serial.available() == 0)
{
for (int i = 0; i < 10; i++)
{
delay(2);
Serial1.write(byteOne[i]);
}
j = 0;
}
}
}
I didn't change electrically anything. I put only delay (2ms) between each bytes and it started work properly. Delay 1ms is not enough, so I think there was a problem.
if (Serial.available())
{
// read from port 0, send to port 1:
char ch = Serial.read();
byteOne[j] = ch - '0';
j++;
if (Serial.available() == 0)
...
You have a race condition here, I would not advise doing it this way.
if (Serial.available())
{
...
char ch = Serial.read();
...
if (Serial.available() == 0)
What if two bytes are available? It passes the first test but not the second.
Have a look at the examples in serial input basics
...R
OK, sorry for last code:
byte byteOne[10] ={0,0,0,0,0,0,0,0,0,0};
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
static int j = 0;
if (Serial.available())
{
Serial.read();
if (Serial.available() == 0)
{
for (int i = 0; i < 10; i++)
{
Serial1.write(byteOne[i]);
}
}
}
}
On this code, on Serial1 I'm not sending 10 bytes each 0x00, but after 4th bytes there is random (I guess) values. This is my problem. I know that if I type 2 bytes, it will not working properly.
Yes, I read that now. I get it. Nevertheless I still has a problem with sending frame using Serial1, as I write. I modify my sketch to only send frame per 1 second and it is still sending "random" values even I assign all bytes in frame:
byte byteOne[10] ={0,0,0,0,0,0,0,0,0,0};
void setup() {
Serial1.begin(9600);
}
void loop() {
delay(1000);
for (int i = 0; i < 10; i++)
{
Serial1.write(byteOne[i]);
}
}